QT6 源(119):阅读与注释工具箱 QToolBox,先给出本类的继承关系,接着测试属性部分,再给出源码,并给出一个具体的模范使用案例

(1) 先给出本类的继承关系

在这里插入图片描述

(2)这个类的学习,很难。又感到头晕了。因为生活中的一些烦心事。我们要有强大的正确的心理素质,所以才可以然后坚持学习,学知识,把工作做好,把生活过好。不要以为安心在那里学习是正常的,这只是结果,重要的是过程。对生活里恩怨的安排,对生活中大事的安排,对生活中利益的取舍,还有持之以恒,以及信仰与对善良的坚持,才有了随后的安心与专注学习。麦穗的下面是一尺多长的麦秆,才到了开花结果的程度。

(3) 虽然老师已经讲过了。 还是先通过一个简单的类,来熟悉下这个类的使用

在这里插入图片描述

++

在这里插入图片描述

++重要的关于界面创建时 QToolBox 的使用 ui_widget . h 的内容如下,太长,给出源代码

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QToolBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QVBoxLayout *verticalLayout_3;
    QToolBox *toolBox;
    QWidget *page;
    QVBoxLayout *verticalLayout_2;
    QPushButton *pushButton;
    QLabel *label;
    QWidget *page_2;
    QVBoxLayout *verticalLayout;
    QGroupBox *groupBox;
    QGridLayout *gridLayout;
    QRadioButton *radioButtonBlack;
    QRadioButton *radioButtonRed;
    QLineEdit *lineEdit;

    void setupUi(QWidget * Widget) //形参是窗体 QWidget对象
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(292, 232);
        QFont font;
        font.setPointSize(14);
        Widget->setFont(font);                      //这是对本程序中 QWidget的设置
        Widget->setContextMenuPolicy(Qt::CustomContextMenu);
        verticalLayout_3 = new QVBoxLayout(Widget); //给窗体加个垂直布局
        verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));

        toolBox = new QToolBox(Widget);             //要加入到窗体里的工具箱 QToolBox
        toolBox->setObjectName(QString::fromUtf8("toolBox"));
        toolBox->setFrameShape(QFrame::Panel);      //这些边框的设置,是为了醒目一点
        toolBox->setFrameShadow(QFrame::Sunken);
        toolBox->setLineWidth(9);
        toolBox->setMidLineWidth(0);

        page = new QWidget();       //这些新的 QWidget是要加入到工具箱 QToolBox里的
        page->setObjectName(QString::fromUtf8("page"));
        page->setGeometry(QRect(0, 0, 262, 142));
        verticalLayout_2 = new QVBoxLayout(page);  //给 page1 设置个垂直布局
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));

        pushButton = new QPushButton(page);        //给 page1添加个按钮
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        verticalLayout_2->addWidget(pushButton);   //把 按钮加入到 page1里

        label = new QLabel(page);                  //生成标签并加入到 page1里
        label->setObjectName(QString::fromUtf8("label"));
        verticalLayout_2->addWidget(label);

        toolBox->addItem(page, QString::fromUtf8("Page 1")); //把完整的 page1加入到工具箱里

        page_2 = new QWidget();                    //要加入到工具箱里的 page2
        page_2->setObjectName(QString::fromUtf8("page_2"));
        page_2->setGeometry(QRect(0, 0, 262, 142));
        verticalLayout = new QVBoxLayout(page_2);  //page2 也采用垂直布局
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));

        groupBox = new QGroupBox(page_2);          //把两个单选按钮放入一个容器里
        groupBox->setObjectName(QString::fromUtf8("groupBox"));
        gridLayout = new QGridLayout(groupBox);    //容器采用了网格布局
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));

        radioButtonBlack = new QRadioButton(groupBox);  //生成黑色字体按钮
        radioButtonBlack->setObjectName(QString::fromUtf8("radioButtonBlack"));
        radioButtonBlack->setChecked(true);
        gridLayout->addWidget(radioButtonBlack, 0, 0, 1, 1);

        radioButtonRed = new QRadioButton(groupBox);    //生成红色字体按钮,并加入容器
        radioButtonRed->setObjectName(QString::fromUtf8("radioButtonRed"));
        gridLayout->addWidget(radioButtonRed, 0, 1, 1, 1);

        lineEdit = new QLineEdit(groupBox);        //把行文本框也加入 QGroupBox
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        gridLayout->addWidget(lineEdit, 1, 0, 1, 2);

        verticalLayout->addWidget(groupBox);       //把制作好的 QGroupBox加入 page2

        toolBox->addItem(page_2, QString::fromUtf8("Page 2")); //把 page2窗体加入工具箱

        verticalLayout_3->addWidget(toolBox);      //把工具箱加入大窗体中

        retranslateUi(Widget);

        toolBox->setCurrentIndex(1);               //让工具箱默认显示第一个页面

        QMetaObject::connectSlotsByName(Widget);   //允许通过函数名来连接信号与槽
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget    ->setWindowTitle(QCoreApplication::translate("Widget", "H", nullptr));
        groupBox  ->setTitle      (QString());

        pushButton      ->setText(QCoreApplication::translate("Widget",
                                                        "PushButton", nullptr));
        label           ->setText(QCoreApplication::translate("Widget",
                                                        "TextLabel" , nullptr));

        radioButtonBlack->setText(QCoreApplication::translate("Widget",
                                "\351\273\221", nullptr));
        radioButtonRed  ->setText(QCoreApplication::translate("Widget",
                                "\347\272\242", nullptr));
        lineEdit        ->setText(QCoreApplication::translate("Widget",
                                "\345\223\210\345\223\210\345\223\210", nullptr));

        toolBox ->setItemText(toolBox->indexOf(page),
                            QCoreApplication::translate("Widget", "Page 1", nullptr));
        toolBox ->setItemText(toolBox->indexOf(page_2),
                            QCoreApplication::translate("Widget", "Page 2", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

(4)给个属性综述

在这里插入图片描述

++

在这里插入图片描述

(5)总之,本工具箱的编程作用不大,其功能都由 UIC 自动实现了,里面的组件的编程更重要。但了解一下其源码也是挺好的。
本源码来自于头文件 qtoolbox . h

#ifndef QTOOLBOX_H
#define QTOOLBOX_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qframe.h>
#include <QtGui/qicon.h>

QT_REQUIRE_CONFIG(toolbox);

QT_BEGIN_NAMESPACE

class QToolBoxPrivate;

/*
The QToolBox class provides a column of tabbed widget items.

工具栏是一个小部件,它显示一列一个接一个的选项卡,当前项显示在当前选项卡下方。
每个选项卡都在选项卡列中具有索引位置。选项卡的项是一个QWidget。
每个项目都有一个itemText ()方法,一个可选的itemlcon()方法,
一个可选的itemToolTip()方法和一个widget ()方法。

项目的属性可以通过setltemText()、setltemlcon()和setltemToolTip()进行更改。
每个项目都可以通过setltemEnabled()单独启用或禁用。
使用addtem()添加项目,或使用insertltem()在特定位置插入项目。
使用count()函数可以获取项目的总数。
可以使用delete删除项目,或使用removeltem()从工具箱中移除项目.
使用removeltem()和insertltem()的组合可以将项目移动到不同的位置。
当前项目小部件的索引由 currentlndex()返回,并使用 setCurrentndex()设置。
特定项目的索引可以通过 indexOf()找到,给定索引处的项目由item()返回。
当当前项目更改时,会发出currentChanged()信号。


*/

class Q_WIDGETS_EXPORT QToolBox : public QFrame
{
    Q_OBJECT

    //This property holds the number of items contained in the toolbox.
    //By default, this property has a value of 0.
    Q_PROPERTY(int count READ count)

    Q_PROPERTY(int         currentIndex
                READ       currentIndex        WRITE     setCurrentIndex
                NOTIFY     currentChanged)
    //This property holds the index of the current item.
    //By default, for an empty toolbox, this property has a value of -1.

private:
    Q_DECLARE_PRIVATE(QToolBox)
    Q_DISABLE_COPY(QToolBox)
    Q_PRIVATE_SLOT(d_func(), void _q_buttonClicked())
    Q_PRIVATE_SLOT(d_func(), void _q_widgetDestroyed(QObject*))

public:
    //Constructs a new toolbox with the given parent and the flags, f.
    explicit QToolBox(QWidget * parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    //WindowFlags = QFlags<WindowType>
    //enum Qt::WindowType { Widget, Window, Dialog, Popup, ToolTip, ...};

    ~QToolBox();


//   Q_PROPERTY(int count   READ   count)
                int count() const;

//   Q_PROPERTY(int        currentIndex
//              READ       currentIndex        WRITE     setCurrentIndex
//              NOTIFY     currentChanged)
                int        currentIndex() const;
public Q_SLOTS:
                void    setCurrentIndex  (int index);
Q_SIGNALS:
                void       currentChanged(int index);

public :
    int insertItem(int index, QWidget * item,                     const QString & text)
    { return insertItem(index, item, QIcon(), text); }       //这里的 Item其实是 QWidget窗体

    int insertItem(int index, QWidget * item, const QIcon & icon, const QString & text);
    //Inserts the widget at position index, or at the bottom of the toolbox,
    //  if index is out of range. The new item's text is set to text,    //插入窗体
    //and the icon is displayed to the left of the text. Returns the new item's index.

    inline int addItem(QWidget * item, const QString & text) //追加窗体
    { return insertItem(-1, item, QIcon(), text); }

    inline int addItem(QWidget * item, const QIcon   & iconSet,   const QString & text)
    { return insertItem(-1, item, iconSet, text); }
    //Adds the widget in a new tab at bottom of the toolbox.
    //The new tab's text is set to text,
    //  and the iconSet is displayed to the left of the text. Returns the new tab's index.

    void removeItem(int index); //把窗体组件从 QToolBox里删除,但并没有从内存上删除。
    //Removes the item at position index from the toolbox.
    //Note that the widget is not deleted.

    //Returns true if the item at position index is enabled; otherwise returns false.
    bool      isItemEnabled(int index) const;
    void     setItemEnabled(int index, bool enabled);
    //If enabled is true then the item at position index is enabled;
    //  otherwise the item at position index is disabled.

    //Returns the text of the item at position index,
    //  or an empty string if index is out of range.
    QString     itemText(int index) const;
    void     setItemText(int index, const QString & text); //可以为工具箱里的条目定义快捷键
    //Sets the text of the item at position index to text.
    //后面的字符,如果提供的文本包含逗号字符('&'),则会自动为其创建一个记事本。“&”将用作快捷键。
    //任何以前的记事语都将被覆盖,如果文本中没有定义记事语,则将被清除。
    //有关详细信息,请参阅QShortcut文档(要显示实际的逗号,请使用“&&。

    //Returns the icon of the item at position index,
    //  or a null icon if index is out of range.
    QIcon       itemIcon(int index)    const;
    void     setItemIcon(int index,    const QIcon   & icon   );
    //Sets the icon of the item at position index to icon.

    //Returns the tooltip of the item at position index,
    //  or an empty string if index is out of range.
    QString     itemToolTip(int index) const;
    void     setItemToolTip(int index, const QString & toolTip);
    //Sets the tooltip of the item at position index to toolTip.

    //Returns a pointer to the current widget, or nullptr if there is no such item.
    QWidget * currentWidget() const;
    QWidget *        widget(int index) const;
    //Returns the widget at position index, or nullptr if there is no such item.
    int indexOf(const QWidget * widget) const;
    //Returns the index of widget, or -1 if the item does not exist.

public Q_SLOTS:
    //void setCurrentIndex(int index); //这是一个成员函数

    void setCurrentWidget(QWidget * widget);
    //Makeswidget the current widget. The widget must be an item in this tool box.

Q_SIGNALS:
    //void currentChanged(int index);
    //This signal is emitted when the current item is changed.
    //The new current item's index is passed in index, or -1 if there is no current item.

protected:
    //This virtual handler is called after a new item was added or inserted at position index.
    virtual void itemInserted(int index);
    virtual void itemRemoved (int index);
    //This virtual handler is called after an item was removed from position index.

    bool       event(QEvent     * e) override;
    void   showEvent(QShowEvent * e) override;
    void changeEvent(QEvent     *  ) override;

}; //完结 class QToolBox : public QFrame


QT_END_NAMESPACE

#endif // QTOOLBOX_H

(6)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值