In my Windows C++ application, I am creating a table of type QTableWidget. The first visible cell in each row is the checkbox. The problem is that if there is a widget in a cell, I cannot highlight the entire row when clicking on any cell. All cells are highlighted, except for the cell with the checkbox. I need to highlight the entire row including the cell with the checkbox
Here is my table code, where I fill in the table header and create checkboxes in each row of the table.
void myClass::ShowTable(void) {
ui.tableWidget->setColumnCount(ui_columns_positions.size());
ui.tableWidget->setRowCount(device_table_.dev_list.size());
for (const auto& column : ui_columns_names) {
ui.tableWidget->setHorizontalHeaderItem(ui_columns_positions.at(column.first), new QTableWidgetItem(QString::fromStdString(column.second)));
}
ui.tableWidget->setColumnHidden(ui_columns_positions.at(dev_table_columns::Id), true);
for (int i = 0; i < device_table_.dev_list.size(); ++i) {
const device dev = device_table_.dev_list[i];
const QString id = QString::number(dev.Id);
const QString name = QString::fromStdString(dev.name);
//............................................
auto* check_box = new QCheckBox();
check_box->setChecked(dev.is_active);
connect(check_box, &QCheckBox::stateChanged, this, [i, this](int state) {
switch (state) {
case Qt::CheckState::Checked:
device_table_.dev_list[i].is_active = true;
break;
case Qt::CheckState::Unchecked:
device_table_.dev_list[i].is_active = false;
break;
}
});
ui.tableWidget->setCellWidget(i, ui_columns_positions.at(lnb_table_columns::IsActive), check_box);
ui.tableWidget->setItem(i, ui_columns_positions.at(lnb_table_columns::Id), new QTableWidgetItem(id));
ui.tableWidget->setItem(i, ui_columns_positions.at(lnb_table_columns::Name), new QTableWidgetItem(name));
//............................................
}
ui.tableWidget->verticalHeader()->setVisible(false);
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tableWidget->resizeColumnsToContents();
ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
ui.tableWidget->resizeRowsToContents();
}
I've tried binding different click event handlers to a table cell to ensure that the entire row is selected, including the cell with the checkbox:
connect(ui.tableWidget, &QTableWidget::cellClicked, this, [=](int row, int column) {
// Выделяем всю строку
for (int col = 0; col < ui.tableWidget->columnCount(); ++col) {
QTableWidgetItem* item = ui.tableWidget->item(row, col);
if (item) {
item->setSelected(true);
}
}
});
However, nothing happened. It always turns out a cell with a checkbox is not highlighted, as in the figure:
I also tried different styles to simulate the highlighting effect for the entire line:
QString styleSheet = "QTableWidget::item:selected {"
"background-color: #a8c2db;" // Highlight color for table cell
"}"
"QTableWidget::item:selected QCheckBox {"
"background-color: transparent;" // Transparent background for the checkbox in the selected cell
"}"
"QTableWidget::item:selected QCheckBox::indicator {"
"background-color: transparent;" // Transparent background for the checkbox indicator in the selected cell
"border: none;" // Remove the border of the checkbox indicator
"}"
"QTableWidget::item:selected QCheckBox:checked {"
"background-color: transparent;" // Transparent background for checked checkbox in selected cell
"}";
ui.tableWidget->setStyleSheet(styleSheet);
But this also proved unsuccessful. Tell me, how can I highlight the entire row of the table, including the cell with the checkbox, when clicking on any cell