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. Select QTableWidget row when right-clicked
Forum Updated to NodeBB v4.3 + New Features

Select QTableWidget row when right-clicked

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.3k Views 2 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I want to know if there is a way to select a QTableWidget row only when you right-click on it that shows a context menu and then deselect it after context menu disappears.
    I want the table to keep the table setSelectionMode to QAbstractItemView::NoSelection and changes it when I customContextMenuRequested and when that is done it goes back to NoSelection mode.

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

      Hi,

      One thing that comes to mind is to interact with the selection model. When you right click, use it directly (not tested though).

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

      H 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        One thing that comes to mind is to interact with the selection model. When you right click, use it directly (not tested though).

        H Offline
        H Offline
        hbatalha
        wrote on last edited by hbatalha
        #3

        @SGaist

        I solved the problem by doing this:

        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
                connect(ui->table, &QTableWidget::customContextMenuRequested, this, &MainWindow::popupMenuTableShow);
        
            ui->table->setSelectionMode(QAbstractItemView::NoSelection);
            ui->table->setFocusPolicy(Qt::NoFocus);
            ui->table->setSelectionBehavior(QTableWidget::SelectRows);
            ui->table->setContextMenuPolicy(Qt::CustomContextMenu);
        
        }
        
        
        //the function
        void MainWindow::popupMenuTableShow(const QPoint &pos)
        {
            QTableWidgetItem *item = ui->table->itemAt(pos);
                                  
                if(item != nullptr)
                {    
                    int i = item->row();    
            
                    ui->table->setSelectionMode(QAbstractItemView::SingleSelection);    
                    ui->table->selectRow(i);
                
                    QMenu* contextMenu = new QMenu(this);
                    contextMenu->deleteLater(); 
            
                    connect(contextMenu, &QMenu::aboutToHide, [this]()
                    {
                        ui->table->selectionModel()->clear();
            
                        ui->table->setSelectionMode(QAbstractItemView::NoSelection);
                    });
        
        //                ...the rest of the function
        
                  }
        
        }
        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