sr1s
22nd March 2018, 20:25
Hello everybody,
I have just started using Qt, and immediately I found the first difficulties in understanding the use of signal / slot.
The problem is simple: I have a window (QWidget) with a QlineEdit and a button: I would like that, after typing a text in the Qline and pressing the button, another window (QWidget) would open with a QlineEdit field, filled with the text previously entered.
I can do the reverse (from first page open the second page, and from here -pressing a button - to fill the parent window), but it seems that in my code there is no link between the signal and the generated window slot ... I found this topic: http://www.qtcentre.org/threads/23989-Communication-between-two-QDialog-windows?highlight=
but I can't apply the suggestions.
Page1.h:
#ifndef PAGE1_H
#define PAGE1_H
#include <QWidget>
namespace Ui {
class Page1;
}
class Page1 : public QWidget
{
Q_OBJECT
public:
explicit Page1(QWidget *parent = 0);
~Page1();
signals:
void transfer(QString);
private:
Ui::Page1 *ui;
private slots:
void on_pushButton_clicked();
};
#endif // PAGE1_H
Page1.cpp:
#include "page1.h"
#include "ui_page1.h"
#include "page2.h"
Page1::Page1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Page1)
{
ui->setupUi(this);
}
Page1::~Page1()
{
delete ui;
}
void Page1::on_pushButton_clicked()
{
Page2 *form = new Page2();
QString testo;
testo=ui->to_pass->text();
emit(transfer(testo));
connect(this, SIGNAL(trasfer(QString)), form, SLOT(UpdateField(QString)));
form->show();
}
Page2.h:
#ifndef PAGE2_H
#define PAGE2_H
#include <QWidget>
namespace Ui {
class Page2;
}
class Page2 : public QWidget
{
Q_OBJECT
public:
explicit Page2(QWidget *parent = 0);
~Page2();
private slots:
void UpdateField(QString text);
private:
Ui::Page2 *ui;
};
#endif // PAGE2_H
Page2.cpp:
#include "page2.h"
#include "ui_page2.h"
Page2::Page2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Page2)
{
ui->setupUi(this);
}
Page2::~Page2()
{
delete ui;
}
void Page2::UpdateField(QString text)
{
ui->receive->clear();
ui->receive->setText(text);
}
Thanks for any suggestion!
I have just started using Qt, and immediately I found the first difficulties in understanding the use of signal / slot.
The problem is simple: I have a window (QWidget) with a QlineEdit and a button: I would like that, after typing a text in the Qline and pressing the button, another window (QWidget) would open with a QlineEdit field, filled with the text previously entered.
I can do the reverse (from first page open the second page, and from here -pressing a button - to fill the parent window), but it seems that in my code there is no link between the signal and the generated window slot ... I found this topic: http://www.qtcentre.org/threads/23989-Communication-between-two-QDialog-windows?highlight=
but I can't apply the suggestions.
Page1.h:
#ifndef PAGE1_H
#define PAGE1_H
#include <QWidget>
namespace Ui {
class Page1;
}
class Page1 : public QWidget
{
Q_OBJECT
public:
explicit Page1(QWidget *parent = 0);
~Page1();
signals:
void transfer(QString);
private:
Ui::Page1 *ui;
private slots:
void on_pushButton_clicked();
};
#endif // PAGE1_H
Page1.cpp:
#include "page1.h"
#include "ui_page1.h"
#include "page2.h"
Page1::Page1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Page1)
{
ui->setupUi(this);
}
Page1::~Page1()
{
delete ui;
}
void Page1::on_pushButton_clicked()
{
Page2 *form = new Page2();
QString testo;
testo=ui->to_pass->text();
emit(transfer(testo));
connect(this, SIGNAL(trasfer(QString)), form, SLOT(UpdateField(QString)));
form->show();
}
Page2.h:
#ifndef PAGE2_H
#define PAGE2_H
#include <QWidget>
namespace Ui {
class Page2;
}
class Page2 : public QWidget
{
Q_OBJECT
public:
explicit Page2(QWidget *parent = 0);
~Page2();
private slots:
void UpdateField(QString text);
private:
Ui::Page2 *ui;
};
#endif // PAGE2_H
Page2.cpp:
#include "page2.h"
#include "ui_page2.h"
Page2::Page2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Page2)
{
ui->setupUi(this);
}
Page2::~Page2()
{
delete ui;
}
void Page2::UpdateField(QString text)
{
ui->receive->clear();
ui->receive->setText(text);
}
Thanks for any suggestion!