我想在 QTWidgetItems 上实现右键菜单
我正在开发一个uni项目,我们的目标是制作一个程序,扫描选定目录上的所有img/视频/电影文件,将它们存储在数据库中,然后以有组织的方式shozs它们(使用QTreeWidgetItem)。程序允许您执行一些操作,例如读取文件、打开文件等。现在,问题是我想右键单击其中一个文件并弹出一个菜单,其中包含许多选项,例如打开目录,删除文件......
我只是不知道如何制作右键菜单,我有点QT 新手,我尝试这样做:
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(openMenu(QTreeWidgetItem *, int)));
我尝试重新定义 itemClicked 方法,但似乎无法找到如何知道它是否是右键单击,我想我可能会以错误的方式尝试。
我从中得到启发:
connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(opennMusic(QTreeWidgetItem *, int)));
双击即可执行音乐文件。
如果您需要更多代码部分,请告诉我:)。
在安迪的最后一条评论后编辑以显示新代码:
#include "affichagemusique.h"
void AffichageMusique::lireMusique(QTreeWidgetItem *item, int column)
{
if(item->text(6)!=NULL)
{
Phonon::MediaSource source(item->text(6));
mediaObject->setCurrentSource(source);
mediaObject->play();
}
}
void AffichageMusique::vueArtiste()
{
layout->removeWidget(treeWidget);
treeWidget = new QTreeWidget();
QAction* pOpenDir = new QAction(tr("Play music"),treeWidget );
treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
treeWidget->addAction(pOpenDir);
.......
}
// later on on the code
void AffichageMusique::pOpenDir()
{
QTreeWidget * treeWidget = new QTreeWidget();
QTreeWidgetItem * QTreeWidgetI= treeWidget->currentItem();
lireMusique(QTreeWidgetI, 6);
}
即使我删除 QTreeWidget * treeWidget = new QTreeWidget();行它不起作用,当我右键单击时我看到菜单,但当我单击播放时,什么也没有发生。
I am working on a uni project and our goal is to make a program that scans all img/video/movie files on a selected directory, stores them in a database and then shozs them in an organised way (using QTreeWidgetItem). Program allows you to do some stuff like read the files, open them and so on. Now, the problem is that I would like to Right click one of the files and pop up a Menu with many options like Open Directory, delete file...
I just don' know how to make that right click menu, I'm kinda new to QT, I tried making this:
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(openMenu(QTreeWidgetItem *, int)));
I tried redifining the itemClicked method but can't seem to find how to know if it's a right click and i think I might be trying it the wrong way.
I inspired from this:
connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(opennMusic(QTreeWidgetItem *, int)));
which executes a music file on double click.
If you need more code parts just let me know :).
Edit after andy's last comment to show new code:
#include "affichagemusique.h"
void AffichageMusique::lireMusique(QTreeWidgetItem *item, int column)
{
if(item->text(6)!=NULL)
{
Phonon::MediaSource source(item->text(6));
mediaObject->setCurrentSource(source);
mediaObject->play();
}
}
void AffichageMusique::vueArtiste()
{
layout->removeWidget(treeWidget);
treeWidget = new QTreeWidget();
QAction* pOpenDir = new QAction(tr("Play music"),treeWidget );
treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
treeWidget->addAction(pOpenDir);
.......
}
// later on on the code
void AffichageMusique::pOpenDir()
{
QTreeWidget * treeWidget = new QTreeWidget();
QTreeWidgetItem * QTreeWidgetI= treeWidget->currentItem();
lireMusique(QTreeWidgetI, 6);
}
Even if I remove the QTreeWidget * treeWidget = new QTreeWidget(); line it wont work, I see the menu when i right click but when I click PLay, nothing happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 QWidget 类中,您可以找到一个名为的方法:
使用参数: Qt::ActionsContextMenu (您可以通过代码或在 QtDesigner 中设置它。)
然后您可以像这样创建一个 QMenu :
使用这个很好,因为您可以在其他菜单中重复使用您的操作,例如在主窗口菜单中...
希望它有所帮助!
In the QWidget class, you can find a method called :
With the parameter : Qt::ActionsContextMenu (You can either set it by code or in the QtDesigner.)
Then you can then create a QMenu like this :
It's nice to use this because you can re-use your actions in other menus, for example, in the main window menu...
Hope it helps a bit !
有一个针对这种情况的事件处理程序:
QWidget::contextMenuEvent
。除了您不必检查特定的鼠标键这一事实之外,它还有一个优点,即用户可以使用键盘创建上下文菜单,或者如果用户正在工作,则可以使用完全不同的方式在通过其他方式请求上下文菜单的平台上。There's an event handler for exactly that case:
QWidget::contextMenuEvent
. Besides the fact that you don't have to check for the specific mouse key, it also has the advantage that this enables the user to create the context menu using the keyboard, or maybe to use a totally different way, if the user is working on a platform where context menus are requested via other means.您还可以快速查看以下 Qt 示例: http://qt .nokia.com/doc/4.6/phonon-qmusicplayer.html。
这里没有树,但也许你应该尝试认真看看他们如何初始化窗口,他们如何构建 QTableWidget 并填充它以及他们如何使用信号/槽。
我还建议您从这个例子中进行测试。获取他们的源代码,并尝试修改它以添加右键单击...就像您对树所做的那样...当您让它完美工作时,您会发现需要 23 秒才能通过它从表到树小部件...
让我们知道您是否在这个示例中遇到了更少的麻烦...
编辑:在您输入一些代码后...
1)lireMusique 有一个从未使用过的列参数。
2) item->text(6) 返回一个不能为 NULL 的 QString。 item,可以为NULL,所以如果是的话,如果不测试item!=NULL,每次进入方法都会崩溃。
3)在 vueArtiste 中,为什么要删除并重新创建 Widget ?我可能错过了一些代码,但是......
4)你的方法pOpenDir很奇怪......我将其命名为onActOpenDirectoryTriggered()。它是否正确连接到您的 pOpenDir 操作的触发信号?
5)为什么每次触发操作时都要创建一个新的 QTreeWidget?您的新树与 vueArtiste 中的 treeWidget 变量同名!这两个变量的作用域不同,但名称相同!这是灾难的根源!
...
我真的建议您阅读一两个 Qt 的模型/视图示例,它们很简单,做得很好,会对您有很大帮助!
我还建议您遵循严格的命名约定,它们将帮助您避免很多错误和烦人的情况...我还会避免在代码中混合语言(忘记法语,到处使用英语)...换句话说,尝试并且让你的代码更加稳定!
最后,由于您似乎是法语母语人士,我建议您阅读一本 Qt4 书籍,例如 这本书 (法语第一,英语第二)。我从这个开始,它将提供您需要的一切,甚至更多!
You could also have a quick look at the following Qt Example : http://qt.nokia.com/doc/4.6/phonon-qmusicplayer.html.
There is no tree in here, but maybe you should try and have a serious look at how they initialise the window, how they build the QTableWidget and populates it and how they use signals/slots.
I would also recommend you to give it a test from this example. Take their source code, and try to modify it to add the right click... The same way you would do for your tree... When you got it to work perfectly, you'll see it will take you 23 seconds to pass it from Table to tree Widget...
Let us know if you have less trouble with this example...
Edit : After you put some code...
1) lireMusique has a column parameter that is never used.
2) item->text(6) returns a QString that cannot be NULL. item, can be NULL, so if it is, you'll crash everytime you enter the method if you don't test item!=NULL.
3) In vueArtiste, why do you remove and recreate your Widget ? I maybe miss some code but...
4) Your method pOpenDir is strange... I would name it onActOpenDirectoryTriggered(). Is it properly connected to your pOpenDir action's trigger signal ?
5) Why creating a new QTreeWidget everytime you trigger on your action ? Your new tree has the same name as the treeWidget var in vueArtiste ! Those two variables don't have the same scope but the same name ! It's a recipe for disaster !
...
I would really suggest you to read one or two model/view example by Qt, they are straightforward, nicely done and it will help you a lot !
I would also suggest that you follow strict naming conventions, they will help you avoiding lot of bugs and annoying situations... I would also avoid mixing languages in the code (forget french, use english everywhere)... In other words, try and be more constant in your code !
Finally, since you seem to be a french native speaker, I would recommend to read a Qt4 book, like this book (1st in french, 2nd in english). I started with this one, it will give everything you need and much more !