Results 1 to 4 of 4

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

Threaded View

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

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
  •  
Qt is a trademark of The Qt Company.