【发布时间】:2011-09-09 06:02:21
【问题描述】:
我想使用 setToolButtonStyle(Qt::ToolButtonTextUnderIcon); 显示工具按钮图标的文本;
我可以看到直接添加到工具栏(关闭并保存)的操作的文本,但不能看到添加到工具按钮中 QMenu 的操作(加载)的文本。我在 Qmenu 中添加了此操作,以将其用作与其他操作(最近的文件)的切换。
我也尝试使用 setText() 和 setWindowIconText() 为 Toolbutton 设置文本,但它不起作用。这就是它现在的样子。
下面是相同的代码sn-p。
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);
感谢任何解决此问题的帮助。
【问题讨论】: