Results 1 to 5 of 5

Thread: Qt sending variables using signals & slots

  1. #1
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QMainWindow>
    5. #include "test2.h"
    6. namespace Ui {
    7. class test;
    8. }
    9.  
    10. class test : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit test(QWidget *parent = 0);
    16. ~test();
    17. signals:
    18. void notifyValueSent(const QString value);
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. private:
    23. Ui::test *ui;
    24. };
    25.  
    26. #endif // TEST_H
    To copy to clipboard, switch view to plain text mode 
    test.cpp
    Qt Code:
    1. #include "test.h"
    2. #include "ui_test.h"
    3.  
    4. test::test(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::test)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11.  
    12. test::~test()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void test::on_pushButton_clicked()
    18. {
    19. QString value = ui->lineEdit->text();
    20. emit notifyValueSent(value);
    21. }
    To copy to clipboard, switch view to plain text mode 
    test2.h
    Qt Code:
    1. #ifndef TEST2_H
    2. #define TEST2_H
    3.  
    4. #include <QWidget>
    5. #include "test.h"
    6. namespace Ui {
    7. class test2;
    8. }
    9.  
    10. class test2 : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit test2(QWidget *parent = 0);
    16. ~test2();
    17. private slots:
    18. void onValueSent(const QString value);
    19. private:
    20. Ui::test2 *ui;
    21. };
    22.  
    23. #endif // TEST2_H
    To copy to clipboard, switch view to plain text mode 
    test2.cpp
    Qt Code:
    1. #include "test2.h"
    2. #include "test.h"
    3. #include "ui_test2.h"
    4.  
    5. test2::test2(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::test2)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. test2::~test2()
    13. {
    14. delete ui;
    15. }
    16. void test2::onValueSent(QString value){
    17.  
    18. ui->label->setText(value);
    19. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "test.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. test t;
    8. test2 t2;
    9. QObject::connect(&t,SIGNAL(notifyValueSent(value)),&t2,SLOT(onValueSent(value)));
    10. t.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Thank you.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt sending variables using signals & slots

    You have to show t2 too.

  3. #3
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt sending variables using signals & slots

    Change line 9 in main to :
    Qt Code:
    1. QObject::connect(&t,SIGNAL(notifyValueSent(QString)),&t2,SLOT(onValueSent(QString)));
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    Akame.L (27th February 2017)

  6. #5
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt sending variables using signals & slots

    I didn't see that! Thank u

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Sending signals from QPushButton
    By iosif18 in forum Qt Quick
    Replies: 0
    Last Post: 15th September 2011, 11:23
  3. Sending signals from QPushButton
    By Cucus in forum Newbie
    Replies: 3
    Last Post: 19th July 2011, 04:06
  4. Sending OS I/O interrupt signals!
    By nofortee in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2010, 15:42
  5. Sending signals from one module to another
    By Dubbie in forum Newbie
    Replies: 5
    Last Post: 6th July 2010, 02:52

Tags for this Thread

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.