setSelectionMode as default is NoSelection to ignore all the selection on widget. Then connect as below code to trigger hhSelected and vhSelected slots. In those slots you just set the corresponding selectionMode and SelectionBehavior.
SO_Qt::SO_Qt(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
QHeaderView* hh = ui.tableWidget->horizontalHeader();
bool success = connect(hh, SIGNAL(sectionClicked( int )), this, SLOT(hhSelected(int)));
QHeaderView* vh = ui.tableWidget->verticalHeader();
success = connect(vh, SIGNAL(sectionClicked( int )), this, SLOT(vhSelected(int)));
}
void SO_Qt::hhSelected( int index )
{
ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectColumns);
ui.tableWidget->selectColumn(index);
}
void SO_Qt::vhSelected( int index )
{
ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tableWidget->selectRow(index);
}