1

I am trying to connect row selections from two QTableWidget. I mean, when I select one row in Table 1, I want my program selects the same row in table 2. The two table dont have the same number of column so I cannot just select one item for the first and select the same item on the second able. I have tried to use the following without success:

connect(ui->table1->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), ui->table2->selectionModel(), SLOT(setCurrentIndex(QModelIndex)));

It is written:

QObject::connect: No such slot QItemSelectionModel::setCurrentIndex(QModelIndex)

Do you know what is going wrong?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
froz
  • 163
  • 1
  • 12

1 Answers1

1

The problem is caused because setCurrentIndex() has two parameters, and not just one, plus the signatures do not match. So in these cases you should use a lambda and use selectRow():

#include <QApplication>
#include <QHBoxLayout>
#include <QTableWidget>
#include <QItemSelectionModel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    auto *table1 = new QTableWidget(4, 3);
    table1->setSelectionBehavior(QAbstractItemView::SelectRows);
    auto table2 = new QTableWidget(4, 4);
    table2->setSelectionBehavior(QAbstractItemView::SelectRows);

    QObject::connect(table1->selectionModel(), &QItemSelectionModel::currentRowChanged,
                     [table2](const QModelIndex &current, const QModelIndex & previous)
    {
        if(previous.isValid())
            table2->selectRow(current.row());
    });

    QWidget w;
    auto lay = new QHBoxLayout(&w);
    lay->addWidget(table1);
    lay->addWidget(table2);
    w.show();

    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Dear eyllanesc, Thank you for your answer. I tried to adapt your code to mine without success. I get compil error:expected ',' or ']' in lambda capture list. I did the following: – froz Nov 09 '18 at 19:03
  • QObject::connect(ui->table1->selectionModel(), &QItemSelectionModel::currentRowChanged, [ui->table2](const QModelIndex &current, const QModelIndex & previous) { if(previous.isValid()){ QItemSelectionModel::SelectionFlags command = QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect; ui->table2->selectionModel()->setCurrentIndex(current, command); } }); – froz Nov 09 '18 at 19:03
  • @froz change `[ui->table2]` to `[this]` – eyllanesc Nov 09 '18 at 19:06
  • Thanks, It is working :) However, one last thing, I have in the terminal sometimes the following: `qt.accessibility.core: Cannot create accessible child interface for object: QTableWidget(0x5593ad844f30, name = "table2") index: 23` – froz Nov 09 '18 at 19:12
  • @froz that's weird, I do not have that message, maybe it's caused by the version or compilation of Qt (or the compilation of your project). If my answer helps you do not forget to mark it as correct, if you do not know how to do it then review the [tour], that is the best way to thank. – eyllanesc Nov 09 '18 at 19:15
  • @froz test compiling in release mode – eyllanesc Nov 09 '18 at 19:16
  • I think it s because the two tables do not have the same number of column – froz Nov 09 '18 at 19:18
  • @froz I have also tried with what you point out and that problem does not generate me, if so, add a validation: `if(previous.isValid() && current.row() < table2->rowCount())` – eyllanesc Nov 09 '18 at 19:21