0

I'm still new to pyqt and trying to learn different things.

What I want is to create a custom Qt widget (using the Qt Creator) and have the result as a python (pyqt) class (I called it 'myCustomWidget') in its own module. Then when I want to add it under the parent widget 'parent_widget' just call myCustomWidget, something along the lines of self.my_widget = myCustomWidget(parent_widget)

IMPORTANT - myCustomWidget is an elaborate widget that contains several nested frames and buttons in various layouts.

IMPORTANT 2 - myCustomWidget is meant to be used an an 'overlay', a floating piece of UI that is not connected to any layout, so in my case the parent_widget is actually centralwidget.

What I tried I created a widgets application on Qt Creator, built the UI with it, Then convert the ui file into a python file. That py file starts with the following code:

class myCustomWidget(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1024, 768)
        MainWindow.setStyleSheet("")
        self.my_frame = QtWidgets.QWidget(MainWindow)
        self.my_frame.setStyleSheet(...
        ...

(Note how it has no 'init' method but a setupUi method. That confused me a lot)

then I replaced it to be the following:

class myCustomWidget(QWidget):
    def __init__(self, parent):
        super(myCustomWidget, self).__init__(parent)
        self.my_frame = QtWidgets.QWidget(parent)
        self.my_frame.setStyleSheet("...
        ...

And then used it:

self.my_widget = myCustomWidget(centralwidget)
self.my_widget.move(QPoint(500, 500)) # Just to see where it gets and how to interact with it.

The problem is, of course, that none of it work. I do see on the top left corner of my main window, a tiny something that obviously has the components of myCustomWidget only without the proportions.

Thanks for your help!

tagseman
  • 21
  • 4
  • See the docs for the [uic module](https://www.riverbankcomputing.com/static/Docs/PyQt5/api/uic/uic-module.html), and also the pyqt examples shown here: [Using Qt Designer](http://pyqt.sourceforge.net/Docs/PyQt5/designer.html#using-the-generated-code). – ekhumoro Jan 11 '22 at 18:59
  • @ekhumoro Thanks! I did learn from the links you posted that it's best to leave the generated code untouched and create a different class to manage and use the generated code (or just the .ui file) as the main window. but I wanted to know how to use myCustomWidget in a different widget/app and haven't found anything like it. – tagseman Jan 12 '22 at 15:57
  • The duiplicate questions I linked to show exactly how to do that with full code examples. There's really no mystery here. Just create a subclass and use it like any other widget class. It's only four lines of code. – ekhumoro Jan 12 '22 at 18:00

0 Answers0