PDA

View Full Version : Child widget problem



poporacer
3rd August 2010, 05:58
I am trying to lear QT creator and have searched and read the forum and several areas but I must be missing something. But first I have a question. I am writing a program that will have a main window and then widgets (dialog forms) that I want to display in the main window. I read about stacked widgets but am concerned that if all the widgets are loaded then this could create a resource issue. Is that thought process correct? I am able to create the forms and have the button open the second form. The problem is that the form does not open as a child of the main window. I am using QT Creator 1.2.1 based on QT 4.5.2. What am I missing? Here is the code!

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;

private slots:
void on_btnNextPage_clicked();

};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "page2.h"

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

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void MainWindow::on_btnNextPage_clicked()
{

Page2* page2 = new Page2 () ;
page2->exec();
}



page2.h

#ifndef PAGE2_H
#define PAGE2_H

#include <QDialog>

namespace Ui {
class Page2;
}

class Page2 : public QDialog
{
Q_OBJECT

public:
explicit Page2(QWidget *parent = 0);
~Page2();

protected:
void changeEvent(QEvent *e);

private:
Ui::Page2 *ui;
};

#endif // PAGE2_H


page2.cpp

#include "page2.h"
#include "ui_page2.h"

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

Page2::~Page2()
{
delete ui;
}

void Page2::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

Thanks for your assistance

Rick

Lykurg
3rd August 2010, 06:13
Ok, do you want to use QStackedWidget? then your code is absolutely wrong:) add your widget in the constructor to the stackwidget and in the slot change the current index of your stackwidget. If you don't want use a stack widget, then you have to add your widget to the layout of your main class. Furthermore it is better to create the widget with a parent, so better always use
Page2* page2 = new Page2 (this) ;

And normally QStackedWidget wont be a resource problem.

poporacer
3rd August 2010, 06:20
I didn't use a stacked widget because of my concerns with resources as several widgets will be connected to databases, so I was going to load and unload each widget as needed. Which way is better?

I think I am missing something because I tried

1.Page2* page2 = new Page2 (this) ;
and Page2 still opened as its own window, not a window within mainwindow. What did I miss?
Thanks for taking your time!
Rick

Lykurg
3rd August 2010, 06:57
As told, you have to put that widget into the layout of your mainwindow. (See layout() to get it, or use setCentralWidget(), but that depends on your needs)

poporacer
3rd August 2010, 14:52
I am not sure how to add the widget into the layout of my window dynamically. I looked at the QLayout* QWidget::layout()const class and couldn't figure it out. I think I might need to place some sort of placeholder widget in the mainwindow widget and then somehow load and unload the widgets as needed. could you give me a little more information? Thanks
Rick