gpt4 book ai didi

python - 在 QTableWidget PyQt5 中突出显示单元格

转载 作者:行者123 更新时间:2023-12-01 02:24:21 25 4
gpt4 key购买 nike

我有一个 pyqt5 QTableWidget,我想突出显示表格的特定单元格

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

我的 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)

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

最佳答案

如果您想从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_())

关于python - 在 QTableWidget PyQt5 中突出显示单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47546162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com