2014-02-28 49 views
2

我只是爲了noob在pyqt,我已經讓我在qtdesigner desing,我只是想顯示一個簡單的矩陣到一個函數MostrarMem由pushButton調用 謝謝!我怎樣才能在qtableview中顯示矩陣與pyqt

from PyQt4 import QtCore, QtGui 

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) 

#Here is the matrix i want to show 
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(708, 557) 
     self.layoutWidget = QtGui.QWidget(Form) 
     self.layoutWidget.setGeometry(QtCore.QRect(440, 20, 258, 227)) 
     self.layoutWidget.setObjectName(_fromUtf8("layoutWidget")) 
     self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget) 
     self.verticalLayout.setMargin(0) 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.textBrowser = QtGui.QTextBrowser(self.layoutWidget) 
     self.textBrowser.setObjectName(_fromUtf8("textBrowser")) 
     self.verticalLayout.addWidget(self.textBrowser) 
     self.pushButton_3 = QtGui.QPushButton(self.layoutWidget) 
     self.pushButton_3.setObjectName(_fromUtf8("pushButton_3")) 
     self.verticalLayout.addWidget(self.pushButton_3) 
     self.layoutWidget1 = QtGui.QWidget(Form) 
     self.layoutWidget1.setGeometry(QtCore.QRect(20, 20, 361, 229)) 
     self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1")) 
     self.verticalLayout_2 = QtGui.QVBoxLayout(self.layoutWidget1) 
     self.verticalLayout_2.setMargin(0) 
     self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2")) 
     self.tableView = QtGui.QTableView(self.layoutWidget1) 
     self.tableView.setObjectName(_fromUtf8("tableView")) 
     self.verticalLayout_2.addWidget(self.tableView) 
     self.horizontalLayout = QtGui.QHBoxLayout() 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.pushButton = QtGui.QPushButton(self.layoutWidget1) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.pushButton_4 = QtGui.QPushButton(self.layoutWidget1) 
     self.pushButton_4.setObjectName(_fromUtf8("pushButton_4")) 
     self.horizontalLayout.addWidget(self.pushButton_4) 
     self.verticalLayout_2.addLayout(self.horizontalLayout) 
     self.pushButton_2 = QtGui.QPushButton(Form) 
     self.pushButton_2.setGeometry(QtCore.QRect(320, 320, 175, 27)) 
     self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) 

     self.retranslateUi(Form) 
     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.MostrarMem) 
     QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.tableView.reset) 
     #QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.slot2) 
     QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.textBrowser.clear) 
     #QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.slot3) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Form", "Form", None)) 
     self.pushButton_3.setText(_translate("Form", "PushButton", None)) 
     self.pushButton.setText(_translate("Form", "Mostrar Memoria", None)) 
     self.pushButton_4.setText(_translate("Form", "Generar Memoria", None)) 
     self.pushButton_2.setText(_translate("Form", "Limpiar", None)) 
    def MostrarMem(self): 
     #here to show matrix in tableView 

if __name__ == "__main__": 
    import sys; 
    app = QtGui.QApplication(sys.argv) 
    Form = QtGui.QWidget() 
    ui = Ui_Form() 
    ui.setupUi(Form) 
    Form.show() 
    sys.exit(app.exec_()) 

的第一個和最後意見的工作......大部分的代碼是接口...感謝

+1

我會建議使用[QTableWidget的(https://qt-project.org/doc/qt-4.8/qtablewidget.html)代替,如果你的需求很簡單。它是一個內置默認模型的QTableView。 – hackyday

回答

2

您應該使用QTableWidget

變化:self.tableView = QtGui.QTableView(self.layoutWidget1)
由:self.tableView = QtGui.QTableWidget(self.layoutWidget1)

而你的功能應該是這樣的:

def MostrarMem(self): 
     self.tableView.setRowCount(len(matrix)) 
     self.tableView.setColumnCount(len(matrix[0])) 
     for i,row in enumerate(matrix): 
      for j,val in enumerate(row): 
       self.tableView.setItem(i,j,QtGui.QTableWidgetItem(str(val))) 

在這種情況下需要使用QTableWidget,因爲您不需要提供模型來顯示數據,如果您使用QTableView,則需要設置一個模型,如果您的數據是比矩陣稍微複雜一些,但代價是幾行代碼。

您可以瞭解更多關於Qt的模型/視圖編程herehere

+0

你能解釋爲什麼QTableWidget而不是QTableView能夠理解爲什麼OP嘗試不起作用。 – Schollii