I want to implement right click menus on QTWidgetItems
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.
In the QWidget class, you can find a method called :
void setContextMenuPolicy (Qt::ContextMenuPolicy policy)
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 :
QTreeWidget* pTreeWidget = new QTreeWidget();
QAction* pOpenFile = new QAction(tr("Open A File"), pContextMenu);
pTreeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
pTreeWidget->addAction(pTestCard);
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 !
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.
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 !
精彩评论