How to display a widget inside a main window

19,552

Solution 1

It all depends on how you want the widget to be displayed.

  1. you could add a layout to your central widget in your MainWindow and add your custom widget to the layout
  2. if you want your custom widget to be centralWidget of the MainWindow then use setCentralWidget
  3. If you want the custom widget as a subWindow then add MdiArea to your MainWindow. Then add custom widget to you MdiArea.
  4. If you just want the custom widget to be displayed as a window then just "widget.show()"

Its better to look at Qt's examples to understand how a MainWindow is used.

Solution 2

marines::marines(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::marines)
{
    ui->setupUi(this); // after this
    iran *ir = new iran(); // create variable ir
    ir->show(); // show window
    ...
}
Share:
19,552

Related videos on Youtube

Gandalf
Author by

Gandalf

Website https://zuidelijkeoceaan.shop/ Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Nine for Mortal Men doomed to die, One for the Dark Lord on his dark throne In the Land of Mordor where the Shadows lie. One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them In the Land of Mordor where the Shadows lie.

Updated on June 04, 2022

Comments

  • Gandalf
    Gandalf almost 2 years

    I have a project marines and i have the following files structure.

    marines.pro

    FORMS

    iran.ui marines.h

    Headers

    iran.h marines.h

    Sources

    iran.cpp main.cpp marines.cpp

    I added the widget iran in the project marines.

    Here is marines.cpp

    #include <QtGui>
    #include "marines.h"
    #include "iran.h"
    
    
    marines::marines(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::marines)
    {
        ui->setupUi(this);
        connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
        connect(ui->actionIran, SIGNAL(triggered()), this, SLOT(ir()));
    }
    
    void marines::ir()
    {
    //slot to display iran ui inside my main window
    }
    
    marines::~marines()
    {
        delete ui;
    }
    

    and here is my iran.cpp

    #include "iran.h"
    #include <QtGui>
    
    iran::iran(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::iran)
    {
        ui->setupUi(this);
    }
    
    iran::~iran()
    {
        delete ui;
    }
    

    How can i display the widget iran i made in qt designer?.

  • Gandalf
    Gandalf over 12 years
    Thanks for the options. Could you explain some more option 4.
  • Chenna V
    Chenna V over 12 years
    well, in the constructor of marines call iran->show() as andrei suggested or in iran constructor you could just call show() or whereever you create iran object call show() on it
  • Rachael
    Rachael over 9 years
    if I may add, I believe option 4 would require *parent = 0 in the constructor (which means it is not a child of any other widgets and will be created as a window)