Results 1 to 6 of 6

Thread: Change the value of a form's widget from other form.

  1. #1
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Change the value of a form's widget from other form.

    I need to change the value of a lineEdit text from other form.
    I now that is a stupid question, I have searched through the forum but didn't found how to.
    I have made a very simple project for illustrate: main class, form1 and form2 that contains a lineEdit and a pushButton each.
    -The main function instantiate and show form1.
    -In form1, the pushButton instantiate and show form2
    -In form2, the pushButton should take the form2.lineEdit text (m_ui->lineEdit->text() ) and save it into form1.lineEdit

    I don't know how to acces form1.lineEdit from form2.
    If I try form1->lineEdit->text() it throw "form1 is not declarated at this scope"

    I include the code.

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "form1.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Form1 form1; //Instanciate and show form1
    8. form1.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    form1.cpp
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3. #include "form2.h"
    4. #include "ui_form2.h"
    5.  
    6. Form1::Form1(QWidget *parent)
    7. : QMainWindow(parent), ui(new Ui::Form1)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. Form1::~Form1()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Form1::on_pushButton_clicked() //Instanciate and show form2
    18. {
    19. static Form2* form2 = new Form2(this);
    20. form2->show();
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    form2.cpp
    Qt Code:
    1. #include "form2.h"
    2. #include "ui_form2.h"
    3. #include "form1.h"
    4. #include "ui_form1.h"
    5.  
    6. Form2::Form2(QWidget *parent) :
    7. QMainWindow(parent),
    8. m_ui(new Ui::Form2)
    9. {
    10. m_ui->setupUi(this);
    11. }
    12.  
    13. Form2::~Form2()
    14. {
    15. delete m_ui;
    16. }
    17.  
    18. void Form2::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. m_ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. void Form2::on_pushButton_clicked()
    31. {
    32. QString text=m_ui->lineEdit->text();
    33. //
    34. // Here I want to change the value of lineEdit widget in form1
    35. //
    36. // I think that "form1->ui->lineEdit->setText("xyz");" should work, but when building it throws:
    37. // "/home/ioseba/QT/Programas QT/2ventanas/form2.cpp:35: error: ‘form1’ was not declared in this scope
    38. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for your time.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change the value of a form's widget from other form.

    Read the Qt doc about signals and slots implementation.
    QLineEdit: signal textChanged(const QString & text) slot setText(const QString &)
    Last edited by toutarrive; 10th March 2010 at 21:06.

  3. #3
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Change the value of a form's widget from other form.

    First of all, thanks for your attention.
    I think that signals-slots are not related with my question. (My English level is very low and maybe I didn't explained it well)
    I will try it again:
    - We have two forms (form1 and form2) created as shown in the first post (form1 instantiates and shows form2) .
    How can form2 gets the value of form1's items (lineedits, checkboxes...) in order to store them in local variables and operate with them? I think there must be an easy way to do it.
    The values of form1's components don´t change. I store the results of form2 in a SQlDatabase.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change the value of a form's widget from other form.

    Pass a (form1)pointer to form2 and you can access the values of form1 via the pointer. Since fom2 is a child of form1 you can also use parent() with a proper cast.

  5. #5
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Change the value of a form's widget from other form.

    Just think how 2 classes can transfer data to each other. You can make some getter methods in form2 that form1 will use to get the data.
    Also, another solution is using the signal-slot mechanism (as toutarrive told you) that Qt provides.
    Last edited by Archimedes; 11th March 2010 at 09:13.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  6. #6
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Change the value of a form's widget from other form.

    Nothing seems to work, and I can't understand it.
    I have make in form1 a public function that returns the QString I want, but I can't call it from form2.
    When I try in form2 "this->parent()->..." or "this->parentWidget()->..."in the autocompletation never appears any of the public components of form1.

    At the end I have modified the form2's constructor including as parameters the QStrings needed. In this case it is enough, but I'm sure I will need access to a form1 instance from a form2 instance after this one was created.

    I know this is not a QT question but, can anybody tell me the code of de function (belongs to form2) that store in the lineEdit of a instance of Form2 the value of the lineEdit of his parent?

Similar Threads

  1. Open one Form (widget)
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 7th October 2009, 13:13
  2. Help about Qt 4.5, how to change form??
    By webquinty in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2009, 13:41
  3. loading an image onto any form's widget
    By sudheer in forum Qt Programming
    Replies: 4
    Last Post: 1st December 2007, 04:55
  4. The issue on Form's size
    By adorp in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2007, 18:30
  5. refresh form after adding a widget
    By cyrille in forum Newbie
    Replies: 2
    Last Post: 2nd February 2007, 10:40

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.