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. Add contextual menu to QTableView

Add contextual menu to QTableView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.8k Views
  • 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 Offline
    V Offline
    veera
    wrote on last edited by SGaist
    #1

    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
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

      Your description is a bit vague. Can you share your code so we can take look at it ?

      V Offline
      V Offline
      veera
      wrote on last edited by
      #2

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

      RatzzR V 2 Replies Last reply
      0
      • V veera

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

        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by
        #3

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

        --Alles ist gut.

        1 Reply Last reply
        2
        • V veera

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

          V Offline
          V Offline
          veera
          wrote on last edited by
          #4

          @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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            2

            • Login

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