Results 1 to 4 of 4

Thread: Access to UI elements from another class c++

  1. #1
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Access to UI elements from another class c++

    Hello,

    I have a problem with accessing to ui elements from another class(with instance). I have a second QMainWindow in my application, I can access in secondWindow.cxx class all ui elements but not in read.cxx class. My code looks like following. Where is my mistake? Thank you for your help.

    Qt Code:
    1. //-------------------------------secondWindow.h------------------------------------
    2. #ifndef __secondWindow_h
    3. #define __secondWindow_h
    4.  
    5. #include "ui_secondwindow.h"
    6.  
    7. class secondWindow : public QMainWindow
    8. {
    9. friend class read;
    10. igstkStandardClassBasicTraitsMacro(secondWindow, QMainWindow);
    11. Q_OBJECT
    12.  
    13. public:
    14. igstkStateMachineMacro();
    15.  
    16. secondWindow();
    17. virtual ~secondWindow();
    18. void createSignalAndSlots();
    19.  
    20. public slots:
    21. void secondWindowTest();
    22.  
    23. protected:
    24.  
    25. private:
    26. Ui::secondMainWindow m_secondWindowUI;
    27. };
    28. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //-------------------------------secondWindow.cxx------------------------------------
    2. #include "secondWindow.moc"
    3. #include "secondWindow.h"
    4. #include "read.h"
    5.  
    6. secondWindow::secondWindow() :m_StateMachine(this)
    7. {
    8. m_secondWindowUI.setupUi(this);
    9. createSignalAndSlots();
    10. }
    11.  
    12. void secondWindow::createSignalAndSlots()
    13. {
    14. connect(m_secondWindowUI.pushButton1, SIGNAL(clicked()),this, SLOT(secondWindowTest()));
    15.  
    16. connect(m_secondWindowUI.pushButton2, SIGNAL(clicked()), read::instance(), SLOT(readTest()));
    17. }
    18.  
    19. void secondWindow::secondWindowTest()
    20. {
    21. m_secondWindowUI.pushButton1->setEnabled(true); //OK
    22. }
    23.  
    24. secondWindow::~secondWindow(){}
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //---------------------------------read.h--------------------------------------
    2. #pragma once
    3.  
    4. #include "secondWindow.h"
    5.  
    6. class read : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. static read *instance();
    12. read();
    13. virtual ~read() {}
    14.  
    15. public slots:
    16. void readTest();
    17.  
    18. protected:
    19. secondWindow *m_readUI;
    20. static read *m_read;
    21.  
    22. private:
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //---------------------------------read.cxx--------------------------------------
    2. #include <read.moc>
    3. #include "secondWindow.h"
    4. #include "read.h"
    5.  
    6. read *read::m_read= NULL;
    7.  
    8. read::read()
    9. {
    10. m_readUI = dynamic_cast<secondWindow*>( QApplication::instance() );
    11. }
    12.  
    13. read *read::instance()
    14. {
    15. if(m_read == NULL)
    16. m_read = new read();
    17.  
    18. return m_read;
    19. }
    20.  
    21. void read::readTest()
    22. {
    23. m_readUI->m_secondWindowUI.qlabelTest->setText("test"); //segmentation fault
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nasil122002; 11th April 2013 at 20:51.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access to UI elements from another class c++

    That dynamic cast will fail because main window is not related to qapplication. You should pass secondwindow ptr into read constructor. You shouldn't need to use statics.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access to UI elements from another class c++

    sorry, how can I do this?

  4. #4
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access to UI elements from another class c++

    Quote Originally Posted by amleto View Post
    That dynamic cast will fail because main window is not related to qapplication. You should pass secondwindow ptr into read constructor. You shouldn't need to use statics.
    Hello,

    Sorry for late reply. now segmentation fault does not occur. I have insert also a new class "app", in which I needed initializations (pointers of UI's) doing. But this time I have another problem. When I click on "pushButton2" on the "secondWindow", nothing happens, no reaction. Probably. should I post my whole program so you can help me further.

    Thank you.

    Qt Code:
    1. -----------------------------mainWindow.h----------------------------------
    2. #ifndef __mainWindow_h
    3. #define __mainWindow_h
    4.  
    5. #include "ui_mainWindow.h"
    6. #include "app.h"
    7.  
    8. class mainWindow : public QMainWindow
    9. {
    10. friend class app;
    11. friend class write;
    12. igstkStandardClassBasicTraitsMacro(mainWindow, QMainWindow);
    13. Q_OBJECT
    14.  
    15. public:
    16. igstkStateMachineMacro();
    17.  
    18. mainWindow();
    19. virtual ~mainWindow();
    20. void createSignalAndSlots();
    21.  
    22. public slots:
    23. void openSecondWindow();
    24. void mainWindowTest();
    25.  
    26. private:
    27. Ui::mainWindow m_mainWindowUI;
    28. };
    29. #endif
    30.  
    31. -----------------------------mainWindow.cxx----------------------------------
    32. #include "mainWindow.moc"
    33. #include "mainWindow.h"
    34. #include "secondWindow.h"
    35. #include "read.h"
    36. #include "write.h"
    37.  
    38. mainWindow::mainWindow() : m_StateMachine(this)
    39. {
    40. m_mainWindowUI.setupUi(this);
    41. createSignalAndSlots();
    42. }
    43.  
    44. mainWindow::~mainWindow(){}
    45.  
    46.  
    47. void mainWindow::createSignalAndSlots()
    48. {
    49. connect(m_mainWindowUI.menuOpenAction, SIGNAL(triggered()), this, SLOT(openSecondWindow()));
    50. connect(m_mainWindowUI.pushButton1, SIGNAL(clicked()), this, SLOT((mainWindowTest()));
    51. connect(m_mainWindowUI.pushButton2, SIGNAL(clicked()), write::instance(), SLOT((writeTest()));
    52. }
    53.  
    54. void mainWindow::openSecondWindow()
    55. {
    56. secondWindow *secondWin = new secondWindow();
    57. secondWin->show();
    58. }
    59.  
    60.  
    61. void mainWindow::mainWindowTest()
    62. {
    63. m_mainWindowUI.pushButton1->setEnabled(true); //OK, it works
    64. }
    65.  
    66. -----------------------------write.h----------------------------------
    67. #pragma once
    68.  
    69. #include "mainWindow.h"
    70.  
    71. class write : public QObject
    72. {
    73. Q_OBJECT
    74.  
    75. public:
    76. static write *instance();
    77. write();
    78. virtual ~write() {}
    79.  
    80. public slots:
    81. void writeTest();
    82.  
    83. protected:
    84. app *m_writeUI;
    85. static write *m_write;
    86.  
    87. private:
    88. };
    89.  
    90. -----------------------------write.cxx----------------------------------
    91. #include <write.moc>
    92. #include "mainWindow.h"
    93. #include "write.h"
    94.  
    95. write *write::m_write= NULL;
    96.  
    97. write::write()
    98. {
    99. m_writeUI = dynamic_cast<app*>(QApplication::instance());
    100. }
    101.  
    102. write *write::instance()
    103. {
    104. if(m_write == NULL)
    105. m_write = new write();
    106.  
    107. return m_write;
    108. }
    109.  
    110. void write::writeTest()
    111. {
    112. m_writeUI->m_mainWindowUI.mainQLabelTest->setText("main label test"); //OK, it works
    113. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. -----------------------------secondWindow.h----------------------------------
    2. #ifndef __secondWindow_h
    3. #define __secondWindow_h
    4.  
    5. #include "ui_secondWindow.h"
    6. #include "app.h"
    7.  
    8. class secondWindow : public QMainWindow
    9. {
    10. friend class read;
    11. friend class app;
    12. igstkStandardClassBasicTraitsMacro(secondWindow, QMainWindow);
    13. Q_OBJECT
    14.  
    15. public:
    16. igstkStateMachineMacro();
    17.  
    18. secondWindow(QWidget *parent= 0);
    19. virtual ~secondWindow();
    20. void createSignalAndSlots();
    21.  
    22. public slots:
    23. void secondWindowTest();
    24.  
    25. private:
    26. Ui::secondWindow m_secondWindowUI;
    27. };
    28. #endif
    29.  
    30. -----------------------------secondWindow.cxx----------------------------------
    31. #include "secondWindow.moc"
    32. #include "secondWindow.h"
    33. #include "read.h"
    34.  
    35. secondWindow::secondWindow(QWidget *parent) :m_StateMachine(this)
    36. {
    37. m_secondWindowUI.setupUi(this);
    38. createSignalAndSlots();
    39. }
    40.  
    41. secondWindow::~secondWindow(){}
    42.  
    43. void secondWindow::createSignalAndSlots()
    44. {
    45. connect(m_secondWindowUI.pushButton1, SIGNAL(clicked()),this, SLOT(secondWindowTest()));
    46. connect(m_secondWindowUI.pushButton2, SIGNAL(clicked()), read::instance(), SLOT(readTest()));
    47. }
    48.  
    49. void secondWindow::secondWindowTest()
    50. {
    51. m_secondWindowUI.pushButton1->setEnabled(true); //OK, it works
    52. }
    53.  
    54. -----------------------------read.h----------------------------------
    55. #pragma once
    56.  
    57. #include "secondWindow.h"
    58.  
    59. class read : public QObject
    60. {
    61. Q_OBJECT
    62.  
    63. public:
    64. static read *instance();
    65. read();
    66. virtual ~read() {}
    67.  
    68. public slots:
    69. void readTest();
    70.  
    71. protected:
    72. app *m_readUI;
    73. static read *m_read;
    74.  
    75. private:
    76. };
    77.  
    78. -----------------------------read.cxx----------------------------------
    79. #include <read.moc>
    80. #include "secondWindow.h"
    81. #include "read.h"
    82.  
    83. read *read::m_read= NULL;
    84.  
    85. read::read()
    86. {
    87. m_readUI = dynamic_cast<app*>(QApplication::instance());
    88. }
    89.  
    90. read *read::instance()
    91. {
    92. if(m_read == NULL)
    93. m_read = new read();
    94.  
    95. return m_read;
    96. }
    97.  
    98. void read::readTest()
    99. {
    100. m_readUI->m_secondWindowUI.secondQLabelTest->setText("second label test"); //ERROR, it works but no reaction
    101. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. -----------------------------app.h----------------------------------
    2. #pragma once
    3.  
    4. class mainWindow;
    5. class secondWindow;
    6.  
    7. class app : public QApplication
    8. {
    9. Q_OBJECT
    10. igstkStandardClassBasicTraitsMacro(app, QApplication);
    11.  
    12. friend class read;
    13. friend class write;
    14. friend class mainWindow;
    15. friend class secondWindow;
    16.  
    17. public:
    18. app(int& argc, char **argv);
    19. virtual ~app() {}
    20.  
    21. protected:
    22. mainWindow *m_mainWin;
    23. secondWindow *m_secondWin;
    24.  
    25. void setMainWindow(mainWindow *mainWindow)
    26. {
    27. m_mainWin = mainWindow;
    28. }
    29.  
    30. void setSecondWindow(secondWindow *secondWindow)
    31. {
    32. m_secondWin = secondWindow;
    33. }
    34. private:
    35. };
    36.  
    37. -----------------------------app.cxx----------------------------------
    38. #include "app.moc"
    39. #include "app.h"
    40.  
    41. app::app(int& argc, char **argv): QApplication(argc, argv), m_StateMachine(this){
    42. }
    43.  
    44. -----------------------------main.cxx----------------------------------
    45. #include "mainWindow.h"
    46. #include "secondWindow.h"
    47. #include "app.h"
    48.  
    49. int main(int argc, char** argv)
    50. {
    51. app app(argc, argv);
    52.  
    53. mainWindow main;
    54. secondWindow second;
    55.  
    56. app.setMainWindow(&main);
    57. app.setSecondWindow(&second);
    58.  
    59. main.showMaximized();
    60.  
    61. return app.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 2nd April 2013, 09:13
  2. Access elements of the xml-file
    By 8Observer8 in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2013, 18:27
  3. SVG: access coordinates of elements
    By bmpix in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2012, 23:29
  4. Replies: 4
    Last Post: 29th May 2010, 12:56
  5. Replies: 4
    Last Post: 6th August 2009, 06:12

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.