0

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.

panofish
  • 7,578
  • 13
  • 55
  • 96

1 Answers1

0

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
Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20
  • I should have clarified... 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. – panofish Sep 13 '14 at 15:19
  • Your can get all selected item with `list-of-QTableWidgetItem QTableWidget.selectedItems (self)`. You can check it in here. – Bandhit Suksiri Sep 13 '14 at 17:16