Qt学习——聊天的QQ列表QToolBox类 .

 

QToolBox展示出来的抽屉效果可以设计联系人列表


下面展示类似聊天软件QQ的主界面

Qt学习——聊天的QQ列表QToolBox类 ._第1张图片



头文件主要就是定义这些联系人QToolButton:

view plain copy to clipboard print ?
  1. #include <QToolBox>   
  2. #include <QToolButton>   
  3. class Drawer : public QToolBox  
  4. {  
  5.     Q_OBJECT  
  6. public:  
  7.     Drawer(QWidget *parent=0,Qt::WindowFlags f=0);  
  8. private:  
  9.     QToolButton *toolBtn1_1;  
  10.     QToolButton *toolBtn1_2;  
  11.     QToolButton *toolBtn1_3;  
  12.     QToolButton *toolBtn1_4;  
  13.     QToolButton *toolBtn1_5;  
  14.     QToolButton *toolBtn2_1;  
  15.     QToolButton *toolBtn2_2;  
  16.     QToolButton *toolBtn3_1;  
  17.     QToolButton *toolBtn3_2;  
  18. };  
#include <QToolBox> #include <QToolButton> class Drawer : public QToolBox { Q_OBJECT public: Drawer(QWidget *parent=0,Qt::WindowFlags f=0); private: QToolButton *toolBtn1_1; QToolButton *toolBtn1_2; QToolButton *toolBtn1_3; QToolButton *toolBtn1_4; QToolButton *toolBtn1_5; QToolButton *toolBtn2_1; QToolButton *toolBtn2_2; QToolButton *toolBtn3_1; QToolButton *toolBtn3_2; };
源文件将联系人加入到三个QToolBox中

