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. How to get the position of QTableWidget when right-clicking?
Forum Updated to NodeBB v4.3 + New Features

How to get the position of QTableWidget when right-clicking?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 628 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello, I have a QTableWidget and want to add context menu to it. I used the following code to locate the row. But during my debugging, the row always gets a value of -1. What's wrong with it?
    Subtype is the name for the QTableWidget

    QModelIndex index = ui->Subtype->indexAt(QCursor::pos());
    int row = index.row();
    
    Taz742T 1 Reply Last reply
    0
    • M MasterBlade

      Hello, I have a QTableWidget and want to add context menu to it. I used the following code to locate the row. But during my debugging, the row always gets a value of -1. What's wrong with it?
      Subtype is the name for the QTableWidget

      QModelIndex index = ui->Subtype->indexAt(QCursor::pos());
      int row = index.row();
      
      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @MasterBlade
      Why do you need row ? to get the item from tableWidget?

      If yes you can do this :

          ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
      
          connect(ui->tableWidget, &QTableWidget::customContextMenuRequested, this, [=](QPoint pos) {
              auto item = ui->tableWidget->itemAt(pos);
              if (item) {
                  //logic
              } else {
                  //logic
              }
          });
      

      Or if you want index you can do this:

      
          ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
          connect(ui->tableWidget, &QTableWidget::customContextMenuRequested, this, [=](QPoint pos) {
              auto index = ui->tableWidget->indexAt(pos);
              if (index.isValid()) {
                  qDebug() << index.row();
              }
          });
      

      Do what you want.

      1 Reply Last reply
      3

      • Login

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