Results 1 to 10 of 10

Thread: Transfer variable from qmainwindow to qwidget

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

    Default Transfer variable from qmainwindow to qwidget

    Hey

    I have the following problem:
    I have a qmainwindow that has only one qwidget. I would like to transfer some variables from the qmainwindow class, which is called struc_window in my implementation into the qwidget class, which is called struc_widget. The variable nodes can easily be loaded into the constructor of struc_window. But then I am not able to get them into struc_widget. I tried it by custom signals and slots or with qobject_cast... I am sitting for days now
    Some guys told me to solve this problem with standard pointers but it didn't work. I think the problem should be solved with methods related to qobject (as signal and slots etc.)
    What I now try to do is to define a pointer to the parent class struc_window: file: struc_widget.cpp, line 19. Unfortunately, it is a Zero Pointer, am I missing something here?
    Perhaps you could take a look at my code, perhaps you could give me some advices...


    Qt Code:
    1. ////////////////////////////////// struc_window.h ////////////////////////////////////
    2. #ifndef STRUC_WINDOW_H
    3. #define STRUC_WINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <iostream>
    7. #include "struc_widget.h"
    8. # include "../../c++_fem/Eigen/Eigen/Dense"
    9. using namespace Eigen;
    10. using namespace std;
    11.  
    12. namespace Ui {
    13. class struc_window;
    14. }
    15.  
    16. class struc_window : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit struc_window(MatrixXd matrix, QWidget *parent = 0);
    22. ~struc_window();
    23.  
    24. MatrixXd nodes;
    25. //MatrixXd elements;
    26. //MatrixXd elements_info;
    27. //MatrixXd bearing_info;
    28. //void set_nodes();
    29. //int a;
    30.  
    31.  
    32. signals:
    33. //void mysignal();
    34.  
    35.  
    36. private:
    37. Ui::struc_window *ui;
    38.  
    39. };
    40.  
    41.  
    42. #endif // STRUC_WINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ////////////////////////////////// struc_window.cpp ////////////////////////////////////
    2. #include "struc_window.h"
    3. #include "ui_struc_window.h"
    4. #include "struc_widget.h"
    5. #include <iostream>
    6. using namespace std;
    7.  
    8. struc_window::struc_window(MatrixXd matrix, QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::struc_window)
    11. {
    12. ui->setupUi(this);
    13. nodes = matrix;
    14. //struc_widget *p = new struc_widget;
    15. //connect(this,SIGNAL(mysignal()),p,SLOT(getsignal()));
    16.  
    17. }
    18.  
    19. struc_window::~struc_window()
    20. {
    21. delete ui;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ////////////////////////////////// struc_widget.h ////////////////////////////////////
    2. #ifndef STRUC_WIDGET_H
    3. #define STRUC_WIDGET_H
    4.  
    5. #include <QOpenGLWidget>
    6. #include "struc_window.h"
    7.  
    8. #include "../../c++_fem/Eigen/Eigen/Dense"
    9. using namespace Eigen;
    10.  
    11. class struc_widget : public QOpenGLWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit struc_widget(QWidget *parent = 0);
    16.  
    17.  
    18.  
    19. protected:
    20. void initializeGL();
    21. void paintGL();
    22. void resizeGL(int width, int height);
    23.  
    24. void mousePressEvent(QMouseEvent *event);
    25. void mouseMoveEvent(QMouseEvent *event);
    26.  
    27. signals:
    28.  
    29. public slots:
    30. //void getsignal();
    31.  
    32. private:
    33. QPoint lastPos;
    34.  
    35. int xrot,yrot,zrot;
    36. int h;
    37. int b;
    38. double norm_angle_x;
    39. double norm_angle_y;
    40. double norm_angle_z;
    41. void setXRotation(int angle);
    42. void setYRotation(int angle);
    43. void setZRotation(int angle);
    44. void normalize_angle_x(int angle);
    45. void normalize_angle_y(int angle);
    46. void normalize_angle_z(int angle);
    47.  
    48. void draw_structure();
    49.  
    50.  
    51. MatrixXd node_matrix;
    52.  
    53. };
    54.  
    55. #endif // STRUC_WIDGET_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ////////////////////////////////// struc_widget.cpp ////////////////////////////////////
    2. #include "struc_widget.h"
    3. #include <QOpenGLWidget>
    4. #include <QtOpenGL>
    5. #include <QtWidgets>
    6. #include <iostream>
    7. using namespace std;
    8.  
    9. #include "struc_window.h"
    10.  
    11.  
    12. struc_widget::struc_widget(QWidget *parent) : QOpenGLWidget(parent)
    13. {
    14. xrot = 0;
    15. yrot = 0;
    16. zrot = 0;
    17. norm_angle_x = 0;
    18. norm_angle_y = 0;
    19. norm_angle_z = 0;
    20. struc_window *struc_window_ptr = qobject_cast<struc_window*>(this->parent()); !!!!!!!!!!!!!!!!!!!!!!!!!!!! this is a zero pointer??????????????????
    21. cout << struc_window_ptr << endl;
    22. //struc_window_ptr->set_nodes();
    23. //struc_window_ptr->set_nodes();
    24.  
    25.  
    26. }
    27.  
    28. void struc_widget::initializeGL()
    29. {
    30. glClearColor(0.5,0.5,0.5,0.4);
    31. }
    32.  
    33. void struc_widget::paintGL()
    34. {
    35. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    36. glLoadIdentity();
    37.  
    38. glLineWidth(2.5);
    39. normalize_angle_x(xrot);
    40. normalize_angle_y(yrot);
    41. normalize_angle_z(zrot);
    42. glRotatef(norm_angle_x,1,0,0);
    43. glRotatef(norm_angle_y,0,1,0);
    44. glRotatef(norm_angle_z,0,0,1);
    45.  
    46. glBegin(GL_TRIANGLES);
    47. glColor3f(1,0,0);
    48. glVertex3f(-1,-1,0);
    49. glColor3f(0,1,0);
    50. glVertex3f(1,-1,0);
    51. glColor3f(0,0,1);
    52. glVertex3f(0,1,0);
    53. glEnd();
    54. }
    55.  
    56. void struc_widget::resizeGL(int width, int height)
    57. {
    58. int side = qMin(width, height);
    59. glViewport((width - side) / 2, (height - side) / 2, side, side);
    60.  
    61. glMatrixMode(GL_PROJECTION);
    62. glLoadIdentity();
    63. #ifdef QT_OPENGL_ES_1
    64. glOrthof(-2, +2, -2, +2,-10, 10);
    65. #else
    66. glOrtho(-2, +2, -2, +2, -10, 10);
    67. #endif
    68. glMatrixMode(GL_MODELVIEW);
    69. h = height;
    70. b = width;
    71. }
    72.  
    73.  
    74. void struc_widget::mousePressEvent(QMouseEvent *event)
    75. {
    76. lastPos = event->pos();
    77. }
    78.  
    79. void struc_widget::mouseMoveEvent(QMouseEvent *event)
    80. {
    81. int dx = event->x() - lastPos.x();
    82. int dy = event->y() - lastPos.y();
    83.  
    84. if (event->buttons() & Qt::LeftButton)
    85. {
    86. setXRotation(xrot + dy);
    87. }
    88. else if (event->buttons() & Qt::RightButton)
    89. {
    90. setYRotation(yrot + dx);
    91. }
    92. else if (event->buttons() & Qt::MiddleButton)
    93. {
    94. setZRotation(zrot + dx);
    95. }
    96.  
    97. lastPos = event->pos();
    98. }
    99.  
    100.  
    101. // x-rotation
    102. void struc_widget::setXRotation(int angle)
    103. {
    104. if (angle != xrot) {
    105. xrot = angle;
    106. update();
    107. }
    108. }
    109.  
    110. // y-rotation
    111. void struc_widget::setYRotation(int angle)
    112. {
    113. if (angle != yrot) {
    114. yrot = angle;
    115. update();
    116. }
    117. }
    118.  
    119. // z-rotation
    120. void struc_widget::setZRotation(int angle)
    121. {
    122. if (angle != zrot) {
    123. zrot = angle;
    124. update();
    125. }
    126. }
    127.  
    128.  
    129. // normalize angles
    130. void struc_widget::normalize_angle_x(int angle)
    131. {
    132. norm_angle_x = 180.0 / h * angle;
    133. }
    134. void struc_widget::normalize_angle_y(int angle)
    135. {
    136. norm_angle_y = 180.0 / b * angle;
    137. }
    138. void struc_widget::normalize_angle_z(int angle)
    139. {
    140. norm_angle_z = 180.0 / b * angle;
    141. }
    142.  
    143. //////////////////////////////////////////////////////////////////////////////////////
    144. //////////////// structural data /////////////////////////////////////////////////////
    145. //////////////////////////////////////////////////////////////////////////////////////
    146.  
    147. void struc_widget::draw_structure()
    148. {
    149.  
    150. }
    151.  
    152.  
    153. ///////////////////////////////////////////////////////
    154. /// slot function
    155. ///////////////////////////////////////////////////////
    156. //void struc_widget::getsignal(){
    157.  
    158. /////////////////////////////////////////////////////////// END OF THE CODE
    159. ////////////////////////////////////////////////////////////////////////////////////////////////////
    160. ////////////////////////////////////////////////////////////////////////////////////////////////////
    161. ////////////////////////////////////////////////////////////////////////////////////////////////////
    162. ////////////////////////////////////////////////////////////////////////////////////////////////////
    To copy to clipboard, switch view to plain text mode 
    I already tried to solve this problem by signal - slots also and I think I had a valid connection, but how to transfer the variable nodes then????????
    Last edited by anda_skoa; 12th April 2016 at 12:32. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer variable from qmainwindow to qwidget

    What do you mean with "transfer"?

    If you don't want the data in your main window, just have it in the widget.

    If you mean passing data to the widget, then this is just a normal C++ method, a setter.

    Or do you change the data in the main window at some point into the operation and need to have the data in the widget updated?
    Does the main window need the data itself?

    Cheers,
    _

  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

    In you declaration, you want to send you node variable from struc_window to struc_widget, i thought signal/slot is a good choice. however, i did not find variable in your connect metod(maybe you want to connection by this statement //connect(this,SIGNAL(mysignal()),p,SLOT(getsignal() )); you did not active mysignal in your struc_window class. i thought you should add some code in you files.
    step1. You should add widget in your struc_window to emit you signal.
    step2. Changing your code to send node from window to widget.

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

    Default Re: Transfer variable from qmainwindow to qwidget

    Hey guys, thank you for responding!

    @anda_skoa:
    I know how to load easily the variable input.nodes from the main function into the object of class struc_window, actually I did that in the constructor. The variable nodes is an array and has drawing information, which I need in the class struc_widget, where everything should be drawn then. This is what I am trying by the pointer in the constructor of struc_widget.
    Yes, it has only to be passed once, so I will now try to realize this also by a setter.


    @ weiweiqiao:
    I will show you the code lines of the running sender slot version in a minute. But I have to admit I really don't know how to implement this in a signal so that the variable nodes is transfered, as that this signal/slot stuff seems always have to do something with pushbutton methods and not custom...

    Cheers
    Last edited by franzib; 12th April 2016 at 15:13. Reason: spelling corrections

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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
    I know how to load easily the variable input.nodes from the main function into the object of class struc_window, actually I did that in the constructor. The variable nodes is an array and has drawing information, which I need in the class struc_widget, where everything should be drawn then. This is what I am trying by the pointer in the constructor of struc_widget.
    Yes, it has only to be passed once, so I will now try to realize this also by a setter.
    I see.
    Then either pass it via a setter or just make the instance in struct_widget accessible via a getter to a reference or pointer.

    Cheers,
    _

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

    Default Re: Transfer variable from qmainwindow to qwidget

    @weiweiqiao:
    in the struc_window.h (qmainwindow) I define a dummy variable (which should be sent) and the signal:

    public:
    int a;
    signals:
    void mysignal(int);

    in the struc_window.cpp file in the constructor of struc_window I initialize my variable a and define the pointer to struc_widget and the connecter:

    a = 1;
    struc_widget *p = new struc_widget;
    connect(this,SIGNAL(mysignal(int)),p,SLOT(getsigna l(int)));

    in the struc_widget.h file i define the slot:

    public slots:
    void getsignal(int);

    and in the struc_widget.cpp file the implementation of the slot

    void struc_widget::getsignal(int){}

    This seems to compile without error!
    My question is: How can I link this procedure now with the dummy variable int a so that it is sent to struc_widget and I could use it later???

    Cheers

  7. #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; 13th April 2016 at 00:25. Reason: changed [qtclass] to [code]

  8. #8
    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 16:11. Reason: spelling corrections

  9. #9
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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, 21:49
  2. Periodically access QML variable from C++
    By mekarim in forum Newbie
    Replies: 4
    Last Post: 24th November 2015, 12:45
  3. Access variable using its name as QString
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 22nd December 2010, 10:11
  4. QtScript access variable from C++
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2009, 00:51
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 21: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.