PDA

View Full Version : Adding Ui created widget into manually created main widget



snaqvi
7th April 2013, 19:34
I have a main widget derived from QWidget which I designed by manually coding. I was wondering if there is a way to add QtDesigner widget (ui) class to the main widget using addWidget function, or some other means. When I try to do addWidget in the MyMainWindow() function the program compiles fine, but the ui widget does not display. Thanks.


class MyMainWindow: public QWidget
{
Q_OBJECT
public:
int count;
MyCurve *c;
inputs *in; //ui designed widget
matrixVector A;
QHBoxLayout *layout;
QLineEdit *value;
QLineEdit *g1;
QLineEdit *g2;

MyMainWindow();
QMenu *fileMenu;
QMenuBar *menuBar;
QAction *openAction;
QAction *saveAction;


};

anda_skoa
7th April 2013, 22:36
The class generated from the .ui file is not a widget itself, it contains setup code for a widget's content.

So what you do is create a QWidget subclass, have a member of the type of the generated class, and call its setupUi( this ) in the widget subclass' constructor.

As a very, very, dirty hack you could also just instantiate a QWidget instance and call the UI class setupUi method passing that widget instance.

Cheers,
_

wysota
7th April 2013, 22:37
Designer created widgets are regular widgets that adhere to every rule code-created widgets adhere to. Which means you can use it any way you want. Just remember that what Designer creates is not really a widget, but rather a class that needs to be deployed on a widget by calling setupUi().

snaqvi
7th April 2013, 23:53
Thanks for the replies. I do have the setupUi(this) in the constructor.

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

In the mainWidget constructor , I do
in=new inputs(this);
QHBoxLayout *lh= new QHBoxLayout;
lv->addWidget(logo);
lh->addWidget(in);

But nothing shows on the screen. Is there something else needed?
Thanks.

wysota
8th April 2013, 05:19
You need to tell the layout what it is supposed to operate on (e.g. pass "this" to its constructor).