PDA

View Full Version : how to include one widget in another?



aurora
2nd November 2011, 08:53
I created two widget one by command another by Qt form designer. The code as shown below. When i execute two separates forms displays....What changes do i need to do, so that one form displays inside another form as single widget?

fileopen.cpp


fileopen::fileopen(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::fileopen),
m_layout( new QVBoxLayout() )
{

QPushButton* add = new QPushButton("Add");

connect(add, SIGNAL(clicked()), this, SLOT(addRow()));

m_layout->addWidget(add);
m_layout->setAlignment(Qt::AlignTop);

QWidget* w = new QWidget();
w->setLayout(m_layout);
w->show();

ui->setupUi(this);

}

Spitfire
2nd November 2011, 09:32
You can have 'this->setCentralWidget(w);' OR 'ui->setupUi(this);', not both.

To stick new widget inside the 'ui' you could do something like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QVBoxLayout* l = new QVBoxLayout();
ui->centralWidget->setLayout(l);

QWidget* w = new QWidget();
l->addWidget(w);
}That will insert new widget inside the ui created in the designer.
That's assuming this ui is empty, if you've something inside already, just add new widget to existing layout or parent it to existing widget, ie:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget* w = new QWidget(ui->someExistingWidget);
}

aurora
2nd November 2011, 10:23
You can have 'this->setCentralWidget(w);' OR 'ui->setupUi(this);', not both.

To stick new widget inside the 'ui' you could do something like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QVBoxLayout* l = new QVBoxLayout();
ui->centralWidget->setLayout(l);

QWidget* w = new QWidget();
l->addWidget(w);
}That will insert new widget inside the ui created in the designer.
That's assuming this ui is empty, if you've something inside already, just add new widget to existing layout or parent it to existing widget, ie:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget* w = new QWidget(ui->someExistingWidget);
}


got error!
:-1: error: cannot open output file debug/FileOpen_V2.exe: Permission denied
:-1: error: collect2: ld returned 1 exit status

Spitfire
2nd November 2011, 12:02
Close the app before building it again (or kill the process if it hanged).