自定义Qt QMenu [英] Custom Qt QMenu
问题描述
Qt中是否可以将布局或小部件添加到QMenu
中以创建自定义菜单?
Is there a way in Qt to add a layout or widgets to a QMenu
to create a custom menu?
下面的示例(左)就是我所拥有的,我想通过添加非菜单窗口小部件来实现类似于右侧模型的目的.如果QMenu
无法做到这一点,是否有指南可以在任何地方产生类似的结果(也许通过使标准的小部件充当上下文菜单)?
The example below (left) is what I have, and I'd like to aim for something similar to the mock-up on the right, by adding non-menu widgets. If it can't be done by QMenu
, are there guides to produce similar results (perhaps by having a more standard widget act as a context menu) anywhere?
推荐答案
当然可以!在Qt中,如果有一个意志,那就有一种方法.
Sure there is! In Qt, if there is a a will there is a way.
您可能需要创建自己的使用QMenu
并使用成员QListWidget
的类.
You will probably need to make your own class that uses QMenu
and uses a member QListWidget
.
然后,您将需要生成布局并重载所有正确的QLayout
函数以进行尺寸重新计算.
You will then need to generate the layout and overload all the correct QLayout
functions for size recalculation.
然后您需要使用此布局(请考虑QHBoxLayout
)来同时显示QMenu
和QListWidget
.
And then you'll need to use this layout (think QHBoxLayout
) to display both a QMenu
and a QListWidget
side by side.
这足以使您指向正确的方向.
That should be enough to point you in the right direction.
正如评论者所指出的,您不能继承两个QObject
内容,因此我更新了
相应地回答.
As a commenter pointed out, you can't inherit two QObject
things so I updated the
answer accordingly.
这篇关于自定义Qt QMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!