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.
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.