Important: Please read the Qt Code of Conduct -

Two mainwindows in the same instace

  • Hello :)
    I'm developing a serial monitor for a microcontroller for an academic project. I need to create a program that has 2 MainWindows. In particular when i press a button in the first window, this one closes and fire up the second one. Viceversa when i press a button on the second windows, this closes and fire up the first one.

    I tryed to do something like this using the exit codes in the main.cpp file, but i can't manage to achieve what i need.
    Can you help me?

  • Lifetime Qt Champion


    Out of curiosity, why do you need them to be both MainWindows ?

    Depending on that, you could use QStackedWidget and switch between the two widgets.

  • I think there's nothing to do with exit code...

  • @Chicowolf
    The easiest way of showing different windows is via QStackedWidget, either on its own or inside a QMainWindow.

    If you really want to have separate windows, use show() & hide(), firing them in the button slots.

    When you start speaking about "exit codes" you are talking about separate programs, which is quite a different thing. You do not want separate programs, do you?

  • Thanks for the replies.
    Yes i need separate programs. The first windows does some stuff to the micro via serial port, and the second window does other stuff over serial port too.

    I tryed to use hide() and show(), but when the first windows shows the second window, doesn't become "disabled" and permits the user to interact with the first window(for example, the user can press some buttons), even if the second window is showed, which can lead to bad beheaving of the micro if the user send some serial codes of the first window while the second windows is opened. That's because the two windows represents two different logic states of the micro.

    So i don't know if QStackedWidget is a viable option for this strange problem.

  • Lifetime Qt Champion

    But why do you need separate applications to work with the same microcontroller using the same serial port ?

Log in to reply