免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4211 | 回复: 1
打印 上一主题 下一主题

pyside的qtablewidget改变单元格问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-05 16:08 |只看该作者 |倒序浏览
本帖最后由 anonymous0502 于 2012-07-05 16:10 编辑

这是一段demo:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. import sys
  4. from PySide.QtCore import *
  5. from PySide.QtGui import *


  6. def myprint(obj, end='\n'):
  7.     sys.stdout.write(str(obj) + end)


  8. class cuwindow(QWidget):

  9.     def __init__(self):
  10.         QWidget.__init__(self)
  11.         self.initgui()

  12.         self.btn_get.clicked.connect(self.test)

  13.     def initgui(self):
  14.         (x, y, w, h) = (500, 200, 600, 400)
  15.         self.setGeometry(x, y, w, h)
  16.         self.setWindowTitle('cu')

  17.         self.btn_get = QPushButton('get', self)
  18.         self.ltxt_reply = QLineEdit(self)
  19.         self.btn_del = QPushButton('delete', self)
  20.         self.view_result = QTableWidget(5, 2, self)

  21.         self.view_result.setEditTriggers(QAbstractItemView.NoEditTriggers)
  22.         self.view_result.setAlternatingRowColors(True)
  23.         self.view_result.setSelectionMode(QAbstractItemView.SingleSelection)
  24.         self.view_result.setSelectionBehavior(QAbstractItemView.SelectRows)
  25.         self.view_result.setSortingEnabled(True)

  26.         layout = QGridLayout()
  27.         layout.addWidget(self.btn_get, 0, 0, 1, 1)
  28.         layout.addWidget(self.ltxt_reply, 0, 1, 1, 1)
  29.         layout.addWidget(self.btn_del, 0, 2, 1, 1)
  30.         layout.addWidget(self.view_result, 1, 0, 10, 10)

  31.         self.setLayout(layout)

  32.     def test(self):
  33.         ts = zip(range(5), range(5))
  34.         rowcount = 0
  35.         for (a, b) in ts:
  36.             rowcount += 1
  37.             myprint('setitem: row=%d content=%s' % (rowcount, str((a,
  38.                     b))))
  39.             self.view_result.setItem(rowcount, 0,
  40.                     QTableWidgetItem(str(a)))
  41.             self.view_result.setItem(rowcount, 1,
  42.                     QTableWidgetItem(str(b)))


  43. def main():
  44.     app = QApplication(sys.argv)
  45.     window = cuwindow()
  46.     window.show()
  47.     app.exec_()


  48. if __name__ == '__main__':
  49.     main()

  50.     # 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).

论坛徽章:
0
2 [报告]
发表于 2012-07-05 16:35 |只看该作者
已解决
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. import sys
  4. from PySide.QtCore import *
  5. from PySide.QtGui import *


  6. def myprint(obj, end='\n'):
  7.     sys.stdout.write(str(obj) + end)


  8. class cuwindow(QWidget):

  9.     def __init__(self):
  10.         QWidget.__init__(self)
  11.         self.initgui()

  12.         self.btn_get.clicked.connect(self.test)

  13.     def initgui(self):
  14.         (x, y, w, h) = (500, 200, 600, 400)
  15.         self.setGeometry(x, y, w, h)
  16.         self.setWindowTitle('cu')

  17.         self.btn_get = QPushButton('get', self)
  18.         self.ltxt_reply = QLineEdit(self)
  19.         self.btn_del = QPushButton('delete', self)

  20.         #self.view_result = QTableWidget(5, 2, self)
  21.         self.view_result = QTableWidget()
  22.         self.view_result.setRowCount(5)
  23.         self.view_result.setColumnCount(2)

  24.         self.view_result.setEditTriggers(QAbstractItemView.NoEditTriggers)
  25.         self.view_result.setAlternatingRowColors(True)
  26.         self.view_result.setSelectionMode(QAbstractItemView.SingleSelection)
  27.         self.view_result.setSelectionBehavior(QAbstractItemView.SelectRows)
  28.         self.view_result.setSortingEnabled(True)

  29.         layout = QGridLayout()
  30.         layout.addWidget(self.btn_get, 0, 0, 1, 1)
  31.         layout.addWidget(self.ltxt_reply, 0, 1, 1, 1)
  32.         layout.addWidget(self.btn_del, 0, 2, 1, 1)
  33.         layout.addWidget(self.view_result, 1, 0, 10, 10)

  34.         self.setLayout(layout)

  35.     def test(self):
  36.         ts = zip(range(5), range(5))
  37.         rowcount = 0
  38.         for (a, b) in ts:
  39.             #rowcount += 1
  40.             myprint('setitem: row=%d content=%s' % (rowcount, str((a,
  41.                     b))))
  42.             self.view_result.setItem(rowcount, 0,
  43.                     QTableWidgetItem(str(a)))
  44.             self.view_result.setItem(rowcount, 1,
  45.                     QTableWidgetItem(str(b)))
  46.             rowcount += 1


  47. def main():
  48.     app = QApplication(sys.argv)
  49.     window = cuwindow()
  50.     window.show()
  51.     app.exec_()


  52. if __name__ == '__main__':
  53.     main()

  54.     # myprint('done')
复制代码
class PySide.QtGui.QTableWidget([parent=None])
class PySide.QtGui.QTableWidget(rows, columns[, parent=None])

看文档貌似可以在创建表格对象时就提供行列参数的,但是实际却不能这样使用,奇怪。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP