Issue
I am struggling with something I think is simple but just don't see it.
I am trying to start using qtdesigner for my gui design and maintenance but I have issues working out how to access them from the output of qtdesigner. I have run them through pyuic5.exe and generated Python code from the xml of designer and setup an app.py to import that into to run my code from.
here is the 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())
the designer output after run through pyui5.exe is:
# -*- 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"))
I am trying to update the listview in here with the updateTemplateList() and can not seem to get there.
thank you in advance for any help :-)
Solution
The main error is that you are referencing non-existent objects, for example listView has not been created and is different from the listView attribute of Ui_MainWindow
that must be referenced through the instance.
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())
Recommendation: Review your OOP notes, especially the concepts of inheritance, instance, etc.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.