QT学习:工具盒类
工具盒類即為QToolBox。它提供了一種列狀的層疊窗體,而QToolButton提供了一種快速訪問命令或選擇項(xiàng)的按鈕,通常在工具條中使用。
抽屜效果是軟件界面設(shè)計(jì)中的一種常用形式,可以以一種動態(tài)直觀的方式在大小有限的界面上擴(kuò)展出更多的功能。
下面我們以代碼形式來介紹具體用法:
頭文件內(nèi)容為:
cpp文件內(nèi)容為:
#include "drawer.h" #include<QGroupBox> #include<QVBoxLayout>Drawer::Drawer() {setWindowTitle(tr("My QQ"));//設(shè)置窗口的標(biāo)題toolBtn11=new QToolButton;toolBtn11->setText(tr("小明"));//設(shè)置按鈕名稱toolBtn11->setIcon(QPixmap("1.png"));//頭像toolBtn11->setIconSize(QPixmap("1.png").size());//頭像大小toolBtn11->setAutoRaise(true);toolBtn11->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn12=new QToolButton;toolBtn12->setText(tr("大花貓"));toolBtn12->setIcon(QPixmap("2.png"));toolBtn12->setIconSize(QPixmap("2.png").size());toolBtn12->setAutoRaise(true);toolBtn12->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn13=new QToolButton;toolBtn13->setText(tr("麗麗"));toolBtn13->setIcon(QPixmap("3.png"));toolBtn13->setIconSize(QPixmap("3.png").size());toolBtn13->setAutoRaise(true);toolBtn13->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn14=new QToolButton;toolBtn14->setText(tr("趙四"));toolBtn14->setIcon(QPixmap("4.png"));toolBtn14->setIconSize(QPixmap("4.png").size());toolBtn14->setAutoRaise(true);toolBtn14->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn15=new QToolButton;toolBtn15->setText(tr("孫點(diǎn)"));toolBtn15->setIcon(QPixmap("5.png"));toolBtn15->setIconSize(QPixmap("5.png").size());toolBtn15->setAutoRaise(true);toolBtn15->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);QGroupBox *groupBox1=new QGroupBox;//創(chuàng)建抽屜QVBoxLayout *layout1=new QVBoxLayout(groupBox1);//創(chuàng)建抽屜內(nèi)各個(gè)按鈕的布局layout1->setMargin(10);layout1->setAlignment(Qt::AlignCenter);layout1->addWidget(toolBtn11);layout1->addWidget(toolBtn12);layout1->addWidget(toolBtn13);layout1->addWidget(toolBtn14);layout1->addWidget(toolBtn15);layout1->addStretch();//在按鈕之后插入一個(gè)占位符,是的所有按鈕能夠靠上對齊,并且在整個(gè)抽屜大小發(fā)生改變時(shí)保證按鈕的大小不發(fā)生變化toolBtn21=new QToolButton;toolBtn21->setText(tr("王五"));toolBtn21->setIcon(QPixmap("6.png"));toolBtn21->setIconSize(QPixmap("6.png").size());toolBtn21->setAutoRaise(true);toolBtn21->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn22=new QToolButton;toolBtn22->setText(tr("張三"));toolBtn22->setIcon(QPixmap("7.png"));toolBtn22->setIconSize(QPixmap("7.png").size());toolBtn22->setAutoRaise(true);toolBtn22->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);QGroupBox *groupBox2=new QGroupBox;//創(chuàng)建抽屜QVBoxLayout *layout2=new QVBoxLayout(groupBox2);//創(chuàng)建抽屜內(nèi)各個(gè)按鈕的布局layout2->setMargin(10);layout2->setAlignment(Qt::AlignCenter);layout2->addWidget(toolBtn21);layout2->addWidget(toolBtn22);toolBtn31=new QToolButton;toolBtn31->setText(tr("陳熱"));toolBtn31->setIcon(QPixmap("8.png"));toolBtn31->setIconSize(QPixmap("8.png").size());toolBtn31->setAutoRaise(true);toolBtn31->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);toolBtn32=new QToolButton;toolBtn32->setText(tr("小李"));toolBtn32->setIcon(QPixmap("9.png"));toolBtn32->setIconSize(QPixmap("9.png").size());toolBtn32->setAutoRaise(true);toolBtn32->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);QGroupBox *groupBox3=new QGroupBox;//創(chuàng)建抽屜QVBoxLayout *layout3=new QVBoxLayout(groupBox3);//創(chuàng)建抽屜內(nèi)各個(gè)按鈕的布局layout3->setMargin(10);layout3->setAlignment(Qt::AlignCenter);layout3->addWidget(toolBtn31);layout3->addWidget(toolBtn32);this->addItem((QWidget*)groupBox1,tr("我的好友"));this->addItem((QWidget*)groupBox2,tr("陌生人"));this->addItem((QWidget*)groupBox3,tr("黑名單"));}文中的圖片放在build-MyQQExample-Desktop_Qt_5_9_3_MinGW_32bit-Debug目錄下即可
運(yùn)行后展現(xiàn)的效果為:
總結(jié)
- 上一篇: QT学习:基本对话框
- 下一篇: QT学习:进度条