view plain copy to clipboard print ?
  1. #include "drawer.h"   
  2. #include <QGroupBox>   
  3. #include <QVBoxLayout>   
  4. Drawer::Drawer(QWidget *parent,Qt::WindowFlags f)  
  5.     :QToolBox(parent,0)  
  6. {  
  7.     setWindowTitle(tr("My QQ"));  
  8. //***************初始化我的好友*****************   
  9.     toolBtn1_1=new QToolButton;  
  10.     toolBtn1_1->setText(tr("张三"));  
  11.     toolBtn1_1->setIcon(QPixmap("res/11.bmp"));  
  12.     toolBtn1_1->setIconSize(QPixmap("res/11.bmp").size());  
  13.     toolBtn1_1->setAutoRaise(TRUE);  
  14.     toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  15.   
  16.     toolBtn1_2=new QToolButton;  
  17.     toolBtn1_2->setText(tr("李四"));  
  18.     toolBtn1_2->setIcon(QPixmap("res/12.bmp"));  
  19.     toolBtn1_2->setIconSize(QPixmap("res/12.bmp").size());  
  20.     toolBtn1_2->setAutoRaise(TRUE);  
  21.     toolBtn1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  22.   
  23.     toolBtn1_3=new QToolButton;  
  24.     toolBtn1_3->setText(tr("王五"));  
  25.     toolBtn1_3->setIcon(QPixmap("res/13.bmp"));  
  26.     toolBtn1_3->setIconSize(QPixmap("res/13.bmp").size());  
  27.     toolBtn1_3->setAutoRaise(TRUE);  
  28.     toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  29.   
  30.     toolBtn1_4=new QToolButton;  
  31.     toolBtn1_4->setText(tr("小赵"));  
  32.     toolBtn1_4->setIcon(QPixmap("res/14.bmp"));  
  33.     toolBtn1_4->setIconSize(QPixmap("res/14.bmp").size());  
  34.     toolBtn1_4->setAutoRaise(TRUE);  
  35.     toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  36.   
  37.     toolBtn1_5=new QToolButton;  
  38.     toolBtn1_5->setText(tr("小孙"));  
  39.     toolBtn1_5->setIcon(QPixmap("res/15.bmp"));  
  40.     toolBtn1_5->setIconSize(QPixmap("res/15.bmp").size());  
  41.     toolBtn1_5->setAutoRaise(TRUE);  
  42.     toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  43. //***************添加我的好友*****************   
  44.     QGroupBox *groupBox1=new QGroupBox;  
  45.     QVBoxLayout *layout1=new QVBoxLayout(groupBox1);  
  46.     layout1->setMargin(10);  
  47.     layout1->setAlignment(Qt::AlignHCenter);  
  48.     layout1->addWidget(toolBtn1_1);  
  49.     layout1->addWidget(toolBtn1_2);  
  50.     layout1->addWidget(toolBtn1_3);  
  51.     layout1->addWidget(toolBtn1_4);  
  52.     layout1->addWidget(toolBtn1_5);  
  53.     layout1->addStretch();  
  54. //***************陌生人*********************   
  55.     toolBtn2_1=new QToolButton;  
  56.     toolBtn2_1->setText(tr("小王"));  
  57.     toolBtn2_1->setIcon(QPixmap("res/21.bmp"));  
  58.     toolBtn2_1->setIconSize(QPixmap("res/21.bmp").size());  
  59.     toolBtn2_1->setAutoRaise(TRUE);  
  60.     toolBtn2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  61.   
  62.     toolBtn2_2=new QToolButton;  
  63.     toolBtn2_2->setText(tr("小张"));  
  64.     toolBtn2_2->setIcon(QPixmap("res/22.bmp"));  
  65.     toolBtn2_2->setIconSize(QPixmap("res/22.bmp").size());  
  66.     toolBtn2_2->setAutoRaise(TRUE);  
  67.     toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  68. //***************添加陌生人*****************   
  69.     QGroupBox *groupBox2=new QGroupBox;  
  70.     QVBoxLayout *layout2=new QVBoxLayout(groupBox2);  
  71.     layout2->setMargin(10);  
  72.     layout2->setAlignment(Qt::AlignHCenter);  
  73.     layout2->addWidget(toolBtn2_1);  
  74.     layout2->addWidget(toolBtn2_2);  
  75.     layout2->addStretch();  
  76. //***************黑名单*********************   
  77.     toolBtn3_1=new QToolButton;  
  78.     toolBtn3_1->setText(tr("小王"));  
  79.     toolBtn3_1->setIcon(QPixmap("res/31.bmp"));  
  80.     toolBtn3_1->setIconSize(QPixmap("res/31.bmp").size());  
  81.     toolBtn3_1->setAutoRaise(TRUE);  
  82.     toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  83.   
  84.     toolBtn3_2=new QToolButton;  
  85.     toolBtn3_2->setText(tr("小张"));  
  86.     toolBtn3_2->setIcon(QPixmap("res/32.bmp"));  
  87.     toolBtn3_2->setIconSize(QPixmap("res/32.bmp").size());  
  88.     toolBtn3_2->setAutoRaise(TRUE);  
  89.     toolBtn3_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);  
  90. //***************添加黑名单*****************   
  91.     QGroupBox *groupBox3=new QGroupBox;  
  92.     QVBoxLayout *layout3=new QVBoxLayout(groupBox3);  
  93.     layout3->setMargin(10);  
  94.     layout3->setAlignment(Qt::AlignHCenter);  
  95.     layout3->addWidget(toolBtn3_1);  
  96.     layout3->addWidget(toolBtn3_2);  
  97.     layout3->addStretch();  
  98. //***************添加至主窗口*****************   
  99.     this->addItem((QWidget*)groupBox1,tr("我的好友"));  
  100.     this->addItem((QWidget*)groupBox2,tr("陌生人"));  
  101.     this->addItem((QWidget*)groupBox3,tr("黑名单"));  
  102. }  
#include "drawer.h" #include <QGroupBox> #include <QVBoxLayout> Drawer::Drawer(QWidget *parent,Qt::WindowFlags f) :QToolBox(parent,0) { setWindowTitle(tr("My QQ")); //***************初始化我的好友***************** toolBtn1_1=new QToolButton; toolBtn1_1->setText(tr("张三")); toolBtn1_1->setIcon(QPixmap("res/11.bmp")); toolBtn1_1->setIconSize(QPixmap("res/11.bmp").size()); toolBtn1_1->setAutoRaise(TRUE); toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_2=new QToolButton; toolBtn1_2->setText(tr("李四")); toolBtn1_2->setIcon(QPixmap("res/12.bmp")); toolBtn1_2->setIconSize(QPixmap("res/12.bmp").size()); toolBtn1_2->setAutoRaise(TRUE); toolBtn1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_3=new QToolButton; toolBtn1_3->setText(tr("王五")); toolBtn1_3->setIcon(QPixmap("res/13.bmp")); toolBtn1_3->setIconSize(QPixmap("res/13.bmp").size()); toolBtn1_3->setAutoRaise(TRUE); toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_4=new QToolButton; toolBtn1_4->setText(tr("小赵")); toolBtn1_4->setIcon(QPixmap("res/14.bmp")); toolBtn1_4->setIconSize(QPixmap("res/14.bmp").size()); toolBtn1_4->setAutoRaise(TRUE); toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn1_5=new QToolButton; toolBtn1_5->setText(tr("小孙")); toolBtn1_5->setIcon(QPixmap("res/15.bmp")); toolBtn1_5->setIconSize(QPixmap("res/15.bmp").size()); toolBtn1_5->setAutoRaise(TRUE); toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //***************添加我的好友***************** QGroupBox *groupBox1=new QGroupBox; QVBoxLayout *layout1=new QVBoxLayout(groupBox1); layout1->setMargin(10); layout1->setAlignment(Qt::AlignHCenter); layout1->addWidget(toolBtn1_1); layout1->addWidget(toolBtn1_2); layout1->addWidget(toolBtn1_3); layout1->addWidget(toolBtn1_4); layout1->addWidget(toolBtn1_5); layout1->addStretch(); //***************陌生人********************* toolBtn2_1=new QToolButton; toolBtn2_1->setText(tr("小王")); toolBtn2_1->setIcon(QPixmap("res/21.bmp")); toolBtn2_1->setIconSize(QPixmap("res/21.bmp").size()); toolBtn2_1->setAutoRaise(TRUE); toolBtn2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn2_2=new QToolButton; toolBtn2_2->setText(tr("小张")); toolBtn2_2->setIcon(QPixmap("res/22.bmp")); toolBtn2_2->setIconSize(QPixmap("res/22.bmp").size()); toolBtn2_2->setAutoRaise(TRUE); toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //***************添加陌生人***************** QGroupBox *groupBox2=new QGroupBox; QVBoxLayout *layout2=new QVBoxLayout(groupBox2); layout2->setMargin(10); layout2->setAlignment(Qt::AlignHCenter); layout2->addWidget(toolBtn2_1); layout2->addWidget(toolBtn2_2); layout2->addStretch(); //***************黑名单********************* toolBtn3_1=new QToolButton; toolBtn3_1->setText(tr("小王")); toolBtn3_1->setIcon(QPixmap("res/31.bmp")); toolBtn3_1->setIconSize(QPixmap("res/31.bmp").size()); toolBtn3_1->setAutoRaise(TRUE); toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); toolBtn3_2=new QToolButton; toolBtn3_2->setText(tr("小张")); toolBtn3_2->setIcon(QPixmap("res/32.bmp")); toolBtn3_2->setIconSize(QPixmap("res/32.bmp").size()); toolBtn3_2->setAutoRaise(TRUE); toolBtn3_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //***************添加黑名单***************** QGroupBox *groupBox3=new QGroupBox; QVBoxLayout *layout3=new QVBoxLayout(groupBox3); layout3->setMargin(10); layout3->setAlignment(Qt::AlignHCenter); layout3->addWidget(toolBtn3_1); layout3->addWidget(toolBtn3_2); layout3->addStretch(); //***************添加至主窗口***************** this->addItem((QWidget*)groupBox1,tr("我的好友")); this->addItem((QWidget*)groupBox2,tr("陌生人")); this->addItem((QWidget*)groupBox3,tr("黑名单")); }
图片自己找,然后放到了res文件夹中

Qt学习——聊天的QQ列表QToolBox类 ._第2张图片Qt学习——聊天的QQ列表QToolBox类 ._第3张图片Qt学习——聊天的QQ列表QToolBox类 ._第4张图片

你可能感兴趣的:(qq,layout,qt,聊天)