PDA

View Full Version : Handling multiple forms



msmihai
6th December 2008, 11:27
Hello everybody . I'm new to this forum ( because it's the first time for me using QT but i'm pretty sure i'll be using it regularly from now ) . I followed a couple of tutorials, wrote some simple GUI applications but I could'nt find an answer for my problem .

I have to ui's ( dlg1.ui and dlg2.ui ). dlg1.ui is the main window of my application(project type: MainWindow) , with the name Form1 , and dlg2.ui is another window( project type Widget ) .

Here is how my projects files look like .
main.cpp


#include <qapplication>
#include "app.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
App1 dialog;
dialog.show();
return app.exec();
}



app.h


#ifndef APP_H
#define APP_H

#include "ui_dlg1.h"
#include "ui_dlg2.h"

class App1:public QMainWindow,private Ui::Form1
{
Q_OBJECT
public:
App1();
public slots:
void button_clicked();
};

class App2:public QWidget,private Ui::Form2
{
Q_OBJECT
public:
App2(QWidget *parent = 0);
};

#endif


app.cpp


#include <QtGui>
#include "app.h"

App1::App1()
{
setupUi(this);
connect(pushButton,SIGNAL(clicked()),this,SLOT(but ton_clicked()));
}

App2::App2(QWidget *parent)
{
setupUi(this);
}

void App1::button_clicked()
{
App2 form2(this);
form2.show();
}



This code works just fine but when I press the button , Form2 appears for a very very short time and then it's being destroyed . Why this happens ? And how can I correct it so that Form2 shows as a modal window ( no interraction with Form1 being possible till Form2 is being closed ) .

Furthermore, how can I make two forms communicate with each other ? My idea was a global variabiles in app.cpp that each form can see .

I hope I was clear enough . Have a great day !

caduel
6th December 2008, 11:59
you create form2 on the stack. When the scope is left, it is destroyed (and thus disappears.)

You have two possibilites.
i) Make it a modal dialog (ie inherit from QDialog and call exec())
ii) allocate form2 on the heap (using new, and ideally passing a QObject parent object to make sure the object gets deleted in time)

HTH

msmihai
6th December 2008, 12:21
Thanks a lot caduel, i used dynamic allocation and it worked .
Still, i have one more question .
Let's say i Press de button, Form2 appears ( modally , that means i Form1 is disabled ).
I input some text into Form2's LineEdit and then i press a button, also on Form2. When I press the button, Form2 closes and Form1 becomes active again .

What I want to do now is to take the text from LineEdit on Form2 and show it somewhere in Form1 . I managed to get the actual text using a global variabile. But ... how can i detect that Form2 has closed ? For example, let's say in Form1 I also have a LineEdit. When I close Form2 , I want Form1 LineEdit to have the text from Form2 LineEdit .

How can I accomplish this ?

I tried the followind code:



void App1::button_clicked()
{
App2 *form2 = new App2(this);
form2->show();
QMessageBox msg;
msg.setText(text);
msg.exec();
}


hoping that the code after form2->show() would be executed after Form2 was closed.Unfortunately, the code executes as soon as Form2 appears on the screen.

caduel
6th December 2008, 13:25
override the okay() slot (if using a QDialog): store your lineedit's contents in your dialog.
at the call site (when exec() returned), call a getter-function to access that text. do not use globals for that. it is ugly and will cause you trouble. (e.g. you can not use two such dialogs that way).

msmihai
6th December 2008, 13:41
could you give me an example ? i'm relative new to QT ... I don't fully understand what you explain to me ... and no .. I have a QmainWindow and a QWidget.