i'm just to noob at pyqt, i've made my desing at qtdesigner and i just want to display a simple matrix into qtableview with a function MostrarMem called by pushButton THANKS!!
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_())
The First and the last comments to work... mostly of code is the interface... thanks
You should use a QTableWidget:
change: self.tableView = QtGui.QTableView(self.layoutWidget1)
by: self.tableView = QtGui.QTableWidget(self.layoutWidget1)
And your function should be like:
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)))
The use of a QTableWidget is desired on this cases because you don't need to provide a model to show your data, if you use a QTableView you need to setup a model too which may be desirable if your data is a little more complex than a matrix but at the cost of a few more lines of code.
You can read more about Qt's model/view programming here and here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With