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. QTableView::selectionChanged connect with C++11 lambdas
Forum Updated to NodeBB v4.3 + New Features

QTableView::selectionChanged connect with C++11 lambdas

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 3.0k 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    In order to catch the QTableView:::selectionChnaged signal I would like to use connect with C++11 lambdas. I tried the following:

    connect(ui->tableView, QOverload<QItemSelection &, QItemSelection &>::of(ui->tableView->selectionChanged()),
                [=](QItemSelection &selected, QItemSelection &deselected) { on_selectionChanged(selected, deselected); });
    

    but I get the following error from the compiler:

    .../widgetaddress.cpp:32: Fehler: invalid use of non-static member function ‘virtual void QTableView::selectionChanged(const QItemSelection&, const QItemSelection&)’
    32 | connect(ui->tableView, QOverload<QItemSelection &, QItemSelection &>::of(ui->tableView->selectionChanged),

    If I use the old connect style like this:

    connect(
         ui->tableView->selectionModel(),
         SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
         SLOT(on_selectionChanged(const QItemSelection &, const QItemSelection &))
        );
    

    it is working.

    What am I doing wrong here?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You're trying to connect to the view, while the signal is emitted by the selection model (as in your macro version). You're also missing consts and the QOverload is not needed:

      connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
              [=](const QItemSelection &selected, const QItemSelection &deselected) { on_selectionChanged(selected, deselected); });
      

      Btw. I would strongly advise to add a third parameter to the connect to scope the life of the connection. Also this lambda seems unnecessary, as you can do just

      connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &YourClassName::on_selectionChanged);
      
      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        No need to use QOverload at all here since there is no other selectionChanged() function except selectionChanged(const QItemSelection &selected, const QItemSelection &deselected). But you're trying to use selectionChanged(QItemSelection &selected, QItemSelection &deselected) which does not exist.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • I Offline
          I Offline
          Infinity
          wrote on last edited by Infinity
          #4

          Thanks.

          What is the cleanest way to get the value of the selected item? I did it like this:

          int selectedRow = ui->tableView->selectionModel()->currentIndex().row();
          qDebug() << "Selected Row:" << selectedRow;
          
          QModelIndex addressIdIndex = m_addressModel->index(selectedRow, 0, QModelIndex());
          QVariant addressId = m_addressModel->data(addressIdIndex, Qt::DisplayRole);
          
          qDebug() << "addressId:" << addressId.toString();
          

          Is there a cleaner way to get the addressId?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            Current item and selected item are not the same, so don't use current item for selection.
            In a single selection model you can get selected value like this:

            if (ui->tableView->selectionModel()->hasSelection()) {
                QVariant addressId = ui->tableView->selectionModel()->selectedRows().first().data();
            }
            
            1 Reply Last reply
            3
            • I Offline
              I Offline
              Infinity
              wrote on last edited by
              #6

              Great Thanks.

              How can I set the model to single selection? At the same time I would also to set the tableView that only one row can be selected and not single cells. How can I achieve this?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @infinity said in QTableView::selectionChanged connect with C++11 lambdas:

                How can I set the model to single selection?

                By taking a look into the documentation: https://doc.qt.io/qt-5/qabstractitemview.html#selectionBehavior-prop

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2

                • Login

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