PDA

View Full Version : Child widgets



poporacer
8th August 2010, 22:59
I am trying to learn QT creator and have searched and read the forum and several other areas but I must be missing something. I am writing a program that will have a main window and then widgets (dialog forms) that I want to open and close inside the main window. I am able to create the forms and have a button open the second form. The problem is that the form does not open as a child of the main window. I think I might need to create some sort of container in the main form and pass a pointer to that container? I am lost at how to do this. 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(); //Identify a pointer here? Buy how?

};



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* page2 = new Page2 (this) ; ??
page2->exec();
}

norobro
9th August 2010, 01:22
You can't call QDialog::exec() and do what you want. From the docs (http://doc.qt.nokia.com/4.6/qdialog.html#details):
A dialog window is a top-level window ...
...
...if it has a parent, its default location is centered on top of the parent's top-level widget ...
Change your slot to this:
void MainWindow::on_btnNextPage_clicked()
{

Page2* page2 = new Page2 () ; // Page2* page2 = new Page2 (this) ; ??
setCentralWidget(page2);
}
The mainWindow takes ownership of the widget so you don't need a this pointer.

The widget that was previously the central widget will be deleted so you will save on memory but if you should want to reload any previous widget you will have to poll your database again and re-instantiate the widget.

I agree with Lykurg's comment in your other post about QStackedWidget not normally being a resource problem.

HTH

poporacer
9th August 2010, 04:13
Thanks, but I am still missing something! I tried your code and a new window opened with the original window still there, but the original window was now empty. How do I close the original window? So you think that the stacked widgets, even if they are connected to databases won't be much of a resource issue?

Thanks
Rick

norobro
10th August 2010, 02:43
You should only have one "MainWindow". Your page1 should be the MainWindow::centralWidget() when the app starts and when the slot is called page2 is set as centralWidget.

Don't know what kind of resource restrictions you have or what size data sets you are working with but maybe the following will be of help: I created a stacked widget with four pages, each displaying a QTableView, with a corresponding QSqlQueryModel. I queried (select *) a db containing three tables each 14 columns wide: one with 21,143 records (used in two views), one with 17,540 records, and one with 35,036 records. I ran the app on an older machine (AMD Athlon XP 2700+ with 512MB of memory) and had no performance problems. The top command reports the app uses 27MB (5.4%) of system memory.

poporacer
10th August 2010, 04:09
I don't know why it isn't working like it should??? I think the stacked widgets is the way to go then. I am trying to figure out the stacked widgets now :confused: I am using Creator 2.0 and can get the form and the holder for the stacked widgets. I see the properties for page and page_2. I can't find the properties to put in the name of the widget I want for each page. From the examples it shows the form created from code. How do I put the widgets I want on the stacked widgets?

norobro
10th August 2010, 04:27
Here's a small example app. I just threw it together so the layout is pretty sloppy but maybe it will help.

poporacer
12th August 2010, 03:04
I feel stupid! I was trying to create separate widgets for the pages and then couldn't find how to load them! You just build each widget on each page! Thanks....Sometimes it helps to see an example!
Thanks a bunch!
I am sure I will be back several times while I am trying to learn this QT stuff!