其他分享
首页 > 其他分享> > Qt4——精彩实例分析6

Qt4——精彩实例分析6

作者:互联网

.h

#ifndef DRAWER_H
#define DRAWER_H
#include <QToolBox>
#include <QToolButton>

//QToolBox 提供一种列装的层叠窗体
//QToolButton提供一种快速访问命令或选择项
class Drawer:public QToolBox
{
    Q_OBJECT
public:
    Drawer();

	//有些没用到
    QToolButton *toolbutton1_1;
    QToolButton *toolbutton1_2;
    QToolButton *toolbutton1_3;
    QToolButton *toolbutton1_4;
    QToolButton *toolbutton1_5;
    QToolButton *toolbutton2_1;
    QToolButton *toolbutton2_2;
    QToolButton *toolbutton3_1;
    QToolButton *toolbutton3_2;

};
#endif // DRAWER_H

.c

#include"drawer.h"
#include <QWidget>
#include <QGroupBox>
#include <QSize>
#include <QIcon>
#include <QPixmap>
#include <QVBoxLayout>
Drawer::Drawer()
{
    setWindowTitle("MY qq");
    QGroupBox *groupBox1 = new QGroupBox;   //创建一个对象
    //工具按钮是一种特殊的按钮,用于快速访问特定命令或选项。
    //与普通命令按钮相反,工具按钮通常不显示文本标签,而是显示图标。
    toolbutton1_1 = new QToolButton;        //创建一个工具按钮
    //对按钮的文字图标以及图标大小进行设置
    toolbutton1_1->setText("张三");
    toolbutton1_1->setIcon(QPixmap(":/new/prefix1/images/aa.png"));//添加图片路径
    toolbutton1_1->setIconSize(QPixmap(":/new/prefix1/images/aa.png").size());
    //设置鼠标离开按钮自动弹起
    toolbutton1_1->setAutoRaise(true);
    //文字显示在图标下面
    /*
        ToolButtonIconOnly, 只显示图标
        ToolButtonTextOnly, 只显示文字
        ToolButtonTextBesideIcon,文字显示在图标旁边
        ToolButtonTextUnderIcon,文字显示在图标下边
        ToolButtonFollowStyle
    */
    toolbutton1_1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    toolbutton1_2 = new QToolButton;
    toolbutton1_2->setText("李四");
    toolbutton1_2->setIcon(QPixmap(":/new/prefix1/images/bb.png"));
    toolbutton1_2->setIconSize(QPixmap(":/new/prefix1/images/bb.png").size());
    toolbutton1_2->setAutoRaise(true);
    toolbutton1_2->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    QGroupBox *groupBox2 = new QGroupBox;
    toolbutton2_1 = new QToolButton;
    toolbutton2_1->setText("王五");
    toolbutton2_1->setIcon(QPixmap(":/new/prefix1/images/cc.png"));//添加图片路径
    toolbutton2_1->setIconSize(QPixmap(":/new/prefix1/images/cc.png").size());
    toolbutton2_1->setAutoRaise(true);
    toolbutton2_1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);


    QVBoxLayout *layout = new QVBoxLayout(groupBox1);//创建QVBoxLayout实例用来设置抽屉内按钮布局
    layout->setMargin(10);  //设置窗体显示间距
    layout->setAlignment(Qt::AlignHCenter);//设置对齐方式
    layout->addWidget(toolbutton1_1);//将抽屉内按钮加入
    layout->addWidget(toolbutton1_2);
    layout->addStretch();//

    QVBoxLayout *layout2 = new QVBoxLayout(groupBox2);
    layout2->setMargin(10);
    layout2->setAlignment(Qt::AlignHCenter);
    layout2->addWidget(toolbutton2_1);
    layout2->addStretch();//在按钮之后加入占位符,使得所有按钮都能向上对齐,抽屉大小发生改变时按钮大小不变

    this->addItem((QWidget*)groupBox1,"My Friends");//把准备好的抽屉匣插入ToolBox
    this->addItem((QWidget*)groupBox2,"My student");
}

main.c

#include "mainwindow.h"
#include <QApplication>
#include "drawer.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Drawer b;
    b.show();

    return a.exec();
}

资源添加

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

标签:Qt4,toolbutton2,QToolButton,精彩,实例,按钮,toolbutton1,new,include
来源: https://blog.csdn.net/KK_546464/article/details/123215756