Results 1 to 11 of 11

Thread: Passing an object from one form to the other.

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Passing an object from one form to the other.

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing an object from one form to the other.

    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?

  3. #3
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing an object from one form to the other.

    Well... there is not much to say, really...
    Qt Code:
    1. class A {
    2. public:
    3. A(){}
    4. };
    5.  
    6. class B {
    7. public:
    8. B(){}
    9. void setA(A *a){
    10. m_a = a;
    11. }
    12. private:
    13. A *m_a;
    14. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

    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)

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing an object from one form to the other.

    You mean something like this?
    Qt Code:
    1. class A : public QWidget {
    2. Q_OBJECT
    3. public:
    4. A(QWidget *parent = 0) : QWidget(parent){
    5. QVBoxLayout *l = new QVBoxLayout(this);
    6. le1 = new QLineEdit;
    7. le2 = new QLineEdit;
    8. connect(pb, SIGNAL(clicked()), SLOT(onClick()));
    9. l->addWidget(le1);
    10. l->addWidget(le2);
    11. l->addWidget(pb);
    12. }
    13. public slots:
    14. void onClick(){
    15. QDialog dlg;
    16. QVBoxLayout *l = new QVBoxLayout(&dlg);
    17. QLabel *lab = new QLabel;
    18. l->addWidget(lab);
    19. lab->setText(le1->text()+" "+le2->text());
    20. dlg.exec();
    21. }
    22. private:
    23. QLineEdit *le1, *le2;
    24. };
    To copy to clipboard, switch view to plain text mode 
    The other "form" is the dialog in this case...

  7. The following user says thank you to wysota for this useful post:

    cbarmpar (3rd September 2008)

  8. #7
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

    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

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing an object from one form to the other.

    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.

  10. #9
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

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

  11. #10
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

    file updated!

  12. #11
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing an object from one form to the other.

    Solution found in an other forum:

    http://www.qtforum.org/article/25888....html?29628f17

    though it is a good idea to included.

    Regards

Similar Threads

  1. Replies: 4
    Last Post: 12th August 2008, 02:55
  2. passing an object
    By mickey in forum General Programming
    Replies: 3
    Last Post: 16th January 2008, 11:27
  3. Replies: 2
    Last Post: 17th May 2006, 22:01
  4. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 10:50
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 16:27

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.