Results 1 to 10 of 10

Thread: Transfer variable from qmainwindow to qwidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    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]

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.