1.Qt实现QQ界面是通过QToolBox类来实现的,基本结构是:QToolBox里面装QGroupBox,然后QGroupBox里面装QToolButton,设置好相关属性即可

2.定义类继承QToolBox

class Drawer : public QToolBox

3.在构造函数中完成布局,头文件代码展示,为了避免代码重复定义初始化函数

initToolButton
#ifndef DRAWER_H
#define DRAWER_H

#include <QWidget>
#include <QToolBox>//工具盒类,层叠窗口,抽屉效果实现
#include <QToolButton>

class Drawer : public QToolBox
{
    Q_OBJECT

public:
    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<QGroupBox>
#include <QVBoxLayout>
#include <QDebug>


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.查看运行效果图

 

QT实现类似QQ界面源码https://download.csdn.net/download/xipengbozai/16022547?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161787967916780271592065%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fdownload.%2522%257D&request_id=161787967916780271592065&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~download~first_rank_v2~rank_dl_default-1-16022547.pc_v2_rank_dl_default&utm_term=Qt%E5%AE%9E%E7%8E%B0QQ%E7%95%8C%E9%9D%A2

版权声明:本文为huipengbo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/huipengbo/p/8244802.html