PDA

View Full Version : Passing an object from one form to the other.



cbarmpar
1st September 2008, 21:48
Hi all,

What I am trying to do is to pass an object from one form (or the main to the other).
I am quite sure on how to do that in console c++ but a bit confused with qt. I have attached my code which cannot be compiled due to an error with an undefined class. The class defined in the header file. I am bit confused any help will be much appreciated.

if you can provide a sample code where an abject is passed from one form to the other it will help a lot clarifying the concept.

Many thanks in advance,
Christos

wysota
1st September 2008, 22:28
Qt is C++. Everything that can be done in C++ can be directly used in Qt-based applications.

I have taken a look at your code - it has at least two basic C++ errors, maybe they are causing you problems?

cbarmpar
1st September 2008, 22:36
Many thanks for the reply.

I have already tried lots of ways to implement this logic. its is likely that the code is wrong. I am trying to pass one object, declared in the first form, to the second form.
Is it possible to provide a sample code for that.
I found a similar topic in the forum but I could not understand it.

Many thanks once again.

wysota
1st September 2008, 22:52
Well... there is not much to say, really...

class A {
public:
A(){}
};

class B {
public:
B(){}
void setA(A *a){
m_a = a;
}
private:
A *m_a;
};

cbarmpar
1st September 2008, 22:58
Many thanks for your time.

It looks that i haven't made my self clear and i am sorry for that. What i am trying to do is not just to define a class.

what i want is to create a formA with two lineEdits. I want to pass the values of these widgets to an object made from a class declared in the formA. I want then to pass the object which cotains the data of the formA to a new form named as formB.

Just wondering if you can demonstrate that with a sort code.

Thanks once again.

(what i don't understand is how the formsB constructor can accept the object)

wysota
2nd September 2008, 00:04
You mean something like this?

class A : public QWidget {
Q_OBJECT
public:
A(QWidget *parent = 0) : QWidget(parent){
QVBoxLayout *l = new QVBoxLayout(this);
le1 = new QLineEdit;
le2 = new QLineEdit;
QPushButton *pb = new QPushButton;
connect(pb, SIGNAL(clicked()), SLOT(onClick()));
l->addWidget(le1);
l->addWidget(le2);
l->addWidget(pb);
}
public slots:
void onClick(){
QDialog dlg;
QVBoxLayout *l = new QVBoxLayout(&dlg);
QLabel *lab = new QLabel;
l->addWidget(lab);
lab->setText(le1->text()+" "+le2->text());
dlg.exec();
}
private:
QLineEdit *le1, *le2;
};
The other "form" is the dialog in this case...

cbarmpar
2nd September 2008, 00:20
Nearly what i meant.

It is partialy what i meant. In a way you pass forms data to the dialog. But its not what my problem is.

IF on your code i create another class for example class B which consists of several integrals and arrays manipulated by class A. How can i pass to the dialog the whole components of the class B (so i can redisplay them in the widgets of form B). I would like to use an object so i dont have to pass every integral individually.

My comes arises from the fact that i need to make a form that is gonna get as arguments an objects from someones else code. I then need to display the contents of this object in my code.

Thanks again

wysota
2nd September 2008, 00:59
A widget can be displayed in one place only. You can't share a widget between other widgets - it will either be here or there, not in two places at once. If that's fine with you simply pass a pointer to object you want to move to the new form while creating its layout or call setParent() directly.

cbarmpar
2nd September 2008, 14:11
Hi again,

I have modified and made some comments on the code that i have uploaded. I hope now is more clear.

I declare a class on the myqtapp form. I create an object from this class with the name ena.
My question is how can i pass the object ena to the parathiro constructor so I can use the object from the dialog2 form.

Thanks again.

cbarmpar
2nd September 2008, 21:37
file updated!

cbarmpar
3rd September 2008, 15:12
Solution found in an other forum:

http://www.qtforum.org/article/25888/passing-an-object-from-one-form-to-the-other.html?29628f17

though it is a good idea to included.

Regards