Load QDialog directly from UI-File?

2024/9/8 8:25:58

I work with QT Designer and create my GUIs with it. To launch the main program, I use this code:

import sys
from PyQt4 import uic, QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *try:_fromUtf8 = QtCore.QString.fromUtf8except AttributeError:def _fromUtf8(s):return stry:_encoding = QtGui.QApplication.UnicodeUTF8def _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)def main():app = QtGui.QApplication(sys.argv)myWindow = MyWindowClass()myWindow.show()app.exec_()main_dialog = uic.loadUiType("GUI.ui")[0]class MyWindowClass(QtGui.QMainWindow, main_dialog):def __init__(self, parent=None):QtGui.QMainWindow.__init__(self, parent)self.setupUi(self)if __name__ == "__main__": main()

So with this line main_dialog = uic.loadUiType("GUI.ui")[0] I define my created UI-File.

Now when I work with QDialogs I have only accomplished to run them by first creating them, then converting them to Python code (using PYUIC4), then implementing the code in my main python file and run the QDialog this way:

def NameOfDialog(self):dialog = Qdialog()            dialog.ui = NameOfDialogClass()dialog.ui.setupUi(dialog)dialog.exec_()

The obvious problem is that whenever I make any tiny change to the GUI I have to go through the entire process again (converting and putting the code in the main code and watch out to not delete any other lines that I added and need to maintain).

I am sure there is a solution to also refer to the UI File of the QDialog directly, but how? I tried the same method like I do for the main window but without success :(

Thanks!

EDIT:

Here is what I have tried in a minimal example, but it's not working. What am I missing?

#-*- encoding: UTF-8 -*-
import sys
from PyQt4 import uic, QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *try:_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:def _fromUtf8(s):return stry:_encoding = QtGui.QApplication.UnicodeUTF8def _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)def main():app = QtGui.QApplication(sys.argv)myWindow = MyWindowClass()myWindow.show()app.exec_()main_dialog = uic.loadUiType("GUI.ui")[0]TestQDialog = uic.loadUiType("Dialog.ui")[0]class QDialogClass(object, TestQDialog):def __init__(self, parent=None):QtGui.QDialog.__init__(self, parent)self.setupUi(self)class MyWindowClass(QtGui.QMainWindow, main_dialog):def __init__(self, parent=None):QtGui.QMainWindow.__init__(self, parent)self.setupUi(self)self.btn_close.clicked.connect(self.dialog)def dialog(self):dialog = Qdialog()dialog.ui = QDialogClass()dialog.ui.setupUi(dialog)dialog.exec_()if __name__ == "__main__":main()
Answer

Your dialog class should be defined in exactly the same way as your main-window class. I obviously can't test it myself, but the script should look like this:

import sys
from PyQt4 import uic, QtGui, QtCoremain_dialog = uic.loadUiType("GUI.ui")[0]    
TestQDialog = uic.loadUiType("Dialog.ui")[0]class QDialogClass(QtGui.QDialog, TestQDialog):def __init__(self, parent=None):QtGui.QDialog.__init__(self, parent)self.setupUi(self)class MyWindowClass(QtGui.QMainWindow, main_dialog):def __init__(self, parent=None):QtGui.QMainWindow.__init__(self, parent)self.setupUi(self)self.btn_close.clicked.connect(self.dialog)def dialog(self):dialog = QDialogClass()dialog.exec_()def main():app = QtGui.QApplication(sys.argv)myWindow = MyWindowClass()myWindow.show()app.exec_()if __name__ == "__main__":  main()
https://en.xdnf.cn/q/73070.html

Related Q&A

Is there a way to detect if running code is being executed inside a context manager?

As the title states, is there a way to do something like this:def call_back():if called inside context:print("running in context")else:print("called outside context")And this would …

Adding title to the column of subplot below suptitle

Is there a simple way to add in to my original code so that I can add another title to both column of my subplot? for example like somewhere in the pink region shown in the picture below.Someone refer…

Conditional Inheritance based on arguments in Python

Being new to OOP, I wanted to know if there is any way of inheriting one of multiple classes based on how the child class is called in Python. The reason I am trying to do this is because I have multip…

Slice endpoints invisibly truncated

>>> class Potato(object): ... def __getslice__(self, start, stop): ... print start, stop ... >>> sys.maxint 9223372036854775807 >>> x = sys.maxint + 69 >…

Selenium Webdriver with Java vs. Python

Im wondering what the pros and cons are of using Selenium Webdriver with the python bindings versus Java. So far, it seems like going the java route has much better documentation. Other than that, it s…

asyncio - how many coroutines?

I have been struggling for a few days now with a python application where I am expecting to look for a file or files in a folder and iterate through the each file and each record in it and create objec…

Calculating a 3D gradient with unevenly spaced points

I currently have a volume spanned by a few million every unevenly spaced particles and each particle has an attribute (potential, for those who are curious) that I want to calculate the local force (ac…

deleting every nth element from a list in python 2.7

I have been given a task to create a code for. The task is as follows:You are the captain of a sailing vessel and you and your crew havebeen captured by pirates. The pirate captain has all of you stand…

Bradley-Roth Adaptive Thresholding Algorithm - How do I get better performance?

I have the following code for image thresholding, using the Bradley-Roth image thresholding method. from PIL import Image import copy import time def bradley_threshold(image, threshold=75, windowsize=5…

How to display all images in a directory with flask [duplicate]

This question already has answers here:Reference template variable within Jinja expression(1 answer)Link to Flask static files with url_for(2 answers)Closed 6 years ago.I am trying to display all image…