Выделите строки и столбцы в QTableWidget, сохраняя выделение

У меня есть QTableWidget, который я настроил так, что вы не можете выбирать ячейки, но можете выбирать строки / столбцы по их заголовкам. Проблема, с которой я сталкиваюсь, заключается в том, что когда я выбираю строку, она отменяет выбор любых выбранных столбцов, и то же самое для столбцов / строк. Я хочу иметь возможность выбирать строки с поведением ExtendedSelection и столбцы с поведением SingleSelection, но независимо друг от друга. Вот что я делаю:

ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
connect(ui->tableWidget->horizontalHeader(),SIGNAL(sectionClicked(int)), this,SLOT(horizontalHeaderClicked(int)));
connect(ui->tableWidget->verticalHeader(),SIGNAL(sectionClicked(int)), this,SLOT(verticalHeaderClicked(int)));

Затем:

void MatrixWidget::horizontalHeaderClicked(int column){
   if(column <= 0) return; //first column is names, doesn't represent a segment

   ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
   ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectColumns);
   ui->tableWidget->selectColumn(column);
   ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

}

void MatrixWidget::verticalHeaderClicked(int row){
   ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
   ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
   ui->tableWidget->selectRow(row);
   ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

}

Ответы на вопрос(1)

Ваш ответ на вопрос