4

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?

okorkut
  • 491
  • 2
  • 12

1 Answers1

1

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

ncica
  • 7,015
  • 1
  • 15
  • 37