Problem in children windows switching

  • I wanna make 3 windows: MainWindow, profile, config, and could switch from "MainWindow" to "profile", and from "profile" to "config" using pushbottons ( the real project is an user manu, have more windows, but work like this. )

    my way is: in class MainWindow, there're two private member pointers, in Profile and Config type, like this:

    // in mainwindow.h
    class MainWindow : public QMainWindow
    Ui::MainWindow *ui;
    Profile *profile;
    Config *config;
    In MainWindow's constructor, realize the two children, and pass "this" pointer to be their parent:
    // in MainWindow.cpp
    // in MainWindow's constructor

    profile = new Profile(this);
    config = new Config(this);

    and,in the corresponding slot in the MainWindow, hide the MainWindow and show the Profile window:
    void MainWindow::on_pushButton_profile_clicked()
    it works fine.

    my problem is, do now know how to jump from "Profile" window to the "Config" window, because do not know how to reach the "config" class in the "profile" class. They are all members of the MainWindow, so MainWindow know them, but I do not know how let them access each other.

    I think there could be some way like this:
    // in profile.cpp

    void Profile::on_pushButton_config_clicked( )
    this->parent->config->show(); /* fake code /
    this->parent->config->exec(); /
    fake code */
    the usage of "parent" is wrong,I just use this way to express my meaning。

    please give me some advice, thanks.

    and, I appreciate for any some example code of menu,


    Thanks a lot.

    [edit] code wrappings added, koahnig

  • Why not add a signal to profile dialog such that when config button is pressed it emits it to a lot in main and it hides profile and shows config

  • Moderators

    welcome to devnet

    Please use "code wrappings": next time. This makes your code section better readable.

  • DBoosalis:

    good idea! I'll try this way.


    Thanks for your remind!

  • another probelm met:

    now I use MainWindow to control the child's switch, when child B want switch to child C, it emit a signal to the MainWindow, and the MainWindow does:


    and when child C want to switch to child B, the MainWindow does:


    After successfully switch from MainWindow to B, and from B to C, now the problem is, when want switch back to C from B, it directly jump to MainWindow, with warning as following:

    Dialog::exec() recursive call

    seems that I abused the exec() in my code.

    What's the way out? thanks.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.