Qt sending variables using signals & slots
Hello,
I want to send a message from a QLineEdit (lineEdit) after clicking a QPushButton (pushButton) from a window (test) and print it to a QLabel (label) in another window (test2).
This is what I've done so far:
test.h
Code:
#ifndef TEST_H
#define TEST_H
#include <QMainWindow>
#include "test2.h"
namespace Ui {
class test;
}
{
Q_OBJECT
public:
explicit test
(QWidget *parent
= 0);
~test();
signals:
void notifyValueSent
(const QString value
);
private slots:
void on_pushButton_clicked();
private:
Ui::test *ui;
};
#endif // TEST_H
test.cpp
Code:
#include "test.h"
#include "ui_test.h"
ui(new Ui::test)
{
ui->setupUi(this);
}
test::~test()
{
delete ui;
}
void test::on_pushButton_clicked()
{
QString value
= ui
->lineEdit
->text
();
emit notifyValueSent(value);
}
test2.h
Code:
#ifndef TEST2_H
#define TEST2_H
#include <QWidget>
#include "test.h"
namespace Ui {
class test2;
}
{
Q_OBJECT
public:
explicit test2
(QWidget *parent
= 0);
~test2();
private slots:
void onValueSent
(const QString value
);
private:
Ui::test2 *ui;
};
#endif // TEST2_H
test2.cpp
Code:
#include "test2.h"
#include "test.h"
#include "ui_test2.h"
ui(new Ui::test2)
{
ui->setupUi(this);
}
test2::~test2()
{
delete ui;
}
void test2
::onValueSent(QString value
){
ui->label->setText(value);
}
main.cpp
Code:
#include "test.h"
#include <QApplication>
int main(int argc, char *argv[])
{
test t;
test2 t2;
QObject::connect(&t,
SIGNAL(notifyValueSent
(value
)),
&t2,
SLOT(onValueSent
(value
)));
t.show();
return a.exec();
}
Thank you.
Re: Qt sending variables using signals & slots
Re: Qt sending variables using signals & slots
Even when I do I don't get the result I want.
After clicking the button i want to open t2 & print the message in QLabel
Re: Qt sending variables using signals & slots
Change line 9 in main to :
Re: Qt sending variables using signals & slots
I didn't see that! Thank u :)