突出显示QTableWidget PyQt5中的Cell

时间:2017-11-29 06:09:32

标签: python python-3.x pyqt pyqt5 qtablewidget

我有一个pyqt5 QTableWidget,我想高手表的特定单元格

例如我给第2行和第2列,我想要突出显示该单元格进行编辑,我的意思是蓝色元素

我的表

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)

在方法中,转到我要突出显示单元格的位置

2 个答案:

答案 0 :(得分:1)

如果您要从QTableWidget中选择项目,则必须使用setRangeSelected方法,这要求选择的范围(在您的情况下为相应的行和列),然后我显示一个例子:

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_())

答案 1 :(得分:0)

如果您需要突出显示单个单元格,我发现使用setItemSelected()方法会更方便:您要做的就是传递项目并设置布尔值(不需要QTableWidgetSelectionRange和坐标)。 / p>