June 26, 2012

Pablik2005 Pablik2005
Lab Rat
33 posts

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

  1. #ifndef MYTREEVIEW_H
  2. #define MYTREEVIEW_H
  3.  
  4. #include <QtGui>
  5. #include <QtCore>
  6.  
  7. class MyTreeView : public QTreeView
  8. {
  9.     Q_OBJECT
  10. public:
  11.     explicit MyTreeView(QWidget *parent = 0);
  12.    
  13. signals:
  14.    
  15. public slots:
  16.     void slotCustomContextMenu(QPoint &point);
  17.     void slotTest();
  18.    
  19. };
  20.  
  21. #endif // MYTREEVIEW_H

.cpp

  1. #include "mytreeview.h"
  2.  
  3. MyTreeView::MyTreeView(QWidget *parent) :
  4.     QTreeView(parent)
  5. {
  6.     this->setContextMenuPolicy(Qt::CustomContextMenu);
  7.     connect(this,SIGNAL(customContextMenuRequested(QPoint &point)), this, SLOT(slotCustomContextMenu(QPoint &point)));
  8. }
  9.  
  10. void MyTreeView::slotCustomContextMenu(QPoint &point)
  11. {
  12.      QMenu *menu = new QMenu;
  13.      QModelIndex index = this->currentIndex();
  14.  
  15.      QString fileName = this->model()->data(this->model()->index(index.row(), 0),0).toString();
  16.      menu->addAction(QString("Import"), this, SLOT(slotTest()));
  17.      menu->addAction(QString("Export"), this, SLOT(slotTest()));
  18.      menu->exec(QCursor::pos());
  19. }
  20.  
  21. void MyTreeView::slotTest()
  22. {
  23. }

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

4 replies

June 27, 2012

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi,

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

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 27, 2012

Pablik2005 Pablik2005
Lab Rat
33 posts

its debug without error ;(

June 27, 2012

Pablik2005 Pablik2005
Lab Rat
33 posts

i finded solution

  1. #include "mytreeview.h"
  2.  
  3. MyTreeView::MyTreeView(QWidget *parent) :
  4.     QTreeView(parent)
  5. {
  6.     this->setContextMenuPolicy(Qt::ActionsContextMenu);
  7.     QAction* lol;
  8.     lol = new QAction("item1",this);
  9.     this->addAction(lol);
  10.     //this->addAction(QString("Import"), this, SLOT(slotTest()));
  11.     //this->addAction(QString("Import2"), this, SLOT(slotTest()));
  12. }
  13.  
  14. void MyTreeView::slotCustomContextMenu(QPoint &point)
  15. {
  16.      QMenu *menu = new QMenu;
  17.      QModelIndex index = this->currentIndex();
  18.  
  19.      QString fileName = this->model()->data(this->model()->index(index.row(), 0),0).toString();
  20.      menu->addAction(QString("Import"), this, SLOT(slotTest()));
  21.      menu->addAction(QString("Export"), this, SLOT(slotTest()));
  22.      menu->exec(QCursor::pos());
  23. }
  24.  
  25. void MyTreeView::slotTest()
  26. {
  27. }

June 27, 2012

Pablik2005 Pablik2005
Lab Rat
33 posts

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

 
  ‹‹ Qt3D - Retrieving Qml Viewport Element as C++ Object      Synchronization of mp3 tracks on Qt ››

You must log in to post a reply. Not a member yet? Register here!