PDA

View Full Version : Multiple Forms Handling



eva2002
15th January 2010, 03:23
hi all,

previously I posted a question on multiple forms handling. However no one replied. Anyway, I hope this time someone would reply me.

I am using qt creator and designer to work out my application. I have 2 forms (f1,f2) I want to load f2 when f1 button is pressed and also pass a variable to f2. How can I do it?

below is my codes. when I clicked on the button. nothing happen.

Form1.h


#ifndef FORM1_H
#define FORM1_H

#include <QWidget>

namespace Ui {
class form1;
}

class form1 : public QWidget {
Q_OBJECT
public:
form1(QWidget *parent = 0);
~form1();

protected:
void changeEvent(QEvent *e);

private slots:
void load_form2();

private:
Ui::form1 *ui;
};

#endif // FORM1_H


form1.cpp


#include "form1.h"
#include "ui_form1.h"
#include "form2.h"
#include "ui_form2.h"
#include <QString>

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

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

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

void form1::load_form2()
{
QString str = "to be pass over to f2";
Form2 f2;
f2.show();
}


from2.h


#ifndef FORM2_H
#define FORM2_H

#include <QWidget>

namespace Ui {
class Form2;
}

class Form2 : public QWidget {
Q_OBJECT
public:
Form2(QWidget *parent = 0);
~Form2();

protected:
void changeEvent(QEvent *e);

private slots:
void load_form1();

private:
Ui::Form2 *ui;
};

#endif // FORM2_H


form2.cpp


#include "form2.h"
#include "ui_form2.h"
#include "form1.h"
#include "ui_form1.h"

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

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

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

void Form2::load_form1()
{
form1 a;
a.show();
}


Main.cpp


#include <QtGui/QApplication>
#include "form1.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
form1 w;
w.show();
return a.exec();
}


When I tried to new form1 like the below code

Form2 *f2 = new Form2(this) <- under button click on form 1
f2.show();

I got error
error: request for member ‘show’ in ‘a’, which is of non-class type ‘form1*’

if I remove the pointer I got this error
error: ‘QWidget::QWidget(const QWidget&)’ is private
within this context (points to class form1)

Can someone help me out? I also need to know how to pass the str string over to f2.

Thanks in advance.

sudhansu
15th January 2010, 06:05
Try like this...
create a pointer to form2 in form1 class. allocate memory in constructor.
inside button_on_click slot

call pointer->show();

eva2002
16th January 2010, 01:45
It works. I also found out the I did a mistake. there is no on clicked events.

Anway I am one more doubt on multiple forms. How can I pass variables of one forms to another.

ie form 1->label 1->text (lbl1) to form 2's->QString str

iosebaere
11th March 2010, 17:22
I have the same problem.
Have you found the way to access to form1 variables from form2?

mauriciod73
11th November 2010, 01:21
To pass a variable you should create a public pointer of the same tipe as the widget from the form you want to get the data from, and, in the constructor of the class you initialize them to the ui element.

pt = ui->widget

ChrisW67
11th November 2010, 07:24
When I tried to new form1 like the below code

Form2 *f2 = new Form2(this) <- under button click on form 1
f2.show();

I got error
error: request for member ‘show’ in ‘a’, which is of non-class type ‘form1*’

The error message tells you exactly what is wrong here (or at least it would if it matched the code snippet). "a" is "f2" and "form1*" is "Form2 *" in your code snippet. "f2" is a pointer to an object. "f2.show()" tries to to treat f2 as an instance of the class Form2.

To your original question regarding sharing something between two forms you have several obvious options:
Pass the value into the constructor of Form2
Create a member variable inside Form2 and a public setter function. Call the setter after creating an instance of Form2.
If the value can change in Form1 while Form2 is open, and the change should be reflected in Form2, then use a signal in Form1 and slot in Form2.
Rethink your design if items inside Form1 do not logically belong there.