Qt设置工具按钮下的文本

发布于 2024-12-03 14:05:15 字数 2198 浏览 1 评论 0原文

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

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

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

图标下的文本不起作用

下面是相同的代码片段。

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);

任何解决此问题的帮助都将受到赞赏。

I want to show text for the tool button icons using setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

I can see the text for actions directly added to Toolbar (Close & Save), but not for an action(Load) that is added to a QMenu in a Toolbutton. I have added this action in Qmenu to work it as a toggle with other actions(recent files).

I tried to set the text for the Toolbutton too using setText() and setWindowIconText(), but it doesn't work. This is how it looks right now.

Text under icon not working

Below is the code snippet for the same.

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);

Any help to resolve this is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-12-10 14:05:15

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

    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,正如你在上面看到的那样。

Why don't you try something like that:

    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();

I didn't use a QMenu as you can see above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文