PDA

View Full Version : ContextMenu in QTreeView



Pablik
26th June 2012, 22:17
Hi i want make CustomContextMenu in reimplement QTreeView , but i have done somting wrong ;(

.h


#ifndef MYTREEVIEW_H
#define MYTREEVIEW_H

#include <QtGui>
#include <QtCore>

class MyTreeView : public QTreeView
{
Q_OBJECT
public:
explicit MyTreeView(QWidget *parent = 0);

signals:

public slots:
void slotCustomContextMenu(QPoint &point);
void slotTest();

};

#endif // MYTREEVIEW_H


.cpp


#include "mytreeview.h"

MyTreeView::MyTreeView(QWidget *parent) :
QTreeView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(QPo int &point)), this, SLOT(slotCustomContextMenu(QPoint &point)));
}

void MyTreeView::slotCustomContextMenu(QPoint &point)
{
QMenu *menu = new QMenu;
QModelIndex index = this->currentIndex();

QString fileName = this->model()->data(this->model()->index(index.row(), 0),0).toString();
menu->addAction(QString("Import"), this, SLOT(slotTest()));
menu->addAction(QString("Export"), this, SLOT(slotTest()));
menu->exec(QCursor::pos(&#41;&#41;;
}

void MyTreeView::slotTest()
{
}


treeview work but right click dont show menu ;( some advice ??

mentalmushroom
27th June 2012, 07:21
As far as I remember it is not necessary to create context menu for the treeview manually. Try something like this:



treeView->setContextMenuPolicy(Qt::ActionsContextMenu);
treeView->addAction(yourAction1);
treeView->addAction(yourAction2);
...

Pablik
27th June 2012, 09:28
thx its work


#include "mytreeview.h"

MyTreeView::MyTreeView(QWidget *parent) :
QTreeView(parent)
{
this->setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* lol;
lol = new QAction("item1",this);
this->addAction(lol);
//this->addAction(QString("Import"), this, SLOT(slotTest()));
//this->addAction(QString("Import2"), this, SLOT(slotTest()));
}

void MyTreeView::slotCustomContextMenu(QPoint &point)
{
QMenu *menu = new QMenu;
QModelIndex index = this->currentIndex();

QString fileName = this->model()->data(this->model()->index(index.row(), 0),0).toString();
menu->addAction(QString("Import"), this, SLOT(slotTest()));
menu->addAction(QString("Export"), this, SLOT(slotTest()));
menu->exec(QCursor::pos());
}

void MyTreeView::slotTest()
{
}

mentalmushroom
27th June 2012, 09:47
Keep in mind slotCustomContextMenu is not really necessary unless you want to implement some custom menu behavior. Take a look at the project attached.
7904

Pablik
27th June 2012, 15:09
Now i have next problem, if i dont have acces to ContextMenu Slot how i can disable/enable action in this menu dependent on what clicken(file/dir/nothing) ???

eny help ??

mentalmushroom
27th June 2012, 15:34
Handle selectionChanged or click events and disable/enable your actions there.

totktonado
28th January 2016, 09:43
Keep in mind slotCustomContextMenu is not really necessary unless you want to implement some custom menu behavior. Take a look at the project attached.
7904

Thanks, wery usefull!