You are not logged in.

1

Tuesday, May 3rd 2005, 5:13pm

access .ui.h variables from .cpp file

Hello all!

Here is my problem :

I have made my program in QT designer.
I have an object in my widget, and for all of my functions in my .ui.h, i can modify its properties like this :
progressBar->setProgress(69);

But now, instead of coding in this .ui.h, i want to code in an external .cpp file, but i can't compile it. What's wrong ?

Source code

1
2
3
4
5
6
7
[B]fonc.cpp[/B]
#include "fonc.h"

void bidon()
{
   progressBarload_2->setProgress(69);
}

Source code

1
2
3
4
5
[B]fonc.h[/B]
#include <qprogressbar.h>
#include "dlgmain.h"

void bidon();

Source code

1
2
3
4
5
6
7
8
[B]output[/B]
bash-2.05b$ make
g++ -c -pipe -Wall -W -O2 [...] -I/usr/qt/3/include -o fonc.o fonc.cpp
fonc.cpp: In function `void bidon()':
fonc.cpp:5: error: `progressBarload_2' undeclared (first use this function)
fonc.cpp:5: error: (Each undeclared identifier is reported only once for each
   function it appears in.)
make: *** [fonc.o] Error 1

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Tuesday, May 3rd 2005, 5:38pm

RE: access .ui.h variables from .cpp file

Quoted

Originally posted by fensoft
But now, instead of coding in this .ui.h, i want to code in an external .cpp file, but i can't compile it. What's wrong?

Each .ui file will be converted by uic to a class and .ui.h file contains implementation of that class' methods --- that's why you can access those widgets directly from there.

From the outside you must access your widgets like member variables of that autogenerated class (this also means that you need to instantiate it first).

3

Tuesday, May 3rd 2005, 5:57pm

RE: access .ui.h variables from .cpp file

Quoted

Originally posted by jacek
this also means that you need to instantiate it first


And... what must i do to instantiate it ?

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

4

Tuesday, May 3rd 2005, 6:18pm

RE: access .ui.h variables from .cpp file

It depends how do you want to use that from. If it's your main form, you could use this (assuming that your class is named dlgmain):

Source code

1
2
3
dlgmain *dlg = new dlgmain(0, "main dialog");
// or
dlgmain dlg(0, "main dialog");
If it isn't the main form, you need to put a pointer to its parent instead of 0.

5

Tuesday, May 3rd 2005, 11:30pm

yes, in fact i allready have that in my main.cpp

Source code

1
2
3
4
5
main.cpp
dlgMain *m = new dlgMain();
a.setMainWidget( m );
m->show();
return a.exec();

but, when i try that in my func.cpp

Source code

1
2
func.cpp
m->progressBarload_2->setProgress(69);
that doesn't work... so, what's wrong ? what must i do to make 'm' public ?

ps : i'm a beginner in c++

This post has been edited 2 times, last edit by "fensoft" (May 3rd 2005, 11:34pm)


6

Wednesday, May 4th 2005, 1:12am

Quoted

Originally posted by fensoft
what must i do to make 'm' public ?

ps : i'm a beginner in c++


You would have to either make the pointer a global variable (which is a bad idea) or pass it as a parameter to your bidon() function.

But the bigger question is why are you trying to do it that way? Your code should probably be totally contained in the member functions of the classes that you instantiate (unless your requirements are such that you do in fact need a global function).

GreyGeek

Unregistered

7

Tuesday, July 12th 2005, 4:12pm

RE: access .ui.h variables from .cpp file

Here is a stripped version of a project I am working on:

Source code

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//main.cpp
#include "haptest.h"
#include <QtGui/QApplication>

int main( int argc, char * argv[] )
{
    QApplication app(argc, argv);
    haptest ht;
    ht.show();
    return app.exec();
}



//haptest.h
#ifndef HAPTEST_H
#define HAPTEST_H
#include <QMainWindow>
#include <QString>
#include "ui_haptest.h"

class haptest : public QMainWindow
{
	Q_OBJECT
	
public:
	   haptest(QWidget *parent = 0);

private:
        Ui::haptest ui;
	void enableProperty();
	void disableProperty();
	void enablePersInfo();
	void disablePersinfo();
	void toggleReject();
	QString itos( int num );


		
		
         
private slots:
	void QuitClicked();
	void editProperty();
	void editPropertyUndo();
	void editPersinfo();
	void editPersInfoUndo();
	void passMsNum( int cNum);
	void passApptype (int cNum);
	void passCntyNum( int cNum );
		
};
#endif


