2016-04-30 47 views
0

從簡單字典中讀取數據d = {key:['desc',dateadded]} 目標是將數據添加到表視圖中,並按列表中的日期排序。嘗試將表放在主窗口上時,無法獲取任何數據添加。在tableview中動態添加和排序python

問題在於我如何將表放置在主窗口上。我有一個帶表的工作小部件,現在正試圖組合這兩個文件。目前該應用程序崩潰。不要你寫代碼,但在正確的方向推動將是一個很大的幫助。

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'display.ui' 
# 
# Created by: PyQt4 UI code generator 4.11.4 
# 
# WARNING! All changes made in this file will be lost! 
from PyQt4.QtGui import (QMainWindow, QApplication) 
from PyQt4 import QtCore, QtGui 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
import sys 
try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_MainWindow(object): 
    def __init__(self): 
     #app = QApplication(sys.argv) 
     self.window = QMainWindow() 
     self.setupUi(self.window) 
     self.window.show() 
     self.setmydata() 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(296, 478) 
     MainWindow.setIconSize(QtCore.QSize(0, 0)) 
     MainWindow.setAnimated(False) 
     MainWindow.setTabShape(QtGui.QTabWidget.Rounded) 

     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.tableView = QtGui.QTableView(self.centralwidget) 
     self.tableView.setGeometry(QtCore.QRect(20, 50, 261, 391)) 
     sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.tableView.sizePolicy().hasHeightForWidth()) 
     self.tableView.setSizePolicy(sizePolicy) 
     self.tableView.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers) 
     self.tableView.setDragEnabled(True) 
     self.tableView.setAlternatingRowColors(True) 
     self.tableView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection) 
     self.tableView.setSortingEnabled(False) 
     self.tableView.setObjectName(_fromUtf8("tableView")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(20, 10, 121, 31)) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.pushButton_2 = QtGui.QPushButton(self.centralwidget) 
     self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 121, 31)) 
     self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 296, 21)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     MainWindow.setStatusBar(self.statusbar) 

     self.retranslateUi(MainWindow) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def setmydata(self): 
     d={"12345678":["do some stuff here that needs to be in a large cell to wrap text","1"],"12343378":["do stuff","1"]} 


     self.data = {} 

     keys=d.keys() 
     self.data['col1']=keys 
     self.data['col2']=[] 
     for key in keys: 
       self.data['col2'].append(d[key][0]) 
     horHeaders = [] 

     for n, key in enumerate(sorted(self.data.keys())): 
      horHeaders.append(key) 
      for m, item in enumerate(self.data[key]): 
       newitem = QTableWidgetItem(item) 
       self.setItem(m, n, newitem) 
     self.setHorizontalHeaderLabels(horHeaders) 
    def retranslateUi(self, MainWindow): 
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) 
     self.pushButton.setText(_translate("MainWindow", "Add New", None)) 
     self.pushButton_2.setText(_translate("MainWindow", "Save", None)) 

app = QApplication(sys.argv) 
frame = Ui_MainWindow() 
#frame.show() 
app.exec_() 

回答

0

通過代碼挖掘後,我發現QTableWidget的,而不是一個視圖需要線

self.tableView = QtGui.QTableView(self.centralwidget) 

。這允許我設置值等。