Can't add widget created by Designer
-
Hello,
I've created a widget using Designer (Right Click -> Add new... -> Qt Designer Form)
It's name is Fuel_widgetnow I'm trying to use it in main window but nothing shows up :(
here is the code
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_fuel_widget.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QWidget *f = new QWidget; Ui::Fuel_widget u; u.setupUi(f);
// This code shows me something look like that i want but I think it's a bad way to do things
ui->gridLayout->addWidget(u.verticalLayoutWidget);
// And this one shows nothing
// ui->gridLayout->addWidget(f);
}@
I'm a total newbie with Qt so I think the fix will be quite simple.
Help :) -
Try f.show();
-
f->show(); shows my widget as different window.
and does only if I didn't added my widget to any of layouts. -
You should read the handbook carefully. Usually we can create a class which derived from QWidget or QMainWindow,or something. And define a private member variable Ui::Fuel_Widget *ui. Then use it.
Fortunately, the Qt Creator will help you make all things to be done.Or you can try UiLoader to use a single UI file.
-
Run Qt Creator, Use menu "New file or Projects"
A dialog will show out( choose a template)
Choose "Qt" under "Files and Classes" at left
Choose "Qt Designer Form Class" .... -
bq. Run Qt Creator, Use menu “New file or Projects”
A dialog will show out( choose a template)
Choose “Qt” under “Files and Classes” at left
Choose “Qt Designer Form Class” ….I've made that.
Now trying to use created class with
@
Fuel_widget *f = new Fuel_widget;
ui->verticalLayout->addWidget(f);
@but nothing happens again :(
-
bq. You should read the handbook carefully. Usually we can create a class which derived from QWidget or QMainWindow,or something. And define a private member variable Ui::Fuel_Widget *ui. Then use it.
Fortunately, the Qt Creator will help you make all things to be done.I've tried that but this doesn't work just like previous variant :(
-
Are you trying to create a multi or single document interface? What I'm asking is do you want your widget to be created in a new window within your mainwindow? If yes, then you need to use an MDI.
@
QMdiArea *myArea = new QMdiArea;
Fuel_widget *f = new Fuel_widget;
QMdiSubWindow *myWidgetSubWindow = myArea->addSubWindow(f);
@ -
bq. Are you trying to create a multi or single document interface?
no, I'm just trying to draw something I created with QT Designer within my main window couple times. That is why I made widget and trying to add it to main window.
Am I doing it wrong? -
Did you set the widget as the main windows central widget using QMainWindow::setCentralWidget()?
-
yes, there is such code in 'ui_mainwindow.h' (which is generated by Designer) and it is run when I make mainwindow object (in constructor ui->setupUi(this))
why does it matter?
-
I would recommend reading the "mainwindow documention":http://developer.qt.nokia.com/doc/qt-4.8/QMainWindow.html, and some beginner Qt material - there is a ton available online.
This "thread":http://developer.qt.nokia.com/forums/viewthread/8779 might prove useful as well.