简易qq对话框

//本程序由QT5 creator编译可运行
//dialog.h
1 #ifndef DIALOG_H
#define DIALOG_H #include <QDialog> class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(QWidget *parent = );
~Dialog();
}; #endif // DIALOG_H
//drawer.h
1 #ifndef DRAWER_H
#define DRAWER_H
#include <QToolBox>
#include <QToolButton> class Drawer : public QToolBox
{
Q_OBJECT
public:
Drawer(QWidget *parent = , Qt::WindowFlags f = );
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; }; #endif // DRAWER_H
//dialog.cpp
1 #include "dialog.h" Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
} Dialog::~Dialog()
{ }
//drawer.cpp
//代码中,图片与build-MyQQExample-Desktop_Qt_5_6_0_MinGW_32bit-Debug放置同一个文件夹下面
1 #include "drawer.h"
#include <QGroupBox>
#include <QVBoxLayout> Drawer::Drawer(QWidget *parent, Qt::WindowFlags f)
:QToolBox(parent, f)
{
setWindowTitle (tr("My QQ")); //设置主窗体的标题
//创建一个QToolButton类实例,分别对应于抽屉中的每一个按钮
toolBtn1_1 = new QToolButton;
toolBtn1_1->setText (tr("张三")); //设置按钮的文字
toolBtn1_1->setIcon (QPixmap("11.png")); //设置按钮的图标
toolBtn1_1->setIconSize (QPixmap("11.png").size ());
toolBtn1_1->setAutoRaise (true); //当鼠标离开时,按钮自动恢复为弹起状态
//设置按钮的ToolButtonStyle属性->主要用来描述按钮的文字和图标的显示方式.
//Qt::ToolButtonTextBesideIcon-->文字在图标旁边
toolBtn1_1->setToolButtonStyle (Qt::ToolButtonTextBesideIcon);
toolBtn1_2 = new QToolButton;
toolBtn1_2->setText (tr("李四"));
toolBtn1_2->setIcon (QPixmap("12.png"));
toolBtn1_2->setIconSize (QPixmap("12.png").size ());
toolBtn1_2->setAutoRaise (true);
toolBtn1_2->setToolButtonStyle (Qt::ToolButtonTextBesideIcon);
toolBtn1_3 = new QToolButton;
toolBtn1_3->setText (tr("王五"));
toolBtn1_3->setIcon (QPixmap("13.png"));
toolBtn1_3->setIconSize (QPixmap("13.png").size ());
toolBtn1_3->setAutoRaise (true);
toolBtn1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn1_4 = new QToolButton;
toolBtn1_4->setText(tr("小赵"));
toolBtn1_4->setIcon(QPixmap("14.png"));
toolBtn1_4->setIconSize(QPixmap("14.png").size());
toolBtn1_4->setAutoRaise(true);
toolBtn1_4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn1_5 = new QToolButton;
toolBtn1_5->setText(tr("小孙"));
toolBtn1_5->setIcon(QPixmap("155.png"));
toolBtn1_5->setIconSize(QPixmap("155.png").size());
toolBtn1_5->setAutoRaise(true);
toolBtn1_5->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
//创建一个QGroupBox类实例,在本例中对应每一个抽屉.
//QGroupBox *groupBox2 = new QGroupBox。。创建其余两栏抽屉
QGroupBox *groupBox1 = new QGroupBox;
//布局
//用来设置抽屉内各个按钮的布局
QVBoxLayout *layout1 = new QVBoxLayout(groupBox1);
//布局中各个窗体显示间距
layout1->setMargin ();
//布局中各个窗体显示位置
layout1->setAlignment (Qt::AlignLeft);
//加入抽屉内的各个按钮
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 ("老王");
toolBtn2_1->setIcon (QPixmap("21.png"));
toolBtn2_1->setIconSize (QPixmap("21.png").size ());
toolBtn2_1->setAutoRaise (true);
toolBtn2_1->setToolButtonStyle (Qt::ToolButtonTextBesideIcon);
toolBtn2_2 = new QToolButton;
toolBtn2_2->setText (tr("小张"));
toolBtn2_2->setIcon (QPixmap("22.png"));
toolBtn2_2->setIconSize (QPixmap("22.png").size ());
toolBtn2_2->setAutoRaise(true);
toolBtn2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox2 = new QGroupBox;
QVBoxLayout *layout2 = new QVBoxLayout(groupBox2);
layout2->setMargin ();
layout2->setAlignment (Qt::AlignLeft);
layout2->addWidget (toolBtn2_1);
layout2->addWidget (toolBtn2_2);
toolBtn3_1 =new QToolButton;
toolBtn3_1->setText(tr("小陈"));
toolBtn3_1->setIcon(QPixmap("31.png"));
toolBtn3_1->setIconSize(QPixmap("31.png").size());
toolBtn3_1->setAutoRaise(true);
toolBtn3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn3_2 =new QToolButton;
toolBtn3_2->setText(tr("小李"));
toolBtn3_2->setIcon(QPixmap("32.png"));
toolBtn3_2->setIconSize(QPixmap("32.png").size());
toolBtn3_2->setAutoRaise(true);
toolBtn3_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox3=new QGroupBox;
QVBoxLayout *layout3=new QVBoxLayout(groupBox3);
layout3->setMargin();
layout3->setAlignment(Qt::AlignLeft);
layout3->addWidget(toolBtn3_1);
layout3->addWidget(toolBtn3_2);
//将准备好的抽屉插入ToolBox中
this->addItem ((QWidget*)groupBox1, tr("我的好友"));
this->addItem ((QWidget*)groupBox2, tr("陌生人"));
this->addItem ((QWidget*)groupBox3, tr("黑名单"));
}
//main.cpp
1 #include "dialog.h"
#include <QApplication>
#include "drawer.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv); Drawer drawer;
drawer.show ();
return a.exec();
}

简易qq对话框运行效果,如图所示.

上一篇:codevs 1033 蚯蚓的游戏问题


下一篇:vb 和vb.net的区别