Results 1 to 7 of 7

Thread: ContextMenu in QTreeView

  1. #1
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default ContextMenu in QTreeView

    Hi i want make CustomContextMenu in reimplement QTreeView , but i have done somting wrong ;(

    .h
    Qt Code:
    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
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    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(&#41;);
    19. }
    20.  
    21. void MyTreeView::slotTest()
    22. {
    23. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: ContextMenu in QTreeView

    As far as I remember it is not necessary to create context menu for the treeview manually. Try something like this:

    Qt Code:
    1. treeView->setContextMenuPolicy(Qt::ActionsContextMenu);
    2. treeView->addAction(yourAction1);
    3. treeView->addAction(yourAction2);
    4. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu in QTreeView

    thx its work
    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Pablik; 27th June 2012 at 09:32.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: ContextMenu in QTreeView

    Keep in mind slotCustomContextMenu is not really necessary unless you want to implement some custom menu behavior. Take a look at the project attached.
    TestTreeView.zip

  5. #5
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu in QTreeView

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

  6. #6
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: ContextMenu in QTreeView

    Handle selectionChanged or click events and disable/enable your actions there.

  7. #7
    Join Date
    Jan 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: ContextMenu in QTreeView

    Quote Originally Posted by mentalmushroom View Post
    Keep in mind slotCustomContextMenu is not really necessary unless you want to implement some custom menu behavior. Take a look at the project attached.
    TestTreeView.zip
    Thanks, wery usefull!

Similar Threads

  1. QTreeWidgetItem have contextMenu ?
    By yunpeng880 in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2012, 03:56
  2. Contextmenu in Qtreeview
    By LordQt in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2009, 16:39
  3. QLineEdit contextMenu
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 17th May 2007, 11:15
  4. contextmenu with MDI
    By Thoosle in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2006, 07:29
  5. ContextMenu
    By Naveen in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2006, 10:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.