PDA

View Full Version : Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)



Ion
18th September 2014, 15:04
Hi there, currently not looking to make a custom widget for this as it is only a layout class object. What I want to do is be able to create a pane/window or whatever i need so that I can place a QWidget class (that i created in code instead of on the form) inside so that I can adjust the positions etc on Qt Designer.

So need two bits of information, one what item in Qt designer should i use to allow me to place a QWidget inside it?

and two, how do i then insert my class object into this window/pane ?

Layout class constructor listed below:


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

QPalette p(palette());
p.setColor(QPalette::Background, Qt::lightGray);
this->setAutoFillBackground(true);
this->setPalette(p);
this->resize(1000,500);
this->setWindowTitle(QApplication::translate("toplevel", "CCTV"));

mplayer_wrapper *wrapper = new mplayer_wrapper(this);
label = new QLabel(this);
Timer = new QTimer(this);


button1 = new QPushButton("Stop State");
button2 = new QPushButton("Start Stream");
button3 = new QPushButton("Time");
vlayout = new QVBoxLayout();

vlayout->addWidget(wrapper);
vlayout->addWidget(button1);
vlayout->addWidget(button2);
vlayout->addWidget(button3);
vlayout->addWidget(label);

ui->centralWidget->setLayout(vlayout);

connect(button1, &QPushButton::clicked,wrapper,&mplayer_wrapper::stop_mplayer);
connect(button2, &QPushButton::clicked,wrapper,&mplayer_wrapper::start_mplayer);
//connect(button3, &QPushButton::clicked,this, &MainWindow::show_time);
connect(Timer,&QTimer::timeout,this,&MainWindow::show_time);

//ui->widget->

Timer->start();

}


Specifically in regards to this code I want to insert mplayer_wrappers object wrapper. 90% of this code I want to do on the designer as it's becoming a nightmare when the layout is alot more complicated.

wysota
18th September 2014, 15:16
The easiest way to insert custom widgets into designer forms is to use the "Promote to" functionality of Designer. Simply drag & drop a widget that is the base class of your custom widget then right click it, choose "Promote to" and follow the instructions. When C++ code is generated from the form, the stand-in you placed on the form will be replaced by your custom widget class.

west
18th September 2014, 15:18
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html

Ion
18th September 2014, 15:35
So is there no way of doing this without creating a custom widget? As seem all of your responses have been about creating a custom widget which wasn't want I wanted.

Isn't there any way I can just insert the QWidget class into the widget and tell it to fill the whole area of "widget" (say something like setting the QWidget class as a child of widget ? and telling it to fill the area ?).

anda_skoa
18th September 2014, 16:09
You can just instantiate the widget and add it to the container's layout if it has one or create the layout in code and then add the new child widget

Cheers,
_

wysota
18th September 2014, 16:11
Isn't there any way I can just insert the QWidget class into the widget and tell it to fill the whole area of "widget" (say something like setting the QWidget class as a child of widget ? and telling it to fill the area ?).

Sure there is (you can drag & drop a widget called "Widget", put it in a layout and set its size policy to expanding) but what is the point of doing that?

You said you wanted to put a widget you created in code in there, so using the Promote feature is the easiest way of doing exactly that -- you create a widget in code, call it "MyWidget" and promote a widget in the form to "MyWidget" so that when your program is built, it contains a MyWidget instance instead of whatever you placed in the form. This way you can adjust all the properties MyWidget shares with whatever you placed on the form directly in Designer.

d_stranz
19th September 2014, 05:15
I think the OP's problem might be that he hasn't set a central widget for his MainWindow class. I don't know what has been done in Qt Designer, or whether it creates a central widget by default. If the code ui->centralWidget->setLayout() works without crashing, then maybe it has (or it points to uninitialized memory).

So, modify the code by creating a plain old QWidget as a child of the MainWindow in the MainWindow constructor, set the layout on that widget, and then set that widget as the central widget for the MainWindow:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Other code as posted, except for line 30
// ... at the end of the main window constructor

QWidget * pCentralWIdget = new QWidget( this );
pCentralWidget->setLayout( vlayout );
setCentralWidget( pCentralWidget );
}

Ion
19th September 2014, 10:16
Hey d_stranz,

yea that's kind of what I wanted however using that method instead of "ui->centralwidget->setlayout" crashes the program with


QLayout: Cannot add null widget to QVBoxLayout/
QLayout: Cannot add null widget to QVBoxLayout/
The program has unexpectedly finished.

Have tried custom widgets now they are ok but I just really don't like the implementation especially considering that really these are as far from a "custom widget" as you can get, as I just need to create two Qwidgets inside another widget (with that widget being on the form, so it's easier creating the layout).

If I don't want to use custom widgets how would I go about this? So far i've created a widget on the ui form called "widget" if I want to insert the two "Qwidgets" inside this am I supposed to be just using "setparent" ? or do i need to include anything else as when I do this using set parent the "widget" is very small and doesn't fill the screen.

If someone could just show me a simple bit of code that creates a widget on the form (don't need to see this bit). Then makes this widget the parent of the two Qwidgets so that the two fill the whole of "widget", that would be great.

anda_skoa
19th September 2014, 11:16
QVBoxLayout *layout = new QVBoxLayout(ui->widget); // or whatever your container is called

QWidget *widget1 = new ....;
layout->addWidget(widget1);
QWidget *widget2 = new ....;
layout->addWidget(widget2);


Cheers,
_