Important: Please read the Qt Code of Conduct -

Class does not name a type?

  • I have an application where I will be switching back and forth between windows. I will need to keep the object states the same (i.e cannot declare a new instance of a class every time I go to a new window) so I have decided to use a class where my objects will remain global variables. The structure of my program is like this:

    class 1: selection screen where the user will input settings that the main screen must use to display the appropriate data.
    class 2: controller where I declare the two classes as public variables. I use the variables declared here in both classes.
    class 3: main screen where I can go back to class 1 view and change the settings
    main: creates instance of class 2 which shows class 1 and hides class 3.

    Is this sustainable and how do I organize the #include and forward declarations so I don't end up with a cyclic dependency? I keep getting the same errors that have to do with 'x' not naming a type, which I am attributing to poor understanding of how to handle multiple classes and global variables. Any help understanding would be much appreciated!

  • Lifetime Qt Champion


    If you already circular dependencies with the setup you describe, then you have an architecture problem.

    Do you really need class 2 ?

    From what it looks like, you have a main widget that you want to show, then from that main widget, you need to present a settings dialog. The settings dialog doesn't need any particular knowledge of that "main widget".

    What else is it supposed to do ?

  • @SGaist Thanks for your reply! My main window needs to have knowledge of the user input that happens at the settings window. My main window's state must be updated from the settings, but it has a list that will be added to so it needs to remember the previous list members that were added as well. Can I declare them as global? How do I declare the dependencies between them if so?

    The current code I have looks like this when I switch between the windows:

    void settingsClass::buttonClicked()
    interfaceClass* interface = new interface;
    QString n = group->checkedButton()->text();

    Creating a new class every time I click the button removes the memory of the previous list elements. I figured having the class as global and declared somewhere else would remove this issue.

  • Lifetime Qt Champion

    You should rather implement getters in your settings widget that your main widget will use to get whatever settings you modified. That way, your settings dialog doesn't need to know or care about the widget that will use it.

    As for global variables, most of the time, they are, again, signs of bad architecture.

  • @SGaist Thanks! How would I implement "showing" and "hiding" my two views if I want to be able to switch between them without creating a new instance of the class everytime I call that function? Would this be a case where I would need to declare the object globally?

  • Lifetime Qt Champion

    No, you would hide, your main widget before showing your settings widget and show it again once you're done with the settings widget.

  • @SGaist Thanks for your reply! I ended up creating some global variables for the settings I needed to "remember" and just added to a global list. My issue was solved so thanks for your help!

Log in to reply