简体   繁体   中英

Get cell highlight for specfic row & column in QTableWidget using Python pyqt4

I am using Python and pyqt4. My QTableWidget allows multiple cell selections by the user. For a single specific row & column cell in a QTableWidget, I need to read whether that cell has been highlighted by user selection or not highlighted. What is the pyqt call to read a cell's selection/highlight value?

EDIT: I need to do it without a signal/slot call. I simply have a QTableWidget and a row and column number and I need to get if that cell is selected or not.

Use list-of-QTableWidgetItem QTableWidget.selectedItems (self) to get selected item from user selection or highlighted. And handle signal void itemSelectionChanged () to doing when item selection changed;

import sys
from PyQt4 import QtGui

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parentQWidget = None):
        super(QCustomTableWidget, self).__init__(parentQWidget)
        self.setColumnCount(2)
        self.setRowCount(3)
        self.setItem(0, 0, QtGui.QTableWidgetItem('Test 1'))
        self.setItem(0, 1, QtGui.QTableWidgetItem('Test 2'))
        self.setItem(1, 0, QtGui.QTableWidgetItem('Work 1'))
        self.setItem(1, 1, QtGui.QTableWidgetItem('Work 2'))
        self.setItem(2, 0, QtGui.QTableWidgetItem('Area 1'))
        self.setItem(2, 1, QtGui.QTableWidgetItem('Area 2'))
        self.itemSelectionChanged.connect(self.itemSelectionChangedCallback)

    def itemSelectionChangedCallback (self):
        print '#' * 80
        for currentQTableWidgetItem in self.selectedItems():
            print currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text()

if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQCustomTableWidget = QCustomTableWidget()
    myQCustomTableWidget.show()
    sys.exit(myQApplication.exec_())

EDIT: I need to do it without a signal/slot call. I simply have a QTableWidget and a row and column number and I need to get if that cell is selected or not.

In PyQt4 only have method bool QTableWidget.isItemSelected (self, QTableWidgetItem item) available to used. If you want to use by index of row & column, You have to create new method your own. Use Use list-of-QTableWidgetItem QTableWidget.selectedItems (self) for check row and column is match;

class QCustomTableWidget (QtGui.QTableWidget):
    .
    .
    .

    def isItemSelectedByIndex (self, row, column):
        isSelection = False
        for currentQTableWidgetItem in self.selectedItems():
            if (row, column) == (currentQTableWidgetItem.row(), currentQTableWidgetItem.column()):
                isSelection = True
                break
        return isSelection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM