QT中有九种容器组件,分别是组合框QGroupBox、滚动区QScrollArea、工具箱QToolBox、选项卡QTabWidget、控件栈QWidgetStack、框架QFrame、组件QWidget、MDI窗口显示区QMdiArea、停靠窗口QDockWidget。
本博主要介绍:组合框QGroupBox、滚动区QScrollArea、工具箱QToolBox、选项卡QTabWidget
(1)属性
class Q_WIDGETS_EXPORT QStackedWidget : public QFrame
{
Q_OBJECT
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentChanged)
Q_PROPERTY(int count READ count)
。。。。。。。。。。。。。。。
}
(2)常用函数
int addWidget(QWidget *w);
int insertWidget(int index, QWidget *w);
void removeWidget(QWidget *w);
QWidget *currentWidget() const;
int currentIndex() const;
int indexOf(QWidget *) const;
QWidget *widget(int) const;
int count() const;
(3)信号、槽
public Q_SLOTS:
void setCurrentIndex(int index);
void setCurrentWidget(QWidget *w);
Q_SIGNALS:
void currentChanged(int);
void widgetRemoved(int index);
(4)示例
![]()
#include "mainwindow.h"
#include <QApplication>
#include <QListWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QStackedWidget>
#include <QObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QWidget *pWidget = new QWidget(&w);
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
QStackedWidget *pStackedWidget = new QStackedWidget(pWidget);
QListWidget* pList = new QListWidget(pWidget);
pList->insertItem(0, "label0");
pList->insertItem(1, "label1");
pList->insertItem(2, "label2");
QLabel* pLabel0 = new QLabel("label0 test");
QLabel* pLabel1 = new QLabel("label1 test");
QLabel* pLabel2 = new QLabel("label2 test");
pStackedWidget->addWidget(pLabel0);
pStackedWidget->addWidget(pLabel1);
pStackedWidget->addWidget(pLabel2);
w.connect(pList, SIGNAL(currentRowChanged(int)), pStackedWidget, SLOT(setCurrentIndex(int)));
pLayout->addWidget(pList);
pLayout->addWidget(pStackedWidget,0,Qt::AlignCenter);
pLayout->setStretchFactor(pList, 1);
pLayout->setStretchFactor(pStackedWidget, 3);
pWidget->setLayout(pLayout);
w.setCentralWidget(pWidget);
w.setWindowTitle("container test");
w.setMinimumSize(50,50);
w.show();
return a.exec();
}
View Code