Qt World Summit: Register Today!

Subwindows on MainWindow

  • Hi folks!
    I am developing this new application, which is a system for medical offices. I have a doubt. I want to load the entries and queries in my main application window. Is there a way to make these sub-windows in widgets (UI's) separate and call them inside the main window? If yes, what should I do?

  • Hi,
    what exactly do you want? Seperate windows?

    QWidget *widget = new QWidget;

    and you have a new window.
    To add subwidgets to your main window you can add it to a layout like this in your mainwindow:

    // Set layout
    QHBoxLayout *layout = new QHBoxLayout;
    // Set layout in QWidget
    QWidget *widget = new QWidget();

    Anything else you want to know?


  • @AlexRoot isnt workin, i get this error:

    QWidget::setLayout: Attempting to set QLayout "" on SimpleDental "SimpleDental", which already has a layout
    The program has unexpectedly finished.

  • I did in this way:

    pacientesCad *p = new pacientesCad;
        QHBoxLayout *b = new QHBoxLayout;

    And it works, is that all correct? Well, it works on the first case, when i try load another widget, i cant, because the widget have a layout loaded, but i try this:
    But the same error above shows up.
    Any tips?

  • Hi Lays,
    when you already have a layout set, you don't need to create a new one.
    Just access your existing Layout with the layout() function.
    In general every QWidget has its own layout (there are different kinds of layouts) and you can add widgets to them.
    So if you want to add a widget to your QMainWindows layout you can call inside your main windows code something like this:
    this->layout()->addWidget(new Widget);
    For further information please read http://doc.qt.io/qt-4.8/layout.html.


Log in to reply