How to Add Widget after loading QtDesiger ui file?

Heads up! You've already completed this tutorial.

parkerlewis4047061 | 2021-02-06 01:34:46 UTC | #1

I'm following the post https://www.pythonguis.com/tutorials/embed-pyqtgraph-custom-widgets-qt-app/

And loading the ui works fine self.mainbox = uic.loadUi('mainwindow.ui', self)

But, how can add a button or label, to either the main window and/or my PlotWidget?
self.label = QtWidgets.QLabel()

For example, none of these work: self.mainbox.addWidget(self.label) self.mainbox.graphWidget().addWidget(self.label) self.mainbox.layout().addWidget(self.label)

How can I access either the main window's layout or my PlotWidget layout? Or Do I need instead to create a NEW layout using setCentralWidget or setLayout? vLayout = QVBoxLayout () vLayout.addWidget( loadUi( 'mainwindow.ui' ) )


martin | 2021-02-06 09:43:51 UTC | #2

1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) Book Now

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

Hi @parkerlewis4047061 welcome to the forum!

To add widgets you need to have a layout in place, and add the widget to that. In your example is mainbox a layout, or a widget? If you can post your ui file here, I can take a look at it.

If mainbox is a container QWidget you can set a layout on it using .setLayout() although this will replace any layout that is currently set (for example in your ui file).

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

You can also set a layout on a widget in Qt designer, and access that either by name or through the widget that has the layout assigned -- like in your final example self.mainbox.layout().addWidget(self.label) what error are you seeing when you do this?

The names of objects are defined in your ui file (editable within Qt Designer) -- if they're not accessible where you think they should be, check the objectName that is defined in designer to make sure it's correct.

Another option is to convert the ui file to Python (using pyuic5) -- you can then look in the resulting Python file to see how things are organised.

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!

More info Get the book


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

How to Add Widget after loading QtDesiger ui file? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.