ContextMenu in QTreeView



  • Hi i want make CustomContextMenu in reimplement QTreeView, i finde on forum this http://qt-project.org/forums/viewthread/1738 , but i have to done somting wrong ;(

    .h
    [code]
    #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
    [/code]

    .cpp
    [code]
    #include "mytreeview.h"

    MyTreeView::MyTreeView(QWidget *parent) :
    QTreeView(parent)
    {
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this,SIGNAL(customContextMenuRequested(QPoint &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&#40;QCursor::pos(&#41;&#41;;
    

    }

    void MyTreeView::slotTest()
    {
    }
    [/code]

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



  • Hi,

    did you check whether you get a debug error message on the connect statement?



  • its debug without error ;(



  • i finded solution
    [code]
    #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&#40;QCursor::pos(&#41;);
    

    }

    void MyTreeView::slotTest()
    {
    }
    [/code]



  • 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 ??


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.