Reputation: 1022
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.
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_()
Upvotes: 4
Views: 6679
Reputation: 1022
Hmm. Had spent much time looking for that answer, started putting the question together, happened across the answer, and decided to post the question anyway and then answer it, for others, then found out I had an 8 hour waiting period before I could, and meanwhile two other fine answers. Anyway, this was mine:
item.text()
... will show you the content of each cell (for any selected rows):
for item in self.myTable.selectedItems():
print item.text()
...prints:
[email protected]
Some One
3E0B001E
You can also cherry pick the content of a particular cell, for example, here's one way:
self.myTable.selectedItems()[1].text()
(Unfortunately the loop above reads downward by successive columns instead of row by row so will have to find a way to deal with that)
Upvotes: 2
Reputation: 5
for item in self.myTable.selectedItems():
if item.column() == 0: print (item.text())
output:
[email protected]
[email protected]
Upvotes: 1
Reputation: 28713
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.
Upvotes: 4
Reputation: 17246
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()
Upvotes: 3