[QT]QToolBox 抽屉控件初步学习使用
创建一个不带界面的widget工程201701100038。
main.c
#include "widget.h" #include <QApplication> #include <QDesktopWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; const QRect availableGeometry = QApplication::desktop()->availableGeometry(&w); w.resize(availableGeometry.width()/3,availableGeometry.height()*2/3); w.move((availableGeometry.width()-w.width())/2, (availableGeometry.height()-w.height())/2); w.show(); return a.exec(); }
widget.c
#include <QtWidgets> #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { QToolBox *toolbox = new QToolBox(this); QPushButton *btnNew1 = new QPushButton(tr("New1")); QPushButton *btnNew2 = new QPushButton(tr("New2")); QPushButton *btnNew3 = new QPushButton(tr("New3")); QWidget *page = new QWidget; QGridLayout *layout = new QGridLayout(page); layout->addWidget(btnNew1); page->setLayout(layout); toolbox->addItem(page,tr("new1")); page = new QWidget; layout = new QGridLayout(page); layout->addWidget(btnNew2); page->setLayout(layout); toolbox->addItem(page,tr("new2")); page = new QWidget; layout = new QGridLayout(page); layout->addWidget(btnNew3); page->setLayout(layout); toolbox->addItem(page,tr("new3")); } Widget::~Widget() { }
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: }; #endif // WIDGET_H
程序运行效果:
如果问题解决起来不妥或者有更好的解决办法,麻烦请告知,帮助曾经和你一样的入门者,谢谢。