I'm new to Qt Designer and I'm trying to just create a really basic window to make sure everything works. When I save the window as a .ui file, and convert it to .py, everything looks good, but when I go to run it, nothing happens. I see that the file does not contain a init method, so it makes sense why it is not running, but all the tutorials that I have seen the init method is generated automatically. Also, when I view the code in the preview window in Qt Designer, it does not show an init method either. Not sure what I am doing wrong. I am using a virtual environment in PyCharm and have loaded Qt Designer as an external tool if that helps.
Asked
Active
Viewed 10 times
0
-
While the linked answer explains what is going to happen, I will strongly suggest you to *ignore* that. pyuic generates files that should **never** be directly used, nor even (*never*) modified, as they should always be imported in your main program scripts, as explained in the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). I believe that that -x flag was created just for convenience and testing, but, in fact, you should never directly launch those files for your actual programs. – musicamante Jan 15 '21 at 01:21
-
So I should be importing the UI separately and then use inheritance to create a class of the UI? – Sean Palmer Jan 15 '21 at 01:27
-
See https://stackoverflow.com/questions/46544780/qtdesigner-changes-will-be-lost-after-redesign-user-interface – eyllanesc Jan 15 '21 at 01:35
-
Precisely. You can use the single inheritance method (all the ui created within `self.ui`), but it's usually considered more comfortable to use the multiple inheritance, which makes all widgets directly members of `self`: while some may argue that it would be better to keep the widgets in a separated object (also because uic could potentially overwrite instance methods) there's rarely an actual benefit, and avoiding `self.ui` is the most suggested practice also considering that Qt is known to use very long names (having less `.ui`-s to type is just 3 characters, but it's still 3 characters. – musicamante Jan 15 '21 at 01:35
-
Also consider that there's an alternative: import the `uic` module from PyQt5, and use its `loadUi('somefile.ui', self)` just after the `__init__` of the superclass, which will dynamically load the ui file avoiding the need to constantly regenerate the files with pyuic. It will work exactly as using the multiple inheritance (all children are members of `self`, there's no `self.ui`) but you only need to inherit from the base QWidget class you're subclassing (QMainWindow, QDialog, etc) and obviously don't call `setupUi`. – musicamante Jan 15 '21 at 01:39