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 know if a row is selected in a QTableView
Forum Updated to NodeBB v4.3 + New Features

How to know if a row is selected in a QTableView

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

    hello guys, my question is simple, how can I know if a row is selected in a QTableView, this is to act according to the selection and delete the selected row, with the Quit button of my form

    Here I leave the screenshot of my form:
    c8e4da9e-1240-4895-a283-d66089a63a82-image.png

    QObject::connect(ui->btnQuit, &QPushButton::clicked,this, [&](){
          if(ui->tvUrl->model()->rowCount()==0){
    //          ui->btnQuit->setDisabled(true);
              return;
            }
          ui->tvUrl->model()->removeRow(ui->tvUrl->currentIndex().row());
          setUpTable();
        });
    

    I have this code for my Quit button, but I'm not sure if it's the best way to delete a row, thanks in advance for your feedback.

    Solitary wolf

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @lincoln said in How to know if a row is selected in a QTableView:

      how can I know if a row is selected in a QTableView,

      Depends a bit on what you mean by "selected."

      The current index (QTableView::currentIndex()), is not generally the same thing as the selection that you access through QTableView::selectionModel(). The selection can be a single cell, whole rows, or random cells.

      If you want to know which, if any, single cell has the focus then use the current index and be aware it may be a null QModelIndex (i.e. no cell is current).
      If you want to know all the cells that are selected then use the selection model.
      If you are going to delete multiple rows based on the selection then be careful what order you do it in (high to low row number) and only delete any given row once.

      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