Qt之QToolBox

简述

QToolBox类提供了一个列(选项卡式的)部件条目。

QToolBox可以在一个tab列上显示另外一个,并且当前的item显示在当前的tab下面。每个tab都在tab列中有一个索引位置。tab的item是一个QWidget 。

  • 简述
  • 详细描述
  • 使用
    • 效果
    • 源码

详细描述

每个item都有一个itemText()、一个可选的itemIcon()、一个可选的itemToolTip()、和一个widget()函数 。item的属性可以通过setItemText()、setItemIcon()、和setItemToolTip()来改变,并且每个item可以通过setItemEnabled()单独设置为是否可用。

Item的添加使用addItem(),或通过insertItem()在特定位置插入。如果要获取items的总数,可以调用count()函数。Item可以使用removeItem()从toolbox中删除。结合removeItem()和insertItem(),允许你将item移动到不同的位置。

当前item部件的索引由currentIndex()返回,并使用setCurrentIndex()来设置。一个特定item的索引可以使用indexOf()来获取,item()则返回给定索引的item。

当前的item发生变化时,会发射currentChanged()信号。

使用

玩穿越,谁不会呀!O(∩_∩)O哈哈~

梦回经典,创建一个金庸老先生的武侠传。主要分为三组:大美女、大英雄、大恶人。

人物属性:会员、名字、图像、个人说明。

古代人其实也蛮拼的,办会员,好让别人更了解TA、关注TA。。。

效果

源码

#include <QToolBox>
#include <QGroupBox>
#include <QLabel>
#include <QHBoxLayout>

class ToolBox : public QToolBox
{
    Q_OBJECT

public:
    ToolBox(QWidget *parent = 0)
        : QToolBox(parent)
    {
        initUI();

        // 连接信号槽
        connect(this, &QToolBox::currentChanged, this, &ToolBox::onCurrentChanged);
    }

private:
    void initUI() {
        struct User {
            bool bVIP;  // 会员
            QString strName;  // 名字
            QString strIcon;  // 图像
            QString strDesc;  // 个人说明
        } user[3][5] =
        {
            {
                {true, QStringLiteral("香香公主"), ":/QQ/1", QStringLiteral("金庸笔下的第一美女")},
                {true, QStringLiteral("小龙女"), ":/QQ/2", QStringLiteral("玉女心经")},
                {true, QStringLiteral("王语嫣"), ":/QQ/3", QStringLiteral("熟读各派武学秘笈")},
                {false, QStringLiteral("赵敏"), ":/QQ/4", QStringLiteral("大元第一美人")},
                {false, QStringLiteral("周芷若"), ":/QQ/5", QStringLiteral("光复汉家河山,光大峨嵋")}
            },

            {
                {true, QStringLiteral("萧峰"), ":/QQ/6", QStringLiteral("丐帮帮主 - 智勇双全、胆略过人、豪迈飒爽")},
                {true, QStringLiteral("令狐冲"), ":/QQ/8", QStringLiteral("独孤九剑")},
                {true, QStringLiteral("杨过"), ":/QQ/9", QStringLiteral("黯然销魂掌")},
                {false, QStringLiteral("郭靖"), ":/QQ/7", QStringLiteral("降龙十八掌")},
                {false, QStringLiteral("胡一刀"), ":/QQ/10", QStringLiteral("辽东大侠")}
            },

            {
                {true, QStringLiteral("金轮法王"), ":/QQ/11", QStringLiteral("龙象般若功")},
                {true, QStringLiteral("丁春秋"), ":/QQ/13", QStringLiteral("星宿老仙,法力无边")},
                {false, QStringLiteral("裘千仞"), ":/QQ/12", QStringLiteral("铁掌水上飘")},
                {false, QStringLiteral("成昆"), ":/QQ/14", QStringLiteral("混元霹雳手")},
                {false, QStringLiteral("李莫愁"), ":/QQ/15", QStringLiteral("冰魄银针,杀人如麻")}
            }
        };

        QStringList groupList;
        groupList << QStringLiteral("大美女") << QStringLiteral("大英雄") << QStringLiteral("大恶人");

        // 初始化列表
        for (int i = 0; i < sizeof(user)/sizeof(user[0]); i++)
        {
            QGroupBox *pGroupBox = new QGroupBox(this);
            QVBoxLayout *pLayout = new QVBoxLayout(pGroupBox);

            // 添加好友
            for (int j = 0; j < sizeof(user[0])/sizeof(user[0][0]); ++j)
            {
                QWidget *pWidget = initWidget(user[i][j].bVIP, user[i][j].strName,
                                              user[i][j].strIcon, user[i][j].strDesc);
                pLayout->addWidget(pWidget);
            }
            pLayout->addStretch();
            pLayout->setSpacing(10);
            pLayout->setContentsMargins(10, 10, 10, 10);

            // 添加分组
            addItem(pGroupBox, groupList.at(i));
        }
    }

    QWidget* initWidget(bool bVIP, const QString &name,
                        const QString &icon, const QString &desc = "") {
        QWidget *pWidget = new QWidget(this);
        QLabel *pPixmapLabel = new QLabel(this);
        QLabel *pNameLabel = new QLabel(this);
        QLabel *pDescLabel = new QLabel(this);

        // 图像 名称 描述
        pPixmapLabel->setPixmap(QPixmap(icon));
        pNameLabel->setText(name);
        pDescLabel->setText(desc);

        // VIP名字为红色;否则,白色
        pNameLabel->setStyleSheet(QString("color: %1;").arg(bVIP ? "rgb(240, 30, 40)" : "white"));
        pDescLabel->setStyleSheet("color: gray;");

        QVBoxLayout *pVLayout = new QVBoxLayout();
        pVLayout->addStretch();
        pVLayout->addWidget(pNameLabel);
        pVLayout->addWidget(pDescLabel);
        pVLayout->addStretch();
        pVLayout->setSpacing(5);
        pVLayout->setContentsMargins(0, 0, 0, 0);

        QHBoxLayout *pHLayout = new QHBoxLayout();
        pHLayout->addWidget(pPixmapLabel);
        pHLayout->addLayout(pVLayout);
        pHLayout->addStretch();
        pHLayout->setContentsMargins(0, 0, 0, 0);

        pWidget->setLayout(pHLayout);

        return pWidget;
    }

private slots:
    void onCurrentChanged(int index) {
        QString strGroup = itemText(index);
        qDebug() << strGroup;
    }
};

这里,我们建立了一个3行5列的二维数组,用来存储人物信息。

对于type array[A][B]形式的二维数组,可以通过计算sizeof获取行、列数。其中:

  • sizeof(array[0][0]):为一个元素占用的空间,
  • sizeof(array[0]):为一行元素占用的空间,
  • sizeof(array):为整个数组占用的空间,

那么,很容易计算出行、列数:

  • 行数 = sizeof(array)/sizeof(array[0]);
  • 列数 = sizeof(array[0])/sizeof(array[0][0]);

为了创建人物信息,我们定义了一个initWidget()函数,主要实现是创建了三个标签,分别用来显示图像、名字、个人说明。

创建完人物之后,再将他们添加至QGroupBox分组中。最后,通过addItem()添加所有的分组。这样,我们就可以和他们聊天啦^_^。

时间: 2024-08-30 13:21:22

Qt之QToolBox的相关文章

【QT】qt中用QToolBox实现qq抽屉效果时,如果好友分组数超过界面,怎么加滚动条QAQ

问题描述 [QT]qt中用QToolBox实现qq抽屉效果时,如果好友分组数超过界面,怎么加滚动条QAQ 问题是这样的,首先我的窗口是固定大小的.发现在QToolBox中添加item时如果数目过大 就会挤压已经存在的item,就是所有的item变小.我想问问能不能设置滚动条来滚动而不是 让item变小,以及一直没有找到让item固定大小的方法QAQ,item的大小不知道怎么控 制.以及纠结好几天了,跪谢大家,新年快乐~(≧▽≦)/~ 解决方案 http://www.cnblogs.com/rol

QT中QToolBox的使用,实现抽屉效果

drawer.h #ifndef DRAWER_H #define DRAWER_H #include <QtGui> #include "ui_drawer.h" class drawer : public QToolBox { Q_OBJECT public: drawer( QWidget *parent=0, Qt::WindowFlags f=0 ); ~drawer(); QToolButton *toolButton1_1; QToolButton *tool

《Qt 实战一二三》

简介 "我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流",不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的状态,来开始每一天的点滴分享,我们是一个有激情,有态度的部队. 但是我们还是我们,我们只是多了一份责任.古语有云:"不积跬步无以至千里,不积小流无以成江海",所以每一个伟大事务的产生都不是一蹴而就的.现在我们要立足眼下,把第一站放在地球,"<Qt 实战一二三>&quo

【C/C++学院】(17)QT标准对话框/toolbox类

1.标准对话框 各种基本对话框通过调用格子不同的静态函数来完成其功能: #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPushButton> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QPushButton *btn1; private slots:

Qt之QSS(QDarkStyleSheet)

简述 关于样式,前面介绍了很多内容,下面分享一个深色样式表,很值得借鉴! 简述 效果 QSS 更多参考 效果 QSS /* * The MIT License (MIT) * * Copyright (c) <2013-2014> <Colin Duquesnoy> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and assoc

Qt Style Sheets Reference

  Qt Style Sheets Reference Qt Style Sheets support various properties, pseudo-states, and subcontrols that make it possible to customize the look of widgets.   List of Stylable Widgets The following table lists the Qt widgets that can be customized

QT中的QInputDialog的小例子

其实这断代码没什么优秀之处,贴出来主要为为了方便自己和他人,因为以后如果用到这一块的话,这些代码可能能够直接拿过来用. InpugDialog.h头文件: #ifndef INPUGDIALOG_H #define INPUGDIALOG_H #include <QtGui> #include "ui_inpugdialog.h" class InpugDialog : public QDialog { Q_OBJECT public: InpugDialog(QWidge

vs2008-VS搭配QT,x64平台下为挖三维编的dll,怎么调试

问题描述 VS搭配QT,x64平台下为挖三维编的dll,怎么调试 VS搭配QT,x64平台下为挖三维编的dll,怎么调试?新手,试过用挖三维打开调试,但是没看到界面在哪里,请懂得QT的前辈回答,谢谢! 解决方案 https://github.com/Vaa3D/Vaa3D_Wiki/wiki/Build-Vaa3D-on-Linux

qt如何实现 滑动屏幕的效果

问题描述 qt如何实现 滑动屏幕的效果 用了qtscrollArea做了只能靠移动滑动条来实现滑动的效果,怎么样才能做到用触摸屏实现滑动效果,而不是靠滑动条.求大神指点. 解决方案 不要依赖于 qtscrollArea,自己做滑动效果,这样就可以不依靠滑动条.