我正在尝试连接来自两个 QTableWidget 的行选择。 我的意思是,当我在表 1 中选择一行时,我希望我的程序在表 2 中选择同一行。这两个表的列数不同,所以我不能只为第一个选择一个项目并在第二能。 我尝试使用以下但没有成功:
connect(ui->table1->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), ui->table2->selectionModel(), SLOT(setCurrentIndex(QModelIndex)));
是这样写的:
QObject::connect: No such slot QItemSelectionModel::setCurrentIndex(QModelIndex)
你知道哪里出了问题吗?
最佳答案
问题是因为setCurrentIndex()
有两个参数,而且不是只有一个,加上签名不匹配。因此,在这些情况下,您应该使用 lambda 并使用 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 ¤t, 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();
}
关于c++ - 连接 2 QTableWidget 中的行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53231320/