1.Qt实现QQ界面是通过QToolBox类来实现的,基本结构是:QToolBox里面装QGroupBox,然后QGroupBox里面装QToolButton,设置好相关属性即可
2.定义类继承QToolBox
class Drawer : public QToolBox
3.在构造函数中完成布局,头文件代码展示,为了避免代码重复定义初始化函数
initToolButton
#ifndef DRAWER_H#define DRAWER_H#include#include //工具盒类,层叠窗口,抽屉效果实现#include class Drawer : public QToolBox{ Q_OBJECTpublic: Drawer(QWidget *parent=0,Qt::WindowFlags f = 0); ~Drawer(); void initToolButton(QToolButton **tb,QString name,QString pathpic);private : QToolButton *toolbtn1; QToolButton *toolbtn2; QToolButton *toolbtn3; QToolButton *toolbtn4; QToolButton *toolbtn5; QToolButton *toolbtn11; QToolButton *toolbtn12; QToolButton *toolbtn21; QToolButton *toolbtn22;};#endif // DRAWER_H
4.主要代码为.cpp文件
#include "drawer.h"#include#include #include Drawer::Drawer(QWidget *parent, Qt::WindowFlags f):QToolBox(parent,f){ setWindowTitle("QQ"); initToolButton(&toolbtn1,"张远山","1.png"); qDebug()<<"ok"; initToolButton(&toolbtn2,"东奥","2.png"); qDebug()<<"ok"; initToolButton(&toolbtn3,"孙选成","3.png"); qDebug()<<"ok"; initToolButton(&toolbtn4,"司宽","4.png"); qDebug()<<"ok"; initToolButton(&toolbtn5,"张宇","5.png"); qDebug()<<"ok"; QGroupBox *gb1 = new QGroupBox; QVBoxLayout *lay1 = new QVBoxLayout(gb1); lay1->setMargin(10); lay1->setAlignment(Qt::AlignHCenter); lay1->addWidget(toolbtn1); lay1->addWidget(toolbtn2); lay1->addWidget(toolbtn3); lay1->addWidget(toolbtn4); lay1->addWidget(toolbtn5); // lay1->addStretch(); initToolButton(&toolbtn11,"刘翔","11.png"); initToolButton(&toolbtn12,"詹姆斯","12.png"); QGroupBox *gb2 = new QGroupBox; QVBoxLayout *lay2 = new QVBoxLayout(gb2); lay2->addWidget(toolbtn11); lay2->addWidget(toolbtn12); lay2->setMargin(10); lay2->setAlignment(Qt::AlignHCenter); initToolButton(&toolbtn21,"惠子","21.png"); initToolButton(&toolbtn22,"名人","22.png"); QGroupBox *gb3 = new QGroupBox; QVBoxLayout *lay3 = new QVBoxLayout(gb3); lay3->addWidget(toolbtn21); lay3->addWidget(toolbtn22); lay3->setMargin(10); lay3->setAlignment(Qt::AlignHCenter); //准备好的抽屉插入TOOlBox中 this->addItem(gb1,"我的好友"); this->addItem(gb2,"陌生人"); this->addItem(gb3,"黑名单");}Drawer::~Drawer(){}void Drawer::initToolButton(QToolButton **tb, QString name, QString pathpic){ (*tb) = new QToolButton; (*tb)->setText(name); (*tb)->setIcon(QPixmap(pathpic)); (*tb)->setIconSize(QSize(20,20)); (*tb)->setAutoRaise(true); (*tb)->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);}
5.
(*tb)->setIcon(QPixmap(pathpic));表示设置按钮图标
6.
(*tb)->setAutoRaise(true);表示鼠标离开时按钮自动恢复为弹起状态
7.
(*tb)->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);表示设置按钮文字标签在图标旁边
8.查看运行效果图
posted on 2018-01-08 20:43 阅读( ...) 评论( ...)