Results 1 to 6 of 6

Thread: accessing form1's objects from form2

  1. #1
    Join Date
    Apr 2006
    Posts
    40
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question accessing form1's objects from form2

    Hello, N00B here

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

    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
    Battle Programmer Ph1L
    Philip_Anselmo
    Greetings From Osorno - Chile

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing form1's objects from form2

    Quote Originally Posted by Philip_Anselmo
    but doesn't close the form1... <=the first question is.. how do I close or better yet.. just hide.. the form1?
    Try this:
    Qt Code:
    1. void Form1::muestraform2()
    2. {
    3. static Form2 *form2 = new Form2( this );
    4. hide();
    5. form2->show();
    6. form2->setActiveWindow();
    7. form2->raise();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Philip_Anselmo
    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.

    Quote Originally Posted by Philip_Anselmo
    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.

    Quote Originally Posted by Philip_Anselmo
    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.

  3. The following user says thank you to jacek for this useful post:

    Philip_Anselmo (4th May 2006)

  4. #3
    Join Date
    Apr 2006
    Posts
    40
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face Re: accessing form1's objects from form2

    Thanks for the help

    but.. 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
    Battle Programmer Ph1L
    Philip_Anselmo
    Greetings From Osorno - Chile

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing form1's objects from form2

    Quote Originally Posted by Philip_Anselmo
    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.

    Quote Originally Posted by Philip_Anselmo
    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 your form and you will be able to do anything you want.

    Quote Originally Posted by Philip_Anselmo
    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.

  6. The following user says thank you to jacek for this useful post:

    Philip_Anselmo (4th May 2006)

  7. #5
    Join Date
    Apr 2006
    Posts
    40
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing form1's objects from form2

    Thanks for the help man... 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

    ... 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
    Battle Programmer Ph1L
    Philip_Anselmo
    Greetings From Osorno - Chile

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing form1's objects from form2

    Quote Originally Posted by Philip_Anselmo
    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.

  9. The following user says thank you to jacek for this useful post:

    Philip_Anselmo (4th May 2006)

Similar Threads

  1. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  2. Accessing objects in different forms
    By Lebowski in forum Newbie
    Replies: 17
    Last Post: 13th April 2006, 11:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.