How to add a custom widget manually(in code) to QMainWindow
-
Hi. I have run into a probleam that I can't add my custom widget manually to QMainWindow.
I originally designed and add it to QMainWindow as this video
https://www.youtube.com/watch?v=-K-VU9I4ffYBut I now want to add my widget in code to have more flexibility. But how?
I tried
layout()->addWidget(trackEditor);//how I do that in QT4But it gave me
QMainWindowLayout::addItem: Please use the public QMainWindow API insteadhow can I do this in QT5?
Thanks
-
Welcome to DevNet!
Take a look on Qt5 widgets examples.
@MainWindow::MainWindow(...)
{
TrackEditor* trackEditor = new TrackEditor;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(trackEditor);
setLayout(layout);
}
@ -
Try like this:
@
ui->mainLayout->addWidget(trackEditor);
@Change 'mainLayout' to actually name of your layout.
-
Thanks, that works. But is there any way to add the widget without using QLayout? I prefer setting the location of my widget using x and y.
Thanks for helping.
-
bq. But is there any way to add the widget without using QLayout?
Yes, just set widget as parent. For example:
@
MyWidget *myWidget = new MyWidget(ui->mainWidget)
@But, using layouts is more properly for many reasons.