//haptest.cpp
#include "haptest.h"
#include <sstream>
#include <iostream>
#include <string>

 haptest::haptest(QWidget *parent)
        : QMainWindow(parent)
{
 		ui.setupUi(this);
   
   		QStringList cntyList;
   		cntyList << "ADAMS" << "ANTELOPE" << "ARTHUR" << "BANNER" << "BLAINE"
		   << "BOONE" << "BOX BUTTE" << "BOYD" << "BROWN" << "BUFFALO"
		   [ .... snip a lot of counties ...]
		   << "THURSTON" << "VALLEY" << "WASHINGTON" << "WAYNE" << "WEBSTER"
		   << "WHEELER" << "YORK" << "OUTSTATE" ;
   		
		   
		ui.cboCountyName->addItems( cntyList ); 

		QStringList msList;
		msList << "Single" << "Married" << "Widow" << "Closely Related";
		ui.cboMS->addItems( msList );
  
  		QStringList appList;
		appList << "Applicant" << "Applicant & Spouse" << "Spouse" << "Other Owner/Occupant";
		ui.cboAppType->addItems( appList );
		
		ui.btnUndoPersInfo->hide();
		ui.btnUndoProperty->hide();   
  
  		ui.leSearch->setFocus();
  		connect(ui.btnEditPersInfo, SIGNAL(clicked()), this, SLOT(editPersinfo()));
		connect(ui.btnUndoPersInfo, SIGNAL(clicked()), this, SLOT(editPersInfoUndo()));
		connect(ui.btnEditProperty, SIGNAL(clicked()), this, SLOT(editProperty()));
		connect(ui.btnUndoProperty, SIGNAL(clicked()), this, SLOT(editPropertyUndo()));
		connect(ui.cboCountyName, SIGNAL(highlighted(int)), this, SLOT(passCntyNum( int )));
		connect(ui.cboMS, SIGNAL(highlighted(int)), this, SLOT(passMsNum( int )));
		connect(ui.cboAppType, SIGNAL(highlighted(int)), this, SLOT(passApptype( int )));

}


// add other functions and slots here

void haptest::QuitClicked()
{
	/* not called because Quit button connects to close event on form 
	   but will redirect close event to here for tests of db state
	 */
	exit(0);
}


void haptest::enableProperty() {
	 ui.cboCountyName->setEnabled(true);
	 ui.txtLegal->setEnabled(true);
	 ui.leParcelID->setEnabled(true);
	 ui.leTaxDistrict->setEnabled(true);
	 ui.leHomeValue->setEnabled(true);
	 ui.teNotes->setEnabled(true);
	 ui.btnEditProperty->setText("Save");
	 ui.btnUndoProperty->setEnabled(true);
	 ui.btnUndoProperty->show();
	 ui.cboCountyName->setFocus();

}


void haptest::disableProperty() {
	 ui.btnEditProperty->setText("Edit");
	 ui.cboCountyName->setEnabled(false);
	 ui.txtLegal->setEnabled(false);
	 ui.leParcelID->setEnabled(false);
	 ui.leTaxDistrict->setEnabled(false);
	 ui.leHomeValue->setEnabled(false);
	 ui.teNotes->setEnabled(false);
	 ui.btnUndoProperty->setEnabled(false);
	 ui.btnUndoProperty->hide();
	 ui.btnEditProperty->setFocus();

}


void haptest::editPropertyUndo() {
	 disableProperty();
	 ui.leStatus->setText("Property edits Undone!");
 	// rollback db changes  here


 	 ui.leSearch->setFocus();
}


void haptest::editProperty() {
	 if (ui.btnEditProperty->text() == "Edit"){
	 	enableProperty();
		} else {
		  disableProperty();
		  //call to code to update db goes here
        }
}


void haptest::editPersinfo() {
	 if (ui.btnEditPersInfo->text() == "Edit"){
	 	ui.leStatus->setText("PersInfo edits enabled!");
		enablePersInfo();
		} else {
		  ui.leStatus->setText("PersInfo edits disabled!");
		  disablePersinfo();
		}
}


void haptest::enablePersInfo() {
	 ui.btnEditPersInfo->setText("Save");
	 ui.btnUndoPersInfo->setEnabled(true);
	 ui.btnUndoPersInfo->show();
	 ui.leWholename->setEnabled(true);
	 
	 [... snip a lot of widget settings...]
	 
	 ui.leAppStatusInfo->setEnabled(true);
	 ui.btnVNI->setEnabled(true);
	 ui.btnReject->setEnabled(true);
}


void haptest::disablePersinfo() {
	 ui.btnEditPersInfo->setText("Edit");
	 ui.btnUndoPersInfo->setEnabled(false);
	 ui.btnUndoPersInfo->hide();
	 ui.leWholename->setEnabled(false);
	 
	 [...snip a lot of widget settings ...]
	 
	 ui.leAppStatusInfo->setEnabled(false);
	 ui.btnVNI->setEnabled(false);
	 ui.btnReject->setEnabled(false);
}


void haptest::editPersInfoUndo() {
	 disablePersinfo();
	 ui.leStatus->setText("PersInfo edits NOT saved!");
 	// rollback db changes  here if needed
 
 	 ui.leSearch->setFocus();
}


void haptest::toggleReject() {
 // put reject test code here
 if (ui.leAppStatusInfo->text()==""){
 	 ui.leAppStatusInfo->setText("Rejected");
 } else {
   	ui.leAppStatusInfo->setText("");
 }
}


void haptest::passCntyNum( int cNum ) {
	 //QString s = itos(cNum + 1);
	 QString  s;
	 s = s.setNum(cNum + 1);
	 ui.leCountyNum->setText(s);
	 ui.leCountyNum->repaint();
}

void haptest::passMsNum( int cNum) {
	 //QString s = itos(cNum + 1);
	 QString  s;
	 s = s.setNum(cNum + 1);
	 ui.lems->setText(s);
	 ui.lems->repaint();
}


void haptest::passApptype (int cNum) {
	 //QString s = itos(cNum + 1);
	 QString  s;
	 s = s.setNum(cNum + 1);
	 ui.leAppType->setText(s);
	 ui.leAppType->repaint();
}