Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract content, result of selectedItems()

The code below produces a little dialog box like the one shown, where the rows of the table can be selected (CTRL key for multiple select, or toggle on/off). Then clicking the 'Ok' button, the content of the selections are available using selectedItems(). The problem is that each 'item' looks like this: <PySide.QtGui.QTableWidgetItem object at 0x00FF0558>.

After selectedItems(), how is the content extracted?

The documentation at http://www.pyside.org/docs/pyside/PySide/QtGui/QTableWidget does not say.

Example of the dialog box, result of the code

from PySide import QtGui, QtCore

class A_Dialog(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(A_Dialog, self).__init__(parent)
        self.setupUi(self)

    def setupUi(self, MainWindow):
        self.buttonBox_ok_cancel = QtGui.QDialogButtonBox(MainWindow)
        self.buttonBox_ok_cancel.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        QtCore.QObject.connect(self.buttonBox_ok_cancel, QtCore.SIGNAL("accepted()"), self.button_ok)
        QtCore.QObject.connect(self.buttonBox_ok_cancel, QtCore.SIGNAL("rejected()"), self.button_cancel)

        content = {
            1: [   '[email protected]',
                   'Some One',
                   '3E0B001E'
                ],
            2: [   '[email protected]',
                   'Some Else',
                   '6C8EAA39',
                ],
         }

        # Table for content
        self.myTable = QtGui.QTableWidget(0, 3)
        self.myTable.setHorizontalHeaderLabels(['Email','Name','ID'])
        self.myTable.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.myTable.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.myTable.horizontalHeader().setStretchLastSection(True)
        self.myTable.verticalHeader().setDefaultSectionSize(18)                 # Vertical height of rows
        self.myTable.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed)

        # Populate the cells
        for k in content.keys():
            self.myTable.insertRow( k-1 )
            c = 0
            for z in content[k]:
                self.myTable.setItem( k-1, c, QtGui.QTableWidgetItem( z ) )
                c += 1

        # Auto-size
        self.myTable.resizeColumnsToContents()

        # A little padding on the right for each column, some room to breath, pixels.
        padding = 12
        for col in range(len(content[1])):
            current_column_width = self.myTable.columnWidth(col)
            self.myTable.setColumnWidth( col, current_column_width + padding )

        self.myTable.setWordWrap(False)
        self.myTable.setShowGrid(False)
        self.myTable.setSortingEnabled(True)
        self.myTable.setDragDropOverwriteMode(False)
        self.myTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

        horizontalLayout = QtGui.QHBoxLayout()
        horizontalLayout.addWidget(self.buttonBox_ok_cancel)
        verticalLayout = QtGui.QVBoxLayout()
        verticalLayout.addWidget(self.myTable)
        verticalLayout.addLayout(horizontalLayout)
        widget = QtGui.QWidget()
        widget.setLayout(verticalLayout)
        self.setCentralWidget(widget)

    def button_ok(self):
        for item in self.myTable.selectedItems():
        #for item in self.myTable.selectedIndexes():
            print "MMM", item
        self.close()

    def button_cancel(self):
        self.close()

    def closeEvent(self, e):
        e.accept()

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = A_Dialog()
    window.show()
    app.exec_()
like image 647
gseattle Avatar asked Sep 16 '25 20:09

gseattle


2 Answers

As the other answer mentioned you can use .text() method of the QTableWidgetItem to get the content.

As is evident pyside documentation doesn't give indication about type of items returned making it harder to figure out which methods to make use of. In such scenarios it is useful to check Qt class documentation itself to get more details about the methods that would be available. For example in your case you can look at Qt docs for QTableWidget which makes it easier to find which methods to call.

like image 80
sateesh Avatar answered Sep 18 '25 22:09

sateesh


Call the .text() method on each QTableWidgetItem. The single argument to the QtGui.QTableWidgetItem constructor is this same value.

More info here: PySide.QtGui.QTableWidgetItem.text()

like image 25
David K. Hess Avatar answered Sep 18 '25 22:09

David K. Hess