okorkut
okorkut

Reputation: 521

Select QTableWidget rows only by clicking row header

I have a QTableWidget. I have set its selection behaviour with

myTableWidget.setSelectionBehaviour(QTableWidget.SelectRows)

Now I can select rows by clicking the cells of the table. But I want to be able to select rows only by clicking the vertical header labels. How can I accomplish that?

Upvotes: 5

Views: 1927

Answers (1)

ncica
ncica

Reputation: 7206

Class Qt.ItemFlag

This enum describes the properties of an item. Note that checkable items need to be given both a suitable set of flags and an initial state, indicating whether the item is checked or not. This is handled automatically for model/view components, but needs to be explicitly set for instances of QTableWidgetItem.

in this case I will use(for all items set flag):

Qt::ItemIsSelectable - It can be selected.

    self.dlg.tableWidget.setRowCount(3)
    self.dlg.tableWidget.setColumnCount(4)
    self.dlg.tableWidget.horizontalHeader().sectionPressed.disconnect() # disconnect horizontal header

    data = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

    for row in range(3):  # add items from array to QTableWidget
        for column in range(4):
            item = QTableWidgetItem(str(data[row][column]))  # each item is a QTableWidgetItem
            item.setFlags(Qt.ItemIsSelectable) # set flag to the item
            self.dlg.tableWidget.setItem(row, column, item)

enter image description here

NOTE: the row will be selected just if you click on the verticalHeader

Upvotes: 1

Related Questions