我正在尝试在单击QPushButton时使用水平方向制作下拉弹出菜单,菜单项仅为图标,没有快捷文本和菜单项文本。 在Qt有可能吗? 我试过谷歌搜索,但我找不到解决方案,也没有在Qt文档中。
在ascii中,水平下拉菜单如下所示:
按键 | Img0 | Img1 | Img2 | Img3 |
我编写了像这样的相关部分:
QImage sourceImage(":/Res/layout/layout_wide_select.png"); int w = sourceImage.width() / 7; int h = sourceImage.height(); QImage lay5= sourceImage.copy(0, 0, w, h); QImage lay7= sourceImage.copy(w, 0, w, h); .... m_menuWide = new QMenu(this); QAction *actLay5 = m_menuWide->addAction(QIcon(QPixmap::fromImage(lay5)), "5view"); QObject::connect(actLay5, SIGNAL(triggered(bool)), this, SLOT(onSplitWideButtonClicked())); actLay5->setData(QVariant(0)); QAction *actLay7 = m_menuWide->addAction(QIcon(QPixmap::fromImage(lay7)), "7view"); QObject::connect(actLay7, SIGNAL(triggered(bool)), this, SLOT(onSplitWideButtonClicked())); actLay7->setData(QVariant(1)); ... m_btnSplitWide->setMenu(m_menuWide);似乎在QMenu中没有类似setMenuDireciton()的方法,或者QMenu样式中没有QMenu属性可用...
如果有人知道解决方案,我将非常感激。
谢谢!!
I'm trying to make drop-down popup menu with horizontal direction when a QPushButton is clicked, with menu item being only icon, without short cut text and menu item text. Is it possible in Qt? I've tried google search, but I can't find the solution, also not in Qt documentation.
In ascii drawing horizontal drop-down menu is appears like this below a button:
button | Img0 | Img1 | Img2 | Img3 |
I've coded the relevant parts like this:
QImage sourceImage(":/Res/layout/layout_wide_select.png"); int w = sourceImage.width() / 7; int h = sourceImage.height(); QImage lay5= sourceImage.copy(0, 0, w, h); QImage lay7= sourceImage.copy(w, 0, w, h); .... m_menuWide = new QMenu(this); QAction *actLay5 = m_menuWide->addAction(QIcon(QPixmap::fromImage(lay5)), "5view"); QObject::connect(actLay5, SIGNAL(triggered(bool)), this, SLOT(onSplitWideButtonClicked())); actLay5->setData(QVariant(0)); QAction *actLay7 = m_menuWide->addAction(QIcon(QPixmap::fromImage(lay7)), "7view"); QObject::connect(actLay7, SIGNAL(triggered(bool)), this, SLOT(onSplitWideButtonClicked())); actLay7->setData(QVariant(1)); ... m_btnSplitWide->setMenu(m_menuWide);It seems that there is no method like setMenuDireciton() in QMenu, or and no QMenu property usable in QMenu style...
I will be very appreciated if anyone know the solution..
Thank you!!
最满意答案
你可以检查QWidgetAction这会将小部件添加到你的QMenu。 您可以创建动作的QWidget(可能是工具栏)并将其放在QWidgetAction上。 并将widgetaction添加到您的QMenu。 示例代码:
QMenu menu; QToolBar toolbar; toolbar.addAction("A"); toolbar.addAction("B"); QWidgetAction act(&toolbar); act.setDefaultWidget(&toolbar); menu.addAction(&act); menu.exec();您只能使用图标更改操作“A”和“B”。
You can check QWidgetAction This will add widget to your QMenu. You can create your QWidget (maybe toolbar) of your action and put it on QWidgetAction. And add the widgetaction to your QMenu. Sample code :
QMenu menu; QToolBar toolbar; toolbar.addAction("A"); toolbar.addAction("B"); QWidgetAction act(&toolbar); act.setDefaultWidget(&toolbar); menu.addAction(&act); menu.exec();You can change the action "A" and "B" only with your icon.
更多推荐
发布评论