Prompt Ui Is Not Loaded Hence Fail Upload File

Qt for Python (i.e. PySide2) was appear recently and got me interested in trying to utilise information technology to load upward a UI file. If you don't know, PyQt and PySide / PySide2 can utilize the Qt Creator awarding to create user interfaces using a drag-and-drop interface. This is actually very like to the way you would create an interface using Visual Studio. Qt Creator / Designer will generate an XML file with a *.ui extension that y'all tin can then load within of your PySide2 application (or PyQt).


Creating the User Interface

For this example, I opened up Qt Creator and went to File --> "New File or Project". And so I chose the "Qt Widgets Application" option. See screenshot beneath:

And then I opened up the mainwindow.ui that Qt Creator made for me. You can just double-click it or click on the Blueprint push that should be on the left-hand side of the program. Here'south a screenshot that might help:

I added three widgets to my UI:

  • QLabel
  • QLineEdit
  • QPushButton

When I saved the file, I got the following in my UI file:

                                      MainWindow                                                                    0                    0                    400                    300                                                                    MainWindow                                                                                                                    160                        210                        fourscore                        25                                                                                    OK                                                                                                                          130                        30                        113                        25                                                                                                                                                20                        30                        111                        17                                                                                    Favorite Language:                                                                                                                            TopToolBarArea                                                        false                                                                                                                                                                                                                  

Now we only need to acquire how to load this file in Qt for Python.


Loading the UI File in Qt for Python

In that location are a couple of different ways that I discovered I could use to load the UI file in Qt for Python (PySide2). The first is taken pretty much wholesale from Qt for Python'south wiki:

import sys  from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication from PySide2.QtCore import QFile   if __name__ == "__main__":     app = QApplication(sys.argv)     file = QFile("mainwindow.ui")     file.open up(QFile.ReadOnly)     loader = QUiLoader()     window = loader.load(file)     window.testify()     sys.get out(app.exec_())          

While this works, it doesn't actually show you how to hook upward events or get anything useful out of the user interface. Bluntly, I thought this was kind of a dumb example. So I looked up some other examples on other websites and somewhen put the post-obit together:

import sys  from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication, QPushButton, QLineEdit from PySide2.QtCore import QFile, QObject  form Class(QObject):          def __init__(cocky, ui_file, parent=None):         super(Form, self).__init__(parent)         ui_file = QFile(ui_file)         ui_file.open(QFile.ReadOnly)                  loader = QUiLoader()         self.window = loader.load(ui_file)         ui_file.close()                  self.line = self.window.findChild(QLineEdit, 'lineEdit')                  btn = cocky.window.findChild(QPushButton, 'pushButton')         btn.clicked.connect(self.ok_handler)         cocky.window.show()      def ok_handler(cocky):         language = 'None' if not cocky.line.text() else self.line.text()         print('Favorite language: {}'.format(language))  if __name__ == '__main__':     app = QApplication(sys.argv)     form = Grade('mainwindow.ui')     sys.get out(app.exec_())          

In this example, we create a Course object and load upwardly the UI file using QUiLoader and QFile. Then we use the findChild method on the object that was returned from QUiLoader to extract the widget objects that we are interested in. For this instance, I extracted the QLineEdit widget and then I could get what the employ entered and the QPushButton widget so I could catch the button clicked result. Finally I continued the click upshot to an event handler (or slot).


Wrapping Upwards

At this point y'all should take a general thought of how you might create a user interface using Qt's Creator / Designer application and and so how to load up the UI file it generates into your Python application. It is fairly straight-frontwards, only not exactly well documented. Yous can also just skip creating the UI file and code everything by hand too. Regardless of the method you choose, this does announced to be a fairly easy toolkit to go into, although I am hoping the documentation / tutorials increase in item presently.


Related Reading

  • Qt for Python Ui Files wiki page
  • PySide Recipes
  • StackOverflow - Loading QtDesigner'south .ui files in PySide
  • PyQt4'due south folio on Qt Designer

pereiraheyes1944.blogspot.com

Source: https://www.blog.pythonlibrary.org/2018/05/30/loading-ui-files-in-qt-for-python/

0 Response to "Prompt Ui Is Not Loaded Hence Fail Upload File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel