Search code examples
pythonpython-3.xpyqtpyqt5qtablewidget

Highlight Cell in a QTableWidget PyQt5


I have a pyqt5 QTableWidget and i want to highight a specific cell of the table

For example I give row 2 and column 2, and I want that cell to be highlighted to edit,i mean for the element in blue

My Table

def createTable(self):
   # Create table
    self.tableWidget = QTableWidget()
    self.tableWidget.setRowCount(4096)
    self.tableWidget.setColumnCount(16)
    horizantalLabels = []
    verticalLabels = []
    for i in range(16):
        horizantalLabels.append(funciones.tohex(i,8))
    for i in range(4096):
        verticalLabels.append(funciones.tohex(i*16,16))
    self.tableWidget.setHorizontalHeaderLabels(horizantalLabels)
    self.tableWidget.setVerticalHeaderLabels(verticalLabels)
    # table selection change6
    for i in range(4096):
        for j in range(16):
            self.tableWidget.setItem(i, j, QTableWidgetItem("00"))
            self.tableWidget.setColumnWidth(j, 29)

    self.tableWidget.setItemDelegate(HexDelegate())
    self.tableWidget.cellChanged.connect(self.changed)

def goToPosition(self):
    position = self.goTo.text()
    self.tableWidget.scrollToBottom()
    #print(position)
    lenght = len(position)
    if lenght > 3:
        position_Row = position[:3]
        position_Column = position[3:]
        print(position_Row)
        print(position_Column)
        position_Row = int(position_Row, 16)
        position_Column = int(position_Column,16)
        print("row " + str(position_Row))
        positionCell = self.tableWidget.item(position_Row, position_Column)
        self.tableWidget.scrollToItem(positionCell)

in the method, go to position i want to highlight the cell


Solution

  • If you want to select items from a QTableWidget you must use the setRangeSelected method, this requires that the range of the selection, in your case would be the respective row and column, then I show an example:

    class Widget(QWidget):
        def __init__(self, *args, **kwargs):
            QWidget.__init__(self, *args, **kwargs)
            hlayout = QHBoxLayout()
            self.rbox = QSpinBox(self)
            self.cbox = QSpinBox(self)
            hlayout.addWidget(self.rbox)
            hlayout.addWidget(self.cbox)
            vlayout = QVBoxLayout(self)
            vlayout.addLayout(hlayout)
    
            nrows = 5
            ncols = 5
            self.rbox.setMaximum(nrows-1)
            self.cbox.setMaximum(ncols-1)
    
            self.table = QTableWidget(nrows, ncols, self)
            vlayout.addWidget(self.table)
            for r in range(nrows):
                for c in range(nrows):
                    it = QTableWidgetItem("{}-{}".format(r, c))
                    self.table.setItem(r, c, it)
    
            self.rbox.valueChanged.connect(self.selectItem)
            self.cbox.valueChanged.connect(self.selectItem)
            self.selectItem()
    
        def selectItem(self):
            self.table.clearSelection()
            x = self.rbox.value()
            y = self.cbox.value()
            self.table.setRangeSelected(QTableWidgetSelectionRange(x, y, x, y), True)
    
    if __name__ == '__main__':
        import sys
    
        app = QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())