本文主要是介绍同步两个QTableWidget列宽和选中改变,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
假设两个QTableWidget变量为:QTableWidget * pTableWndOne, pTableWndTwo;
1. 同时修改两个QTableWidget中列宽方式:
connect(pTableWndOne->horizontalHeader(), &QHeaderView::sectionResized, this, [=](int logicalIndex, int oldSize, int newSize) {if (pTableWndTwo->columnCount() < logicalIndex) { // 防止delete时崩溃return;}pTableWndTwo->setColumnWidth(logicalIndex, newSize);
});connect(pTableWndTwo->horizontalHeader(), &QHeaderView::sectionResized, this, [=](int logicalIndex, int oldSize, int newSize) {if (pTableWndOne->columnCount() < logicalIndex) { // 防止delete时崩溃return;}pTableWndOne->setColumnWidth(logicalIndex, newSize);
});
2. 同时修改两个QTableWidget中选中行
connect(pTableWndOne, &QTableWidget::itemClicked, this, [=](QTableWidgetItem *item) {if (item == nullptr) {return;}int nCurRow = item->row();int nTwoCurRow = pTableWndTwo->currentRow();if (nCurRow != nTwoCurRow) {pTableWndTwo->selectRow(nCurRow);}
});connect(pTableWndTwo, &QTableWidget::itemClicked, this, [=](QTableWidgetItem *item) {if (item == nullptr) {return;}int nCurRow = item->row();int nTwoCurRow = pTableWndOne->currentRow();if (nCurRow != nTwoCurRow) {pTableWndOne->selectRow(nCurRow);}
});
这篇关于同步两个QTableWidget列宽和选中改变的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!