使用模型作为 QMenu 的源
我创建了一个模型,其中列出了现有配置(假设它列出了“文件”,因为这在这里并不重要)。到目前为止,它在附加到 QListView 时运行良好。
示例:
--- ListView ---
- file #1 -
- file #2 -
- file #3 -
- file #4 -
----------------
是否可以对动态更新的 QMenu
使用相同的模型?
类似于:
Menu
-> Submenu #1
-> Submenu #2
-> File-submenu
-> file #1
-> file #2
-> file #3
-> file #4
-> Submenu #3
简而言之:有什么方法可以根据模型QAction列表(分组到相同的QMenu
) >(源自QAbstractListModel
)?
I created a model which list the existing configurations (let's say it lists "files", as this doesn't really matter here). So far, it works well when attached to a QListView
.
Example:
--- ListView ---
- file #1 -
- file #2 -
- file #3 -
- file #4 -
----------------
Is it possible to use the same model for a dynamically updated QMenu
?
Something like:
Menu
-> Submenu #1
-> Submenu #2
-> File-submenu
-> file #1
-> file #2
-> file #3
-> file #4
-> Submenu #3
In short: is there any way to create a list of dynamicaly updated QAction
s (grouped into the same QMenu
) depending on a model (derived from QAbstractListModel
) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,没有
QMenuView
类,但我在网上发现了这个有希望的实现:QMenuView
(qmenuview.h,qmenuview .cpp)。Unfortunately there is no
QMenuView
class but I found this promising implementation on the net:QMenuView
(qmenuview.h, qmenuview.cpp).如果您的目标只是使用 QAbstractListModel 中可用的项目文本更新菜单操作,那么答案是“是”。
这是一种方法..
可以使用以下函数获得单个项目的索引。
有了获得的索引,就可以通过以下方式获得数据,
然后可以通过使用获得索引中可用的文本,
现在有了获得的QString,您可以向菜单添加一个操作。
您必须确保的是,您应该能够遍历 Model 中的所有项目,以便获得每个项目的索引。希望有帮助..
If your objective is just to update your menu actons with the item text that are available in the
QAbstractListModel
, then the answer is Yes.Here is a way..
Individual item's index can be obtained by using the following function.
With the obtained index, the data can be obtained by,
Then the text availalble in the index can be obtained by using,
Now with the obtained QString you can add an action to the menu.
The thing you have to make sure is that, you should be able to traverse through all the items in the Model, so that you can obtain the index of the each and every item. Hope it helps..
回答你的简短问题,是的,有。但你必须自己写。
最简单的部分是创建 QAbstractListModel 的子类。
困难的部分是当你创建自己的视图时。 Qt 将允许您创建自己的视图,就像您创建自己的模型一样,但它会变得更加复杂,因为您必须自己处理一切。
对于特定目的来说,这是完全可行的,但它也比我认为你想要的要多得多。因此,正如 Gianni 所说,Qt 的模型视图框架不适合以这种方式使用。
To answer your short question, yes, there is. But you'll have to write it yourself.
The easy part would be to create a subclass of QAbstractListModel.
The hard part would be when you create your own view. Qt will let you create your own view, just like if you were to create your own model, but it would become so much more complex, since you've got to handle everything yourself.
It's entirely doable for a specific purpose, but it's also much more work than I think you want. So, like Gianni was saying, Qt's model-view framework isn't meant to be used this way.
不可以。模型只能与视图一起使用,根据 模型视图 Qt 使用的框架。
No. Models can only be used with Views, as per the Model-View framework that Qt uses.
您可以创建一个菜单项并使用
QWidgetAction
将QListView
放入其中。当然这个菜单不能有子菜单。下面的示例是用 Python 编写的,但我希望在这种情况下这并不重要。You can create a menu item and put
QListView
into it usingQWidgetAction
. Of course this menu cannot have submenus. The example below is in Python, but I hope it doesn't matter in this case.