QToolBar上的标签,可以吗? [英] Label on QToolBar, possible?

查看:143
本文介绍了QToolBar上的标签,可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 QToolBar 中包含标签?我在应用程序屏幕截图上方绘制了绿色文字。

Is is possible to have labels in a QToolBar? Something like the green text I draw on top of the app screenshot.

我希望在工具栏中有一组动作,所有动作都还原到某个系统上。然后是另一个工具栏,它具有与另一系统有关的另一组动作。因此,每个工具栏都有一个根据特定系统命名的标签。

I would like to have a set of actions in a toolbar, all them retated to some a system. Then another toolbar with another set of actions related to another system. So each toolbar would have a label named accordingly to the specific system.

QApplication a(argc, argv);
QMainWindow *w = new QMainWindow;

QToolBar *barA = new QToolBar;
barA->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

QAction *actOnA = new QAction("OnA");
actOnA->setIcon(QIcon("../../../on.png"));
barA->addAction(actOnA);
QAction *actOffA = new QAction("OffA");
actOffA->setIcon(QIcon("../../../off.png"));
barA->addAction(actOffA);
w->addToolBar(barA);

QToolBar *barB = new QToolBar;
barB->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

QAction *actOnB = new QAction("OnB");
actOnB->setIcon(QIcon("../../../on.png"));
barB->addAction(actOnB);
QAction *actOffB = new QAction("OffB");
actOffB->setIcon(QIcon("../../../off.png"));
barB->addAction(actOffB);
w->addToolBar(barB);

w->show();
return a.exec();

我想我在Microsoft MFC项目中看到了类似的标签。

I think I saw similar labels in a Microsoft MFC project.

推荐答案

是的,您需要先创建自定义窗口小部件,然后执行以下操作:

yes, you need to create your custom widget first and then do:

ui->statusBar->addWidget(MY_CUSTOM_WIDGEt);

例如

auto b = new QPushButton(this);
b->setText("hello");
connect(b, &QPushButton::clicked, [](){qDebug()<< "ok...";});
ui->statusBar->addWidget(b);

这篇关于QToolBar上的标签,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