Results 1 to 10 of 10

Thread: Transfer variable from qmainwindow to qwidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transfer variable from qmainwindow to qwidget

    this is a sample for passing data from one dialog to dialog01.
    In dialog: one qpushbutton, one qlienedit, in dialog01: qlineedit;

    dialog.h

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. class Dialog01;
    7.  
    8. namespace Ui {
    9. class Dialog;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19.  
    20. public slots:
    21. void sendText();
    22.  
    23. signals:
    24. void signalText(QString text);
    25.  
    26. private:
    27. Ui::Dialog *ui;
    28.  
    29. Dialog01* pDialog;
    30. };
    31.  
    32. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. #include "dialog01.h"
    5.  
    6. Dialog::Dialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Dialog),
    9. pDialog(new Dialog01(this))
    10. {
    11. ui->setupUi(this);
    12.  
    13. connect(ui->pushButton, &QPushButton::clicked,
    14. this, &Dialog::sendText);
    15. connect(this, &Dialog::signalText,
    16. pDialog, &Dialog01::recvText);
    17. }
    18.  
    19. Dialog::~Dialog()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void Dialog::sendText()
    25. {
    26. emit signalText(ui->lineEdit->text());
    27.  
    28. pDialog->setVisible(true);
    29. }
    To copy to clipboard, switch view to plain text mode 

    dialog01.h
    Qt Code:
    1. #ifndef DIALOG01_H
    2. #define DIALOG01_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class Dialog01;
    8. }
    9.  
    10. class Dialog01 : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Dialog01(QWidget *parent = 0);
    16. ~Dialog01();
    17.  
    18. public slots:
    19. void recvText(QString text);
    20.  
    21. private:
    22. Ui::Dialog01 *ui;
    23. };
    24.  
    25. #endif // DIALOG01_H
    To copy to clipboard, switch view to plain text mode 

    dialog01.cpp
    Qt Code:
    1. #include "ui_dialog01.h"
    2.  
    3. #include "dialog01.h"
    4.  
    5. Dialog01::Dialog01(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog01)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. Dialog01::~Dialog01()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Dialog01::recvText(QString text)
    18. {
    19. ui->lineEdit->setText(text);
    20. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "maindialog.h"
    4.  
    5. #include "dialog.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. //MainDialog w;
    11. //w.show();
    12.  
    13. Dialog d;
    14. d.show();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 12th April 2016 at 23:25. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Apr 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Transfer variable from qmainwindow to qwidget

    Ah I see, I have to implement two connections. One signal in each class and one slot in each class and the variable is emitted in the slot functions, right?
    Last edited by franzib; 12th April 2016 at 15:11. Reason: spelling corrections

  3. #3
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transfer variable from qmainwindow to qwidget

    yeah, pushbutton's click have no param, so you should send a slot with no param, however, you could emit signal with param which you want in slot method.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer variable from qmainwindow to qwidget

    Quote Originally Posted by franzib View Post
    Ah I see, I have to implement two connections. One signal in each class and one slot in each class and the variable is emitted in the slot functions, right?
    No.
    One signal at the source and one slot at the receiver is enough.

    And you don't need signal/slots at all if all you want is to provide initial data to the other object.

    The example is a demonstration on how to use signal/slots but in a real project just useless code complexity.

    Cheers,
    _

Similar Threads

  1. access to bits of a char variable
    By Alex22 in forum Newbie
    Replies: 3
    Last Post: 27th December 2015, 20:49
  2. Periodically access QML variable from C++
    By mekarim in forum Newbie
    Replies: 4
    Last Post: 24th November 2015, 11:45
  3. Access variable using its name as QString
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 22nd December 2010, 09:11
  4. QtScript access variable from C++
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 23:51
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 20:48

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.