[solved]How to get options on right click of mouse in a tableview



  • I have a tableview set to a QStandardItemModel . I have 6 rows and 4 column in it . each cell has "item 0" by default. When an user select some cell on the mouse i want some options to be displayed on the right click of the mouse.
    mymodel.cpp

    mymodel::mymodel(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::mymodel)
    {
        ui->setupUi(this);
        moniterFilterModel = new QStandardItemModel(6,4,this);
        ui->tableView->setModel(moniterFilterModel);
        for(int row = 0; row < 6; row++)
        {
            for(int col = 0; col < 4; col++)
            {
                QModelIndex index= moniterFilterModel->index(row,col,QModelIndex());
                moniterFilterModel->setData(index,"Item 0") ;
            }
        }
        connect(ui->tableView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(MonitorFilterContextMenu(QPoint)));
            ui->tableView->setMouseTracking(true);
            ui->tableView->viewport()->setAttribute(Qt::WA_Hover,true);
    
    void mymodel::MonitorFilterContextMenu(QPoint position)
    {
        Q_UNUSED(position);
        QAction *pTxAction  = new QAction("Change to Tx",this);
        connect(pTxAction,SIGNAL(triggered()),this,SLOT(ChangeSelectedToTx()));
    
        QAction *pRxAction  = new QAction("Change to Rx",this);
        connect(pRxAction,SIGNAL(triggered()),this,SLOT(ChangeSelectedToRx()));
    
        QAction *pTRAction  = new QAction("Change to TR",this);
        connect(pTRAction,SIGNAL(triggered()),this,SLOT(ChangeSelectedToTR()));
    
        QAction *pDisableAction  = new QAction("Disable Monitoring",this);
        connect(pDisableAction,SIGNAL(triggered()),this,SLOT(DisableMonitoringForSelectedItems()));
    
        QMenu *pContextMenu = new QMenu( this);
        pContextMenu->addAction(pTRAction);
        pContextMenu->addAction(pTxAction);
        pContextMenu->addAction(pRxAction);
        pContextMenu->addAction(pDisableAction);
        pContextMenu->exec(QCursor::pos());
    }
    
    void mymodel::ChangeSelectedToTR()
    {
        QModelIndexList selectedItemList = ui->tableView->selectionModel()->selectedIndexes();
        foreach(QModelIndex itemIndex, selectedItemList)
        {
            moniterFilterModel->setData(itemIndex, QVariant("TR"));
        }
    }
    
    void mymodel::ChangeSelectedToTx()
    {
        QModelIndexList selectedItemList = ui->tableView->selectionModel()->selectedIndexes();
        foreach(QModelIndex itemIndex, selectedItemList)
        {
            moniterFilterModel->setData(itemIndex, QVariant("T"));
        }
    }
    
    void mymodel::ChangeSelectedToRx()
    {
        QModelIndexList selectedItemList = ui->tableView->selectionModel()->selectedIndexes();
        foreach(QModelIndex itemIndex, selectedItemList)
        {
            moniterFilterModel->setData(itemIndex, QVariant("R"));    }
    }
    
    void mymodel::DisableMonitoringForSelectedItems()
    {
        QModelIndexList selectedItemList = ui->tableView->selectionModel()->selectedIndexes();
        foreach(QModelIndex itemIndex, selectedItemList)
        {
            moniterFilterModel->setData(itemIndex, QVariant(".."));
        }
    }
    
    what am i missing ?

  • Lifetime Qt Champion

    Hi,

    What problem are you experiencing ? By the way, why don't you use position when executing your context menu ?

    On a side note, you are currently leaking memory, each time your slot is called, you recreate everything.



  • @SGaist said:

    why don't you use position when executing your context menu ?

    how to use postion when execulting context menu ?



  • I added below code to make it worked .

        ui->TableView->setContextMenuPolicy(Qt::CustomContextMenu);
    

  • Lifetime Qt Champion

    For the position thing, just use it in place of calling QCursor::pos()

    Glad you found out, don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)



  • @SGaist
    Thank you.


Log in to reply
 

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