单击按钮时如何进行水平下拉菜单?(How can I make horizontal drop down menu when button clicked?)

编程入门 行业动态 更新时间:2024-10-07 00:19:00
单击按钮时如何进行水平下拉菜单?(How can I make horizontal drop down menu when button clicked?)

我正在尝试在单击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.

更多推荐

本文发布于:2023-08-04 21:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   菜单   水平   horizontal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!