Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ContextMenu in QTreeView
Forum Updated to NodeBB v4.3 + New Features

ContextMenu in QTreeView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 10.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pablik2005
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

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

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pablik2005
        wrote on last edited by
        #3

        its debug without error ;(

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pablik2005
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pablik2005
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved