Qt add widget to main window

I am new to Qt. I took an example from here http://doc.qt.io/qt-5/qtmultimediawidgets-player-example.html. Now I want to integrate the player in the main window. I created a Qt Widgets application project, I thought, that I would just have to edit the main window code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Player* player;
    MainWindow::setCentralWidget(player);

}

But it doesn't work and I get the following error:

Starting /home/***/Documents/build-player-Desktop-Debug/player... The program has unexpectedly finished.

/home/***/Documents/build-player-Desktop-Debug/player crashed

How can I integrate a custom widget which is written in code, without ui in a main window? Thank you in advance.

asked May 17, 2015 at 19:29

Qt add widget to main window

In your own MainWindow class you can add a widget to the layout of that MainWindow:

MyMainWindow::MyMainWindow(QWidget *parent) :
    ...
{
    this->ui->setupUi(this);

    QLabel *myLabel = new QLabel();

    this->layout()->addWidget(myLabel);
}

answered Oct 26, 2018 at 22:40

Qt add widget to main window

goulashsoupgoulashsoup

2,3782 gold badges32 silver badges57 bronze badges

1

Well, player can't be placed on the window if it is not initialized. Write something like that :

Player *player = new Player();

answered May 17, 2015 at 19:34

La masseLa masse

1,1601 gold badge8 silver badges24 bronze badges

I usually add a QWidget (or whatever widget type I'm extending) to my .ui-file in the designer and then promote it to the actual derived type. See the Qt docs for more info on promoting widgets. This means that I can set the base widget's properties and design the window as usual but still get an instance of my special class when the UI is instantiated.

answered May 17, 2015 at 20:13

user408952user408952

8629 silver badges22 bronze badges

MainWindow:MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    SomeStupidWidget *ssw = new SomeStupidWidget(this); /* important! don't forget about passing "this" as argument, otherwise this could cause a memory leak(Qt handles object's lifetime by means of it's "owner" mechanism)*/

    layout()->addWidget(ssw);
}

answered Dec 9, 2019 at 20:51

Qt add widget to main window

Not the answer you're looking for? Browse other questions tagged c++ qt qwidget qmainwindow or ask your own question.

I think I've created a circular dependency in my code, but I'm not sure if it really is circular dependency, and if it's even the correct way to do things.

I know that it is possible to design a UI that can be instantiated in another UI. For example, a widget with a button that dynamically adds new buttons to the widget when pressed.

I've struggled to replicate this in source/ manually until today. But the way I've designed it confuses me somewhat because it seems like two widgets depend upon each other circularly.

The following are the steps that lead to the circular dependency I'm referring to:

  1. In MainWindow::setup(), I create the containerWidget followed by the inCl class object which takes containerWidget as a param.
  2. In customComboClass::setup I basically set the layout to the containerWidget.
  3. Back in MainWindow::setup() I add containerWidget to mainLayout.
  4. Then I add inCl to containerWidget's layout. This is where I see the circle complete.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVBoxLayout>
#include <QDebug>

#include "customcomboclass.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    void setup();

    customComboClass *inCl;

    QPushButton *call_button;
    QVBoxLayout *mainLayout;
    QWidget *containerWidget;
    QWidget *mainWidget;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setup();
}

MainWindow::~MainWindow()
{
}

void MainWindow::setup(){
    containerWidget = new QWidget(this);// Container widget for customComboClass widget object. Holds elements of inheriting class.
    inCl = new customComboClass(containerWidget);// Inheriting class object. Send containing widget as param.

    call_button = new QPushButton(this);
    call_button->setText("Make a Call");

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(call_button);
    mainLayout->addWidget(containerWidget);// Add containing widget to layout.
    containerWidget->layout()->addWidget(inCl);// Add inheritng class object to containing widget. Fill containing widget with elements from inheriting class object.

    mainWidget = new QWidget();
    mainWidget->setLayout(mainLayout);
    mainWidget->setMinimumSize(120,100);
    setCentralWidget(mainWidget);
}

customcomboclass.h

#ifndef CUSTOMCOMBOCLASS_H
#define CUSTOMCOMBOCLASS_H

#include <QWidget>
#include <QDebug>
#include <QListWidget>
#include <QListWidgetItem>
#include <QHBoxLayout>
#include <QPushButton>
#include <QMessageBox>

class customComboClass : public QWidget
{
    Q_OBJECT

public:
    explicit customComboClass(QWidget *w, QWidget *parent = nullptr);
    ~customComboClass();

public slots:
    void setup(QWidget *w);

private:
    QStringList itemList = {"", "OFF", "Circuit", "Radio"};
    QPushButton *button;
    QListWidget *listWidget;
    QHBoxLayout *hLayout;
};

#endif // CUSTOMCOMBOCLASS_H

customcomboclass.cpp

#include "customcomboclass.h"
#include "ui_customcomboclass.h"

customComboClass::customComboClass(QWidget *tWidget, QWidget *parent) :
    QWidget(parent)
{
    setup(tWidget);
}

customComboClass::~customComboClass()
{
}

void customComboClass::setup(QWidget *w){
    button = new QPushButton();
    button->setText("Press Me");
    button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    listWidget = new QListWidget();
    listWidget->addItems(itemList);
    listWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    listWidget->setMaximumHeight(25);

    hLayout = new QHBoxLayout();
    hLayout->addWidget(listWidget);
    hLayout->addWidget(button);

    w->setLayout(hLayout);
}



Main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

How do I add a custom widget to Qt?

Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.

What is the main window in Qt?

A main window provides a framework for building an application's user interface. Qt has QMainWindow and its related classes for main window management. QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar.