Context menu over editable QTableView



  • 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.


  • Lifetime Qt Champion

    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 ?



  • @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++


  • Lifetime Qt Champion

    The code is still needed



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


  • Lifetime Qt Champion

    On what widget did you add that ?

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


  • Qt Champions 2016

    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



  • @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.


  • Qt Champions 2016

    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?



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.