c++ - 连接 2 QTableWidget 中的行选择

标签 c++ qt qt5 selection qtablewidget

我正在尝试连接来自两个 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 &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();
}

关于c++ - 连接 2 QTableWidget 中的行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53231320/

相关文章:

c++ - 在托管 C++ 测试中使用 TestContext.TestName

c++ - 如何在 Ubuntu 上提高我的程序磁盘读取速度?

c++ - 从文件中的字符串转换 QString

c++ - 使用 QUdpSocket 作为 QIODevice 的正确方法是什么?

c++ - Qt 从 QTextStream 读取

c++ - Qt向QMainWindow添加menuBar、菜单和子菜单

c++ - 对多个索引数据数组的面向数据的访问

c++ - 使用 std::vector<Vec8d> 是好是坏(性能方面)

c++ - QGraphicsScene/View 比例理解

c++ - Qt5:如何将 QPointer 与 Forward Declared 类一起使用