PySide6 – Qt Designer ui object has no attribute 'load_pages'

Question:

I’m trying to create a GUI with Qt Creator and PySide6. I got help from a friend, and the code works on his project, but when used in mine it doesn’t work, and I can’t figure out why. I can already tell it’s something stupid, but I’ve gotta ask.

Essentially, I have a frame with a layout called enter_search_layout – as shown here in the screenshot:

Screenshot

I then go to my code (after generating it into a python file):

import sys

from qt_pycode.ui_scoopgui import *

from qt_material import *

from PySide6 import QtWidgets

from PySide6.QtCore import *

from PySide6.QtGui import *

from PySide6.QtSvgWidgets import *

from PySide6.QtWidgets import *

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, obj=None, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)
        self.search_page_button = QtWidgets.QPushButton()
        self.load_pages.enter_search_button.addWidget(self.search_page_button, Qt.AlignCenter, Qt.AlignCenter)

app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

The error appears when I try to add a widget to my layout – self.load_pages.enter_search_button.addWidget(self.search_page_button, Qt.AlignCenter, Qt.AlignCenter).

My full error is:

AttributeError: 'MainWindow' object has no attribute 'load_pages'

Note this works on my friend’s device and we have the same directory setup. Any ideas on what could be going wrong? Maybe I’m using it wrong, or havn’t imported it properly (my friend is on Linux, so it’s a possibility). Any help would be appreciated.

Asked By: Thomas Kerby

||

Answers:

The code should just be:

self.enter_search_layout.addWidget( self.search_page_button, Qt.AlignCenter, Qt.AlignCenter)

The generated gui module always creates attributes for user-defined objects at the top level of the namespace – so enter_search_layout couldn’t ever appear as an attribute of another attribute.

Answered By: ekhumoro