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
#include <QtGui/QApplication>
#include "form1.h"
int main(int argc, char *argv[])
{
Form1 form1; //Instanciate and show form1
form1.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "form1.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form1 form1; //Instanciate and show form1
form1.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
form1.cpp
#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();
}
#include "form1.h"
#include "ui_form1.h"
#include "form2.h"
#include "ui_form2.h"
Form1::Form1(QWidget *parent)
: QMainWindow(parent), ui(new Ui::Form1)
{
ui->setupUi(this);
}
Form1::~Form1()
{
delete ui;
}
void Form1::on_pushButton_clicked() //Instanciate and show form2
{
static Form2* form2 = new Form2(this);
form2->show();
}
To copy to clipboard, switch view to plain text mode
form2.cpp
#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
}
#include "form2.h"
#include "ui_form2.h"
#include "form1.h"
#include "ui_form1.h"
Form2::Form2(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::Form2)
{
m_ui->setupUi(this);
}
Form2::~Form2()
{
delete m_ui;
}
void Form2::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
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
}
To copy to clipboard, switch view to plain text mode
Thanks for your time.
Bookmarks