【问题标题】:how can i show a Matrix in qtableview with pyqt如何使用 pyqt 在 qtableview 中显示矩阵
【发布时间】:2014-03-31 22:43:23
【问题描述】:

我只是在 pyqt 上新手,我已经在 qtdesigner 进行了设计,我只想在 qtableview 中显示一个简单的矩阵,并使用由 pushButton 调用的函数 MostrarMem 谢谢!!

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_())

第一个和最后一个工作的 cmets...大部分代码是接口...谢谢

【问题讨论】:

  • 如果您的需求很简单,我建议您改用QTableWidget。它是一个内置默认模型的 QTableView。

标签: python python-2.7 pyqt pyqt4


【解决方案1】:

你应该使用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,您还需要设置一个模型,如果您的data 比矩阵稍微复杂一点,但代价是多行代码。

你可以阅读更多关于Qt的模型/视图编程herehere

【讨论】:

  • 你能解释一下为什么 QTableWidget 而不是 QTableView 有助于理解为什么 OP 尝试不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多