在工具栏中嵌入其他控件

在工具栏中嵌入其他控件

如何在工具栏中嵌入其他控件?

1、新建toolbar.h

#ifndef TOOLBAR_H
#define TOOLBAR_H
#include <QtGui>

class ToolBar : public QMainWindow
{
    Q_OBJECT

public:
    ToolBar();

public slots:
    void slotComboBox(QString);
    void slotSpinBox(QString);

private:
    QTextEdit *textEdit;
    QComboBox *comboBox;
    QSpinBox *spinBox;
};

#endif // TOOLBAR_H

2、创建toolbar.cpp

#include "toolbar.h"

ToolBar::ToolBar():QMainWindow()
{
    setWindowTitle(tr("myToolBar"));

    textEdit = new QTextEdit(this);
    textEdit->setReadOnly(true);
    setCentralWidget(textEdit);

    QToolBar *toolBar = addToolBar("control");
    QLabel *label1 = new QLabel(tr("ctrl1: "));
    comboBox = new QComboBox;
    comboBox->insertItem(0,tr("ComboBox 0"));
    comboBox->insertItem(1,tr("ComboBox 1"));
    comboBox->insertItem(2,tr("ComboBox 2"));
    toolBar->addWidget(label1);
    toolBar->addWidget(comboBox);
    toolBar->addSeparator();
    QLabel *label2 = new QLabel(tr("ctrl2: "));
    spinBox = new QSpinBox;
    spinBox->setRange(1,10);
    toolBar->addWidget(label2);
    toolBar->addWidget(spinBox);

    connect(comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(slotComboBox(QString)));
    connect(spinBox,SIGNAL(valueChanged(QString)),this,SLOT(slotSpinBox(QString)));
}

void ToolBar::slotComboBox(QString combo)
{
    QString doc;
    QString spinStr;
    doc = "QComboBox: " + combo + "\n"
            + "QSpinBox: " + spinStr.setNum(spinBox->value());
    textEdit->setText(doc);
}

void ToolBar::slotSpinBox(QString value)
{
    QString doc;
    doc = "QComboBox: " + comboBox->currentText() + "\n"
            + "QSpinBox: " + value;
    textEdit->setText(doc);
}

3、创建main.cpp

#include <QApplication>

#include "toolbar.h"

int main(int argc,char *argv[])
{
    QApplication app(argc, argv);

    ToolBar *toolbar = new ToolBar;
    toolbar->show();

    return app.exec();
}

4、运行
这里写图片描述

5、资源代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值