工具按钮下的Qt设置文本

标签 qt qt4

我想使用 setToolButtonStyle(Qt::ToolButtonTextUnderIcon) 显示工具按钮图标的文本;

我可以看到直接添加到工具栏(关闭并保存)的操作的文本,但不能看到添加到工具按钮中 QMenu 的操作(加载)的文本。我在 Qmenu 中添加了此操作,以将其用作与其他操作(最近的文件)的切换。

我也尝试使用 setText() 和 setWindowIconText() 设置工具按钮的文本,但它不起作用。这就是它现在的样子。

Text under icon not working

下面是相同的代码片段。

actionLoad = new QAction(QIcon(QString("%1/cn_open.png").arg(imageDir)),tr("Load"), this);
actionLoad->setShortcut(tr("Ctrl+L"));
actionLoad->setStatusTip(tr("Load the model"));
connect(actionLoad, SIGNAL(triggered()), this, SLOT(loadModelDlg()));

actionClose = new QAction(QIcon(QString("%1/cn_close.png").arg(imageDir)),tr("Close"), this);
actionClose->setShortcut(tr("Ctrl+X"));
actionClose->setStatusTip(tr("Close the Model"));
connect(actionClose, SIGNAL(triggered()), this, SLOT(closeModel()));

actionSave = new QAction(QIcon(QString("%1/cn_save.png").arg(imageDir)),tr("Save"), this);
actionSave->setShortcut(tr("Ctrl+S"));
actionSave->setStatusTip(tr("Save the Model"));
connect(actionSave, SIGNAL(triggered()), this, SLOT(saveModel()));

m_FileToolBar = addToolBar(tr("File"));
// Show text under the icon in toolbar
m_FileToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

// Add a menu for recent file items
m_FileMenu     = new QMenu();
m_FileMenu->addAction(actionLoad); // Add load button as the first item
for (int i = 0; i < MaxRecentFiles; ++i)
  m_FileMenu->addAction(recentFileActions[i]);
updateRecentFileActions();

// Create a tool button. Load button and recent files will be added as a drop down menu
m_FileToolButton = new QToolButton();
m_FileToolButton->setText(tr("Load")); // Not working
m_FileToolButton->setWindowIconText(tr("Load")); // Not working
m_FileToolButton->setMenu(m_FileMenu);
m_FileToolButton->setDefaultAction(actionLoad); 

// This creates a dropdown arrow to click.
m_FileToolButton->setPopupMode(QToolButton::MenuButtonPopup); 

m_FileToolBar->addWidget(m_FileToolButton);

// These actions show text under the icon
m_FileToolBar->addAction(actionClose); 
m_FileToolBar->addAction(actionSave);

任何解决此问题的帮助表示赞赏。

最佳答案

你为什么不尝试这样的事情:

    QToolBar bar;
    QToolButton button;

    button.setPopupMode(QToolButton::MenuButtonPopup);
    button.setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    QAction loadAction(QIcon(":/img/openfile"),"Load",&button);
    button.addAction(&loadAction);
    button.setDefaultAction(&loadAction);

    QAction loadAction2("Load 2",&button);
    button.addAction(&loadAction2);

    bar.addWidget(&button);
    bar.show();

正如你在上面看到的,我没有使用 QMenu。

关于工具按钮下的Qt设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7357708/

相关文章:

c++ - Qt - 一个简单的回显服务器

c++ - QWidget::mousePressEvent() 同时在两个小部件上

python - 是否可以更改 QTableWidget 行标签的颜色?

c++ - 将 C++ 对象暴露给 QML 并监听更改事件

c++ - 如何在 Windows 中从命令行运行 Qt

c++ - Qt4 与 UI 线程的接口(interface)

qt - 如何通过拖动 Widget 的角来调整 Widget 上的 QTableWidget 大小

c++ - QRegEx 帮助,一般的 RegEx

html - QDomDocument 按类获取元素

qt - 将现有 QDialog 保存到 *.ui 文件