一直尝试使用 qtdesigner 而不是手动创建 gui。开始使用列表视图时遇到问题

Trying to use qtdesigner instead of manually creating gui all the time. having trouble getting started with a listview

我正在努力解决一些我认为很简单但却看不到的事情。

我正在尝试开始使用 qtdesigner 进行我的 gui 设计和维护,但我在弄清楚如何从 qtdesigner 的输出中访问它们时遇到了问题。我通过 pyuic5.exe 获得了 运行 它们,并从设计师的 xml 生成了 Python 代码,并设置了一个 app.py 以将其导入 运行 我的代码来自.

这里是 app.py:

import sys
from PyQt5 import QtGui
from PyQt5.QtGui import QStandardItem, QStandardItemModel

from PyQt5.QtWidgets import (
    QApplication, QDialog, QMainWindow, QMessageBox, QListView
)

from PyQt5.QtSql import (
    QSqlDatabase, QSqlDriver, QSqlQuery
)

import PyQt5.QtCore

from PyQt5.uic import loadUi

from main_window_ui import Ui_MainWindow

class Window(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.connectSignalsSlots()
        updateTemplateList()
    def connectSignalsSlots(self):
        self.action_Exit.triggered.connect(self.close)
        #self.action_About.triggered.connect(self.about)

def updateTemplateList():
    model = QStandardItemModel()
    listView.setModel(model)

    entries = ['one','two', 'three']

    for i in entries:
        item = QStandardItem(i)
        model.appendRow(item)

    model.layoutChanged.emit()

def checkDB():
    # Try to open the connection and handle possible errors
    if not con.open():
        QMessageBox.critical(
            None,
            "App Name - Error!",
            "Database Error: %s" % con.lastError().databaseText(),
        )
        sys.exit(1)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    checkDB()
    win = Window()
    win.show()
    sys.exit(app.exec())

运行 到 pyui5.exe 之后的设计器输出是:

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

# Form implementation generated from reading ui file '.\ui\main_window_ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(309, 652)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.listView = QtWidgets.QListView(self.centralwidget)
        self.listView.setGeometry(QtCore.QRect(10, 40, 291, 521))
        self.listView.setObjectName("listView")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 570, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(210, 570, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(110, 570, 93, 28))
        self.pushButton_3.setObjectName("pushButton_3")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(60, 10, 211, 16))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 309, 26))
        self.menubar.setObjectName("menubar")
        self.menu_file = QtWidgets.QMenu(self.menubar)
        self.menu_file.setObjectName("menu_file")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.action_Exit = QtWidgets.QAction(MainWindow)
        self.action_Exit.setObjectName("action_Exit")
        self.actionAbout = QtWidgets.QAction(MainWindow)
        self.actionAbout.setObjectName("actionAbout")
        self.menu_file.addAction(self.action_Exit)
        self.menu_file.addAction(self.actionAbout)
        self.menubar.addAction(self.menu_file.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Edit"))
        self.pushButton_2.setText(_translate("MainWindow", "Delete"))
        self.pushButton_3.setText(_translate("MainWindow", "Add"))
        self.label.setText(_translate("MainWindow", "Job Data for instructions"))
        self.menu_file.setTitle(_translate("MainWindow", "&File"))
        self.action_Exit.setText(_translate("MainWindow", "&Exit"))
        self.actionAbout.setText(_translate("MainWindow", "About"))

我正在尝试使用 updateTemplateList() 更新此处的列表视图,但似乎无法到达那里。

提前感谢您的帮助:-)

主要错误是引用了不存在的对象,比如listView还没有创建,与Ui_MainWindow的listView属性不同,必须通过实例引用

import sys

from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication, QMainWindow, QListView

from main_window_ui import Ui_MainWindow


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.model = QStandardItemModel()
        self.listView.setModel(self.model)
        self.connectSignalsSlots()
        self.updateTemplateList()

    def connectSignalsSlots(self):
        self.action_Exit.triggered.connect(self.close)
        # self.action_About.triggered.connect(self.about)

    def updateTemplateList(self):
        entries = ["one", "two", "three"]
        for i in entries:
            item = QStandardItem(i)
            self.model.appendRow(item)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec())

建议:复习一下你的OOP笔记,尤其是继承、实例等概念