工具盒--ToolBox drawer.h drawer.cpp main.cpp 运行图
drawer.h
# ifndef DRAWER_H
# define DRAWER_H
# include <QWidget>
# include <QToolBox>
# include <QToolButton>
# include <QGroupBox>
# include <QVBoxLayout> class Drawer : public QToolBox
{ Q_OBJECT
public : Drawer ( QWidget* parent = nullptr , Qt:: WindowFlags f = Qt :: WindowFlags ( ) ) ; private : QToolButton* ToolBtn1_1; QToolButton* ToolBtn1_2; QToolButton* ToolBtn1_3; QToolButton* ToolBtn2_1; QToolButton* ToolBtn2_2; QToolButton* ToolBtn2_3;
} ; # endif
drawer.cpp
# include "drawer.h" Drawer :: Drawer ( QWidget* parent, Qt:: WindowFlags f) : QToolBox ( parent, f)
{ setWindowTitle ( tr ( "Test QQ" ) ) ; ToolBtn1_1 = new QToolButton; ToolBtn1_1-> setText ( tr ( "张1" ) ) ; ToolBtn1_1-> setIcon ( QPixmap ( "312.ico" ) ) ; ToolBtn1_1-> setIconSize ( QPixmap ( "312.ico" ) . size ( ) ) ; ToolBtn1_1-> setAutoRaise ( true ) ; ToolBtn1_1-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; ToolBtn1_2 = new QToolButton; ToolBtn1_2-> setText ( tr ( "张2" ) ) ; ToolBtn1_2-> setIcon ( QPixmap ( "312.png" ) ) ; ToolBtn1_2-> setIconSize ( QPixmap ( "312.png" ) . size ( ) ) ; ToolBtn1_2-> setAutoRaise ( true ) ; ToolBtn1_2-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; ToolBtn1_3 = new QToolButton; ToolBtn1_3-> setText ( tr ( "张3" ) ) ; ToolBtn1_3-> setIcon ( QPixmap ( "312.png" ) ) ; ToolBtn1_3-> setIconSize ( QPixmap ( "312.png" ) . size ( ) ) ; ToolBtn1_3-> setAutoRaise ( true ) ; ToolBtn1_3-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; QGroupBox* GroupBox1 = new QGroupBox; QVBoxLayout* Layout1 = new QVBoxLayout ( GroupBox1) ; Layout1-> setMargin ( 2 ) ; Layout1-> setAlignment ( Qt:: AlignLeft) ; Layout1-> addWidget ( ToolBtn1_1) ; Layout1-> addWidget ( ToolBtn1_2) ; Layout1-> addWidget ( ToolBtn1_3) ; Layout1-> addStretch ( ) ; ToolBtn2_1 = new QToolButton; ToolBtn2_1-> setText ( tr ( "张6" ) ) ; ToolBtn2_1-> setIcon ( QPixmap ( "312.png" ) ) ; ToolBtn2_1-> setIconSize ( QPixmap ( "312.png" ) . size ( ) ) ; ToolBtn2_1-> setAutoRaise ( true ) ; ToolBtn2_1-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; ToolBtn2_2 = new QToolButton; ToolBtn2_2-> setText ( tr ( "张7" ) ) ; ToolBtn2_2-> setIcon ( QPixmap ( "312.png" ) ) ; ToolBtn2_2-> setIconSize ( QPixmap ( "312.png" ) . size ( ) ) ; ToolBtn2_2-> setAutoRaise ( true ) ; ToolBtn2_2-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; ToolBtn2_3 = new QToolButton; ToolBtn2_3-> setText ( tr ( "张8" ) ) ; ToolBtn2_3-> setIcon ( QPixmap ( "312.png" ) ) ; ToolBtn2_3-> setIconSize ( QPixmap ( "312.png" ) . size ( ) ) ; ToolBtn2_3-> setAutoRaise ( true ) ; ToolBtn2_3-> setToolButtonStyle ( Qt:: ToolButtonTextBesideIcon) ; QGroupBox* GroupBox2 = new QGroupBox; QVBoxLayout* Layout2 = new QVBoxLayout ( GroupBox2) ; Layout2-> setMargin ( 2 ) ; Layout2-> setAlignment ( Qt:: AlignHCenter) ; Layout2-> addWidget ( ToolBtn2_1) ; Layout2-> addWidget ( ToolBtn2_2) ; Layout2-> addWidget ( ToolBtn2_3) ; this -> addItem ( ( QWidget* ) GroupBox1, tr ( "测试1" ) ) ; this -> addItem ( ( QWidget* ) GroupBox2, tr ( " 测试2" ) ) ; this -> setItemIcon ( 0 , QPixmap ( "312.ico" ) ) ; }
main.cpp
# include "myqqexample.h"
# include "drawer.h"
# include <QApplication> int main ( int argc, char * argv[ ] )
{ QApplication a ( argc, argv) ; Drawer drawer; drawer. show ( ) ; return a. exec ( ) ;
}
运行图