- 论坛徽章:
- 0
|
本帖最后由 anonymous0502 于 2012-07-05 16:10 编辑
这是一段demo:- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import sys
- from PySide.QtCore import *
- from PySide.QtGui import *
- def myprint(obj, end='\n'):
- sys.stdout.write(str(obj) + end)
- class cuwindow(QWidget):
- def __init__(self):
- QWidget.__init__(self)
- self.initgui()
- self.btn_get.clicked.connect(self.test)
- def initgui(self):
- (x, y, w, h) = (500, 200, 600, 400)
- self.setGeometry(x, y, w, h)
- self.setWindowTitle('cu')
- self.btn_get = QPushButton('get', self)
- self.ltxt_reply = QLineEdit(self)
- self.btn_del = QPushButton('delete', self)
- self.view_result = QTableWidget(5, 2, self)
- self.view_result.setEditTriggers(QAbstractItemView.NoEditTriggers)
- self.view_result.setAlternatingRowColors(True)
- self.view_result.setSelectionMode(QAbstractItemView.SingleSelection)
- self.view_result.setSelectionBehavior(QAbstractItemView.SelectRows)
- self.view_result.setSortingEnabled(True)
- layout = QGridLayout()
- layout.addWidget(self.btn_get, 0, 0, 1, 1)
- layout.addWidget(self.ltxt_reply, 0, 1, 1, 1)
- layout.addWidget(self.btn_del, 0, 2, 1, 1)
- layout.addWidget(self.view_result, 1, 0, 10, 10)
- self.setLayout(layout)
- def test(self):
- ts = zip(range(5), range(5))
- rowcount = 0
- for (a, b) in ts:
- rowcount += 1
- myprint('setitem: row=%d content=%s' % (rowcount, str((a,
- b))))
- self.view_result.setItem(rowcount, 0,
- QTableWidgetItem(str(a)))
- self.view_result.setItem(rowcount, 1,
- QTableWidgetItem(str(b)))
- def main():
- app = QApplication(sys.argv)
- window = cuwindow()
- window.show()
- app.exec_()
- if __name__ == '__main__':
- main()
- # myprint('done')
复制代码![]()
为什么QTableWidget的setItem得到的结果不对?我觉得应该是会按dos窗口显示的数据分别填到表格中的啊。哪里出了问题?
PySide.QtGui.QTableWidget.setItem(row, column, item)
Parameters:
row – PySide.QtCore.int
column – PySide.QtCore.int
item – PySide.QtGui.QTableWidgetItem
Sets the item for the given row and column to item .
The table takes ownership of the item.
Note that if sorting is enabled (see PySide.QtGui.QTableView.sortingEnabled() ) and column is the current sort column, the row will be moved to the sorted position determined by item .
If you want to set several items of a particular row (say, by calling PySide.QtGui.QTableWidget.setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. PySide.QtGui.QTableWidget.setItem() will not move the row).
|
|