Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Add contextual menu to QTableView

    General and Desktop
    3
    5
    3309
    Loading More Posts
    • 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.
    • V
      veera last edited by SGaist

      Hi All,
      In QT4 i am trying to implement context menu for my table got options on table like add delete after clicking the right clicking of mouse button ,......further i need to edit the table header elements as well as contents of the table on the same window with using push button or without using push button i am sharing my code below ....plz any one help me above issue and further procedure....

      model=new QStandardItemModel(10,5,this);
      
      QVBoxLayout *l=new QVBoxLayout(this);
      table=new QTableView(this);
      table->setModel(model);
      table->setContextMenuPolicy(Qt::CustomContextMenu);
      //connect(table, SIGNAL(customContextMenuRequested(QPoint)),SLOT(customMenuRequested(QPoint)));
      connect(this, SIGNAL(customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
      
      
      table->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(table->horizontalHeader(),
              SIGNAL(customContextMenuRequested(QPoint)),
              SLOT(customHeaderMenuRequested(QPoint)));
      l->addWidget(table);
      }
      
      
      void Widget::customMenuRequested(QPoint pos){
      QModelIndex index=table->indexAt(pos);
      
      QMenu *menu=new QMenu(this);
      menu->addAction(new QAction("Add", this));
      menu->addAction(new QAction("delete", this));
      //menu->addAction(new QAction("refresh", this));
      menu->popup(table->viewport()->mapToGlobal(pos));
      }
      
      void Widget::customHeaderMenuRequested(QPoint pos){
      int column=table->horizontalHeader()->logicalIndexAt(pos);
      
      QMenu *menu=new QMenu(this);
      menu->addAction(new QAction("ID", this));
      menu->addAction(new QAction("Name", this));
      menu->addAction(new QAction("Contact_No", this));
      menu->popup(table->horizontalHeader()->viewport()->mapToGlobal(pos));
      }
      
      
      #if 0
      void SomeDialog::itemClicked(QTableWidgetItem* item) // or any other slot
      {
        QMenu* contextMenu = new QMenu ( this );
        Q_CHECK_PTR ( contextMenu );
        contextMenu->addAction ( "New" , this , SLOT (newUnitBtnSlot()) );
        contextMenu->addAction ( "Clone" , this , SLOT (cloneUnitBtnSlot()) );
        contextMenu->popup( QCursor::pos() );
        contextMenu->exec ();
        delete contextMenu;
        contextMenu = 0;
      }
      
      
      void someWidget::someWidgetContextMenu(QPoint pos)
      {
          QMenu menu;
          menu.addAction(someActionCreatedByYou1);
          menu.addAction(someActionCreatedByYou2);
          menu.exec(QCursor::pos());
      }
      #endif
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      void Widget::on_pushButton_clicked()
      {
      
      }
      

      [edit: Add missing coding tags SGaist]

      1 Reply Last reply Reply Quote 0
      • V
        veera @SGaist last edited by

        @SGaist
        Hi SGaist, plz can u see my code and guide me in right direction .....
        WIth regards
        Veera

        Ratzz V 2 Replies Last reply Reply Quote 0
        • Ratzz
          Ratzz @veera last edited by

          @veera
          Please post new question for your problem. Do not try to use others topic .

          --Alles ist gut.

          1 Reply Last reply Reply Quote 2
          • V
            veera @veera last edited by

            @veera
            I am creating the table for context menu like add delete after right clicking the mouse button got the options as add delete ....now i need to save the data how should i do it......

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi and welcome to devnet,

              First thing, like @Ratzz wrote, don't highjack other people thread with unrelated questions. That's rude. I've created a new thread for your question.

              Second, don't ping your own questions so soon, allow at least 24 hours before doing that. This is a community forum and people answering here are not all living in the same time zone as you.

              Then, what issue do you have exactly ? Currently, it looks like you are asking for someone to review your code and write the solution.

              Also, at least part of the topics you are asking about are covered by the documentation:

              • Model View
              • Model View Programming
              • The related examples

              Did you take a look at them ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 2
              • First post
                Last post