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. QTableWidget contextMenu crashing the application
Forum Updated to NodeBB v4.3 + New Features

QTableWidget contextMenu crashing the application

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 263 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 hbatalha
    #1

    I just created a context menu for my QTableWidget and it is working fine, as long as I request the menu with the mouse on a row.

    If the table is empty or if I put the mouse on a empty space inside the table this will crash my application

    //this is my connect
    connect(ui->table, &QTableWidget::customContextMenuRequested, this, &MainWindow::popupMenuTableShow);
    
    //snnipet of function
    void MainWindow::popupMenuTableShow(const QPoint &pos)
    {
        int i = ui->table->itemAt(pos)->row(); // crashes here
    
    ...
    }
    

    I understand that since I have no row selected the application is expected to crash, but I can't seem to find a solution for this.

    Any help or insight is appreciated.

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

      Hi,

      You don't check the value returned by itemAt which is going to be nullptr if the table is empty.

      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
      1
      • SGaistS SGaist

        Hi,

        You don't check the value returned by itemAt which is going to be nullptr if the table is empty.

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

        @SGaist Yeah, I went to the documentation and I just solved the problem

        void MainWindow::popupMenuTableShow(const QPoint &pos)
        {
            QTableWidgetItem *item = ui->table->itemAt(pos);
        
            if(item != nullptr)
            {
                int i = item->row();
                ...
            }
        
        ...
        }
        

        Still, thank you.

        1 Reply Last reply
        1

        • Login

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