Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to implement right click menus on QTWidgetItems

Tags:

right-click

qt

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.

like image 892
magnus Avatar asked Jan 25 '10 16:01

magnus


2 Answers

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 !

like image 92
Andy M Avatar answered Oct 07 '22 00:10

Andy M


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.

like image 42
balpha Avatar answered Oct 06 '22 23:10

balpha