Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom widget to the main window in Qt Creator

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.

like image 620
Rareform Avatar asked Dec 04 '25 16:12

Rareform


2 Answers

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);
}
like image 116
goulashsoup Avatar answered Dec 06 '25 06:12

goulashsoup


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

Player *player = new Player();
like image 33
La masse Avatar answered Dec 06 '25 07:12

La masse