Access to UI elements from another class Qt-C++

Hello,
I have a problem with accessing ui elements from another class(with instance). I have a second QMainWindow in my application, I can access in mainWindow.cxx, write.cxx and secondWindow.cxx class all ui elements but not in read.cxx class. When I click on "pushButton2" on the "secondWindow", nothing happens,
no reaction. My code looks like following. Where is my mistake? Thank you for any help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef __mainWindow_h
#define __mainWindow_h

#include "ui_mainWindow.h"
#include "app.h"

class mainWindow : public QMainWindow
{
	friend class app;
	friend class write;
	igstkStandardClassBasicTraitsMacro(mainWindow, QMainWindow);
	Q_OBJECT

public:
	igstkStateMachineMacro();

	mainWindow();
	virtual ~mainWindow();
	void createSignalAndSlots();

public slots:
	void openSecondWindow();
	void mainWindowTest();

private:
	Ui::mainWindow	m_mainWindowUI;
};
#endif

-----------------------------mainWindow.cxx----------------------------------
#include "mainWindow.moc"
#include "mainWindow.h"
#include "secondWindow.h"
#include "read.h"
#include "write.h"

mainWindow::mainWindow() : m_StateMachine(this)
{
	m_mainWindowUI.setupUi(this);
	createSignalAndSlots();
}

mainWindow::~mainWindow(){}


void mainWindow::createSignalAndSlots()
{
	connect(m_mainWindowUI.menuOpenAction, SIGNAL(triggered()), this, SLOT(openSecondWindow()));
	connect(m_mainWindowUI.pushButton1, SIGNAL(clicked()), this, SLOT((mainWindowTest()));
	connect(m_mainWindowUI.pushButton2, SIGNAL(clicked()), write::instance(), SLOT((writeTest()));
}

void mainWindow::openSecondWindow()
{
	secondWindow  *secondWin = new secondWindow(); 
	secondWin->show();
}


void mainWindow::mainWindowTest()
{
   m_mainWindowUI.pushButton1->setEnabled(true); //OK, it works
}

-----------------------------write.h----------------------------------
#pragma once

#include "mainWindow.h"

class write : public QObject
{
    Q_OBJECT

public:
    static write *instance();
    write();
    virtual ~write() {}

public slots:
    void writeTest();

protected:
    app		        *m_writeUI;
    static write 	*m_write;

private:
};

-----------------------------write.cxx----------------------------------
#include <write.moc>
#include "mainWindow.h"
#include "write.h"

write *write::m_write= NULL;

write::write()
{
	m_writeUI = dynamic_cast<app*>(QApplication::instance());
}

write *write::instance()
{
    if(m_write == NULL)
    m_write = new write();
        
    return m_write;
}

void write::writeTest()
{
    m_writeUI->m_mainWindowUI.mainQLabelTest->setText("main label test"); //OK, it works
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef __secondWindow_h
#define __secondWindow_h

#include "ui_secondWindow.h"
#include "app.h"

class secondWindow : public QMainWindow
{
    friend class read;
	friend class app;
    igstkStandardClassBasicTraitsMacro(secondWindow, QMainWindow);
    Q_OBJECT

public:
    igstkStateMachineMacro();

    secondWindow(QWidget *parent= 0);
    virtual ~secondWindow();
    void createSignalAndSlots();

public slots:
    void secondWindowTest();

private:
    Ui::secondWindow	m_secondWindowUI;
};
#endif

-----------------------------secondWindow.cxx----------------------------------
#include "secondWindow.moc"
#include "secondWindow.h"
#include "read.h"

secondWindow::secondWindow(QWidget *parent) :m_StateMachine(this)
{
    m_secondWindowUI.setupUi(this);
    createSignalAndSlots();
}

secondWindow::~secondWindow(){}

void secondWindow::createSignalAndSlots()
{
    connect(m_secondWindowUI.pushButton1, SIGNAL(clicked()),this, SLOT(secondWindowTest()));
    connect(m_secondWindowUI.pushButton2, SIGNAL(clicked()), read::instance(), SLOT(readTest()));
}

void secondWindow::secondWindowTest()
{
    m_secondWindowUI.pushButton1->setEnabled(true); //OK, it works
}

-----------------------------read.h----------------------------------
#pragma once

#include "secondWindow.h"

class read : public QObject
{
    Q_OBJECT

public:
    static read *instance();
    read();
    virtual ~read() {}

public slots:
    void readTest();

protected:
    app		        *m_readUI;
    static read 	*m_read;

private:
};

-----------------------------read.cxx----------------------------------
#include <read.moc>
#include "secondWindow.h"
#include "read.h"

read *read::m_read= NULL;

read::read()
{
    m_readUI = dynamic_cast<app*>(QApplication::instance());
}

read *read::instance()
{
    if(m_read == NULL)
    m_read = new read();
        
    return m_read;
}

void read::readTest()
{
    m_readUI->m_secondWindowUI.secondQLabelTest->setText("second label test"); //ERROR, it works but no reaction 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

class mainWindow; 
class secondWindow;

class app : public QApplication
{
  Q_OBJECT
  igstkStandardClassBasicTraitsMacro(app, QApplication);

  friend class read;
  friend class write;
  friend class mainWindow;
  friend class secondWindow;

public:
  app(int& argc, char **argv);
  virtual ~app() {}

protected:
	mainWindow	*m_mainWin;
	secondWindow	*m_secondWin;

	void setMainWindow(mainWindow *mainWindow)
	{
		m_mainWin = mainWindow;
	}

	void setSecondWindow(secondWindow *secondWindow)
	{
		m_secondWin = secondWindow;
	}
private:
};

-----------------------------app.cxx----------------------------------
#include "app.moc"
#include "app.h"

app::app(int& argc, char **argv): QApplication(argc, argv), m_StateMachine(this){
}

-----------------------------main.cxx----------------------------------
#include "mainWindow.h"
#include "secondWindow.h"
#include "app.h"

int main(int argc, char** argv)
{ 
	app app(argc, argv);

	mainWindow main;
	secondWindow second;

	app.setMainWindow(&main);
	app.setSecondWindow(&second);
 
	main.showMaximized();

	return app.exec();
}

Last edited on
Topic archived. No new replies allowed.