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

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


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



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

#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中

#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文件夹中


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值