gpt4 book ai didi

工具按钮下的Qt设置文本

转载 作者:行者123 更新时间:2023-12-04 13:28:12 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com