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. [solved]How to get options on right click of mouse in a tableview

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

Scheduled Pinned Locked Moved General and Desktop
tableviewmouse control
6 Posts 2 Posters 4.2k 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.
  • R Offline
    R Offline
    Ratzz
    wrote on 9 Jul 2015, 08:28 last edited by Ratzz
    #1

    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 ?

    --Alles ist gut.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Jul 2015, 21:24 last edited by
      #2

      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.

      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
      0
      • R Offline
        R Offline
        Ratzz
        wrote on 10 Jul 2015, 04:42 last edited by
        #3

        @SGaist said:

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

        how to use postion when execulting context menu ?

        --Alles ist gut.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ratzz
          wrote on 10 Jul 2015, 12:09 last edited by
          #4

          I added below code to make it worked .

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

          --Alles ist gut.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 11 Jul 2015, 21:01 last edited by
            #5

            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 :)

            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
            0
            • R Offline
              R Offline
              Ratzz
              wrote on 13 Jul 2015, 04:39 last edited by
              #6

              @SGaist
              Thank you.

              --Alles ist gut.

              1 Reply Last reply
              0

              3/6

              10 Jul 2015, 04:42

              • Login

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