Issue
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_()
Solution
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.
Answered By - sateesh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.