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
Code:
#include <QtGui/QApplication>
#include "form1.h"
int main(int argc, char *argv[])
{
Form1 form1; //Instanciate and show form1
form1.show();
return a.exec();
}
form1.cpp
Code:
#include "form1.h"
#include "ui_form1.h"
#include "form2.h"
#include "ui_form2.h"
{
ui->setupUi(this);
}
Form1::~Form1()
{
delete ui;
}
void Form1::on_pushButton_clicked() //Instanciate and show form2
{
static Form2* form2 = new Form2(this);
form2->show();
}
form2.cpp
Code:
#include "form2.h"
#include "ui_form2.h"
#include "form1.h"
#include "ui_form1.h"
m_ui(new Ui::Form2)
{
m_ui->setupUi(this);
}
Form2::~Form2()
{
delete m_ui;
}
void Form2
::changeEvent(QEvent *e
) {
switch (e->type()) {
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void Form2::on_pushButton_clicked()
{
QString text
=m_ui
->lineEdit
->text
();
//
// Here I want to change the value of lineEdit widget in form1
//
// I think that "form1->ui->lineEdit->setText("xyz");" should work, but when building it throws:
// "/home/ioseba/QT/Programas QT/2ventanas/form2.cpp:35: error: ‘form1’ was not declared in this scope
}
Thanks for your time.
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 &)
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.
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.
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.
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?