PDA

View Full Version : New window doesn't open even though I initialize QMainWindow parent class



Cheems
17th January 2021, 20:11
Hello there!

I'm currently working on a project for my resumé that is supposed to be a GUI based login system. It's been going pretty well until now. I want to open a new window for the LoggedIn class after you have successfully logged in but it doesn't open a new window and I don't for the life of me get why. Please excuse my awful code, I haven't taken the time to tidy things up but here it is:

https://github.com/FlatEarthGary/login/blob/main/main.py

Any help as well suggestions on improvements is highly appreciated, thank you and have a great day!

d_stranz
17th January 2021, 21:31
There is too much code there to spend the time to understand it all. However, both of your top-level window classes seem to be derived from QMainWindow (which is strange), and you have another window class that is derived from main, which is derived from QMainWindow (doubly strange).

I think you are confusing QMainWindow for a generic top-level QWidget, which can be any widget without a parent.

A more typical scenario would be to have the initial window be a QDialog which is used to log in the user after validation. The slot that handles the login validation should create the single QMainWindow instance that represents your app's GUI, and then show() it before closing itself.

Cheems
18th January 2021, 13:40
There is too much code there to spend the time to understand it all. However, both of your top-level window classes seem to be derived from QMainWindow (which is strange), and you have another window class that is derived from main, which is derived from QMainWindow (doubly strange).

I think you are confusing QMainWindow for a generic top-level QWidget, which can be any widget without a parent.

A more typical scenario would be to have the initial window be a QDialog which is used to log in the user after validation. The slot that handles the login validation should create the single QMainWindow instance that represents your app's GUI, and then show() it before closing itself.

Hey, thank you for your reply! I really appreciate you taking time out of your day to help me out! :)

You have given me quite a bit to take look at, that's great, thank you! If I have understood this correct you're only supposed to have one instance of the QMainWindow and all other should rely on QWidget or QDialog?

d_stranz
18th January 2021, 16:53
If I have understood this correct you're only supposed to have one instance of the QMainWindow and all other should rely on QWidget or QDialog?

A typical desktop app will have only one QMainWindow-based class that holds the entire UI. All other widgets will be children of that and most will be children of whatever is defined as the "central widget" except for some modal dialogs and the decorations around the QMainWindow (menus, toolbars, dock widgets, etc.

That is why in almost all of the Qt examples, the C++ main() entry point consists of the same few lines of code: create the QApplication instance, create the main window instance, show() the main window, and exec() the app. The rest of the UI is a tree of widgets that hang off the main window as children, children of children, and so on.

For a case like your login scenario, you could go one of two ways: (1) Have a login dialog appear before the main window is shown to validate the user, and if successful launch the main window from that, or (2) follow the traditional approach of showing the main window, but then immediately post the login dialog. If login is successful, close the dialog, otherwise the dialog calls the main window's close() slot. It is probably easier to take the second approach.