Results 1 to 10 of 10

Thread: Transfer variable from qmainwindow to qwidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.