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. Context menu over editable QTableView
Qt 6.11 is out! See what's new in the release blog

Context menu over editable QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 6.9k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have an editable QTableView. I want the user be able to delete a record by right clicking the row and choosing the delete option from a context menu. I changed the ActionContextMenu to ActionContextMenu. The context menu pops up when there is no QTableView, but with QTableView both mouse button does what the left button should (allow editing the cell). Is there a way to use the right click in this case?
    Thank you.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt ?
      On what OS ?
      With which compiler ?

      Can you show the code you use to setup your widget and its contextual menu ?

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

      G 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What version of Qt ?
        On what OS ?
        With which compiler ?

        Can you show the code you use to setup your widget and its contextual menu ?

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @SGaist
        Hi,
        I use

        • Qt Creator 4.4.1 Based on Qt 5.9.2

        • Windows 10

        • MinGW 5.3.0 32 bit for C++

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The code is still needed

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

          G 1 Reply Last reply
          0
          • SGaistS SGaist

            The code is still needed

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @SGaist
            At this point I only changed the ActionContextMenu in designer and created an action in the Action Editor.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              On what widget did you add that ?

              Just in case, there's an old thread here about that topic.

              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
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Make sure to translate the position of the popupmenu if you want it to be over the cell.
                Like
                menu->popup(viewX->viewport()->mapToGlobal(pos));
                as mention here
                http://doc.qt.io/qt-5/qmenu.html#popup

                G 1 Reply Last reply
                1
                • mrjjM mrjj

                  Hi
                  Make sure to translate the position of the popupmenu if you want it to be over the cell.
                  Like
                  menu->popup(viewX->viewport()->mapToGlobal(pos));
                  as mention here
                  http://doc.qt.io/qt-5/qmenu.html#popup

                  G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

                  @mrjj , @SGaist
                  I have the following now:

                  ui->tableView_Fix->setModel (fixModel);
                  
                    ui->tableView_Fix->setContextMenuPolicy (Qt::CustomContextMenu);
                  
                    connect(ui->tableView_Fix, SIGNAL(customContextMenuRequested(QPoint)),
                            SLOT(customMenuRequested(QPoint)));
                  
                  void FixDb::customMenuRequested(QPoint pos) {
                    QModelIndex index = ui->tableView_Fix->indexAt(pos);
                  
                    QMenu* menu = new QMenu(this);
                    menu->addAction(new QAction("Action 1", this));
                    menu->addAction(new QAction("Action 2", this));
                    menu->addAction(new QAction("Action 3", this));
                    menu->popup(ui->tableView_Fix->viewport()->mapToGlobal(pos));
                  
                    QPoint point;
                    point = menu->mapToParent (pos);
                    qDebug() << "pos: " << pos;
                  

                  This gives me the coordinates of the context menu, but how can I convert it to the index f the cell in QTableView?
                  Thank you.

                  mrjjM 1 Reply Last reply
                  0
                  • G gabor53

                    @mrjj , @SGaist
                    I have the following now:

                    ui->tableView_Fix->setModel (fixModel);
                    
                      ui->tableView_Fix->setContextMenuPolicy (Qt::CustomContextMenu);
                    
                      connect(ui->tableView_Fix, SIGNAL(customContextMenuRequested(QPoint)),
                              SLOT(customMenuRequested(QPoint)));
                    
                    void FixDb::customMenuRequested(QPoint pos) {
                      QModelIndex index = ui->tableView_Fix->indexAt(pos);
                    
                      QMenu* menu = new QMenu(this);
                      menu->addAction(new QAction("Action 1", this));
                      menu->addAction(new QAction("Action 2", this));
                      menu->addAction(new QAction("Action 3", this));
                      menu->popup(ui->tableView_Fix->viewport()->mapToGlobal(pos));
                    
                      QPoint point;
                      point = menu->mapToParent (pos);
                      qDebug() << "pos: " << pos;
                    

                    This gives me the coordinates of the context menu, but how can I convert it to the index f the cell in QTableView?
                    Thank you.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi

                    • how can I convert it to the index f the cell in QTableVie

                    The index is this
                    QModelIndex index = ui->tableView_Fix->indexAt(pos);

                    Is that what you mean?

                    G 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      Hi

                      • how can I convert it to the index f the cell in QTableVie

                      The index is this
                      QModelIndex index = ui->tableView_Fix->indexAt(pos);

                      Is that what you mean?

                      G Offline
                      G Offline
                      gabor53
                      wrote on last edited by gabor53
                      #10

                      @mrjj
                      Yes, thank you. That part works now.
                      Now I have the following code:

                      #include "fixdb.h"
                      #include "ui_fixdb.h"
                      #include <QDebug>
                      
                      
                      FixDb::FixDb(QWidget* parent) :
                        QDialog(parent),
                        ui(new Ui::FixDb) {
                        ui->setupUi(this);
                      
                      
                      
                        correctFriend ();
                      }
                      
                      FixDb::~FixDb() {
                        delete ui;
                      }
                      
                      void FixDb::correctFriend() {
                      
                      
                        QSqlTableModel* fixModel = new QSqlTableModel(this);
                      
                        fixModel->setTable ("Items");
                        fixModel->setEditStrategy (QSqlTableModel::OnRowChange);
                        fixModel->setSort (2, Qt::DescendingOrder);
                      
                        fixModel->select ();
                      
                        ui->tableView_Fix->setModel (fixModel);
                      
                        ui->tableView_Fix->setContextMenuPolicy (Qt::CustomContextMenu);
                      
                        connect(ui->tableView_Fix, SIGNAL(customContextMenuRequested(QPoint)),
                                SLOT(customMenuRequested(QPoint)));
                      
                      
                        ui->tableView_Fix->setItemDelegateForColumn(2, new FixViewDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (3, new WhatFixViewDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (4, new MaterialDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (5, new colorFixDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (7, new adoptDateFixDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (9, new historyFixDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn (11, new notesFixDelegate(this));
                      
                        QSqlQuery fixQueryDisp;
                        fixQueryDisp.prepare("SELECT * FROM Items");
                        fixQueryDisp.exec ();
                      
                        for(int row1 = 0; fixQueryDisp.next (); row1++) {
                          ui->tableView_Fix->setRowHeight (row1, 100);
                        };
                      
                        ui->tableView_Fix->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold; text-decoration: underline;}");
                        ui->tableView_Fix->verticalHeader ()->setVisible (false);
                        ui->tableView_Fix->setAlternatingRowColors (true);
                        ui->tableView_Fix->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial; ");
                      
                        fixModel->setHeaderData (0, Qt::Horizontal, QObject::tr ("ID"));
                        fixModel->setHeaderData (1, Qt::Horizontal, QObject::tr ("Name"));
                        fixModel->setHeaderData (3, Qt::Horizontal, QObject::tr ("What"));
                        fixModel->setHeaderData (2, Qt::Horizontal, QObject::tr ("Image"));
                        fixModel->setHeaderData (4, Qt::Horizontal, QObject::tr ("Material"));
                        fixModel->setHeaderData (5, Qt::Horizontal, QObject::tr ("Color"));
                        fixModel->setHeaderData (6, Qt::Horizontal, QObject::tr ("Description"));
                        fixModel->setHeaderData (7, Qt::Horizontal, QObject::tr ("Adoption Date"));
                        fixModel->setHeaderData (8, Qt::Horizontal, QObject::tr ("Signed by"));
                        fixModel->setHeaderData (9, Qt::Horizontal, QObject::tr ("History"));
                        fixModel->setHeaderData (10, Qt::Horizontal, QObject::tr ("Age"));
                        fixModel->setHeaderData (11, Qt::Horizontal, QObject::tr ("Notes"));
                      
                      
                        ui->tableView_Fix->setColumnWidth (0, 60);
                        ui->tableView_Fix->setColumnWidth (1, 120);
                        ui->tableView_Fix->setColumnWidth (2, 150);
                        ui->tableView_Fix->setColumnWidth (3, 100);
                        ui->tableView_Fix->setColumnWidth (4, 130);
                        ui->tableView_Fix->setColumnWidth (5, 120);
                        ui->tableView_Fix->setColumnWidth (6, 400);
                        ui->tableView_Fix->setColumnWidth (7, 150);
                        ui->tableView_Fix->setColumnWidth (8, 200);
                        ui->tableView_Fix->setColumnWidth (9, 400);
                        ui->tableView_Fix->setColumnWidth (10, 130);
                        ui->tableView_Fix->setColumnWidth (11, 300);
                      
                        ui->tableView_Fix->setWordWrap (true);
                      
                      }
                      
                      void FixDb::customMenuRequested(QPoint pos) {
                        index = ui->tableView_Fix->indexAt(pos);
                      
                        QMenu* menu = new QMenu(this);
                      
                        const QIcon delIcon = QIcon::fromTheme ("delete-record", QIcon("C:/Programming/Projects/Folkfriends_bzr/checkout/icons/trash.png"));
                        QAction* delAction = new QAction(delIcon, tr("&Delete..."), this);
                        delAction->setShortcuts (QKeySequence::Delete);
                        delAction->setStatusTip ("Delete the record you clicked on.");
                        connect (delAction, &QAction::triggered, this, &FixDb::deleteRecord);
                        menu->addAction(delAction);
                      
                        menu->popup(ui->tableView_Fix->viewport()->mapToGlobal(pos));
                      }
                      
                      void FixDb::deleteRecord() {
                          
                        qDebug() << "index " << index;
                        int fixRow;
                        fixRow = index.row ();
                        qDebug() << "fixRow: " << fixRow;
                        
                       
                      }
                      
                      //end of correctFriend
                      

                      Is there a way I can use fixModel (defined in void correctFriend()) in
                      void deleteRecord()? If so, how?
                      Thank you.

                      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