PDA

View Full Version : accessing form1's objects from form2



Philip_Anselmo
28th April 2006, 15:04
Hello, N00B here :D

I'm as new to C++ as to Qt so I'm having problems all the time and wel some of them I could resolved my self with the documentation and reading some posts here and in qtforum..

Well straight to the point:
I've made an app (inb Qt Designer) that has 2 forms the first one (form1, wich is a MainWindow) has a LineEdit called LE1, a QTextLabel called TL1 and 2 pushbuttons... I think you can guess how did I called them (PB1,PB2)
The second form.. (form2) a Dialog has only a QTextLabel TL2 and 2 PushButtons PBCaptura and PBSalir (I worked my head to the bone thinking on those names XD)

The thing is that I've included the forms on each other, like
Form1
#include <form2.h>

on the slots of the form1 clicking on the PB1 makes the text from LE1 appear on TL1, and on a QMessageBox, then if you click on PB2 it calls the form2...

void Form1::muestraform2()
{
static Form2 *form2 = new Form2(this);
static Form1 *form1 = new Form1;
fprima->close();
form2->show();
form2->setActiveWindow();
form2->raise();
}

but doesn't close the form1... <=the first question is.. how do I close or better yet.. just hide.. the form1?


the other problem is that I made an object of the class form1 on form2 ( Form1 *form1 = new Form1(); ), the I tryied to fill the TL2 with this:

TL2->setText("en el form1 escribiste: " + form1->LE1->text());

It doesn't gives me any errors at all, but the text is empty :confused: <= 2º question

So I tryied another way...

on form1 I'm trying to capture the LE1->text() on a QString called texto

QString texto = LE1->text(); (it's declared and filled inside a slot wich I use to pass the text from LE1 to TL1) <= 3º question.. how do I capture on a QString the text inputed on the LE1?

I'm not shure about managing a QString... to be honest I don't think is that way but I just can't find any examples anywhere.

after that I want to pass that QString to form2 to fill the TL2 <= 4º question

It should be like: form1->texto right?

With these I'm pretending to learn how to capture input from lineedits to fill variables on main.cpp, and other forms, so I can make an app that connects to a Database wich I set on runtime... and well to learn that and to avoid my boss to kick me out of the job

Please helpme :crying:

PD: I'm working on a Linux Fedora Core 1 with Qt 3.1.2

... I miss my deb sarge
I must leave Deby Ann at home :(

jacek
28th April 2006, 15:49
but doesn't close the form1... <=the first question is.. how do I close or better yet.. just hide.. the form1?
Try this:
void Form1::muestraform2()
{
static Form2 *form2 = new Form2( this );
hide();
form2->show();
form2->setActiveWindow();
form2->raise();
}


the other problem is that I made an object of the class form1 on form2 ( Form1 *form1 = new Form1(); ), the I tryied to fill the TL2 with this:
...
It doesn't gives me any errors at all, but the text is empty
It looks like you create a new object of the Form1 class instead of using the existing one.


QString texto = LE1->text(); (it's declared and filled inside a slot wich I use to pass the text from LE1 to TL1)
...
It should be like: form1->texto right?
Again, it looks like you are using a local variable instead another one --- in this case you should use a member variable.


after that I want to pass that QString to form2 to fill the TL2
You can either create a method that returns that string and just invoke it from that second form in the Right Time(tm) or you can use signals and slots mechanism to update that string after every change.

Philip_Anselmo
28th April 2006, 17:03
Thanks for the help :D

but..:o I'm still a little confused with the last 3

If I'm making a new object of the Form1, how can I access the LE1 without making the object? :$

some code plz.. I'm lost here :(

and well the same with passing variables :S I don't even see where or how I can declare the to be accesible from other forms or even functions.

and about the slot.. wel on the connections dialog of Qt Designer doesn't appear form2 on the form1 or form1 on the form2 :S

Thanks for you time and pacience

jacek
28th April 2006, 17:23
If I'm making a new object of the Form1, how can I access the LE1 without making the object?
If you want to access information from the already existing object, you can't create a new one and access it instead --- these are two different object. If you want to read information from an opened window, you must have a pointer to an object that represents that window.


and well the same with passing variables :S I don't even see where or how I can declare the to be accesible from other forms or even functions.
Subclass (http://doc.trolltech.com/3.3/designer-manual-6.html) your form and you will be able to do anything you want.


and about the slot.. wel on the connections dialog of Qt Designer doesn't appear form2 on the form1 or form1 on the form2
Qt Designer is only for designing GUI. You will have to use some IDE to write your code.

Philip_Anselmo
4th May 2006, 22:49
Thanks for the help man... :o but I'm still too lost on this...

I'm trying to do everything as old good C but C++ style without signals & slots :$ well as I can avoid them... just keeping it simple.. maybe if you could take a look at my code?
http://www.qtcentre.org/forum/showthread.php?t=2076

on that post is my code... plz take a pik :p

... by the way is there a qt + c++ portable.. for dummies?.. xD I could realy make use of it

I'm already troubled to read and write in inglish xD

jacek
4th May 2006, 22:54
I'm trying to do everything as old good C but C++ style without signals & slots :$ well as I can avoid them... just keeping it simple..
The signals and slots mechanism is one of those things that make Qt programming easy --- you shouldn't avoid it.