PDA

View Full Version : global variable



Shuchi Agrawal
9th February 2007, 13:07
hi,
i m making a producer consumer program in Qt 4.2.2
now the problem is :
i m declaring freebytes, usedbytes variables in globalvar.h
and want to use this in producer.h and consumer.h
now i m including this producer n consumer files in form.h as i need to make its object in it
then i need to include this form.h in main.cpp
So can any one please help me in declaring and including and defining these global variables in my project?

jpn
9th February 2007, 13:18
Check the Wait Conditions Example (http://doc.trolltech.com/4.2/threads-waitconditions.html) shipped together with Qt. Does it do what you want?

jacek
9th February 2007, 13:19
i m declaring freebytes, usedbytes variables in globalvar.h
First of all you should avoid global variables as they can cause a lot of troubles. Secondly, you shouldn't define variables in header files, because every .cpp file that includes such header will have its own copy of those variables.

If you really want to use global variables you should do it this way:
// globalvar.h
...
extern int freebytes;
extern int usedbytes;
...

// globalvar.cpp
int freebytes = 0;
int usedbytes = 0;

Shuchi Agrawal
13th February 2007, 06:10
i have seen "Wait Conditions Example" example but my problem is not with threads right now. i m having problem in using the same variable in multiple cpp files and the example given is in one main cpp file.
I am attaching my file so can anyone see the code and tel me what can i do further?

[QUOTE=jacek;28772]First of all you should avoid global variables as they can cause a lot of troubles. Secondly, you shouldn't define variables in header files, because every .cpp file that includes such header will have its own copy of those variables.

ok. But can u see my code and tel me how to do it.


913

Shuchi Agrawal
13th February 2007, 12:30
hi,
sorry. this global problem is solved but i m not able to see the form after execution. why? no run time error.

jacek
13th February 2007, 20:18
class Consumer : public QThread
{
public:
Ui::Form *ui;
Consumer(Ui::Form *u);
void run();
};
This is not going to work. You shouldn't access any widgets from a non-GUI thread.

Shuchi Agrawal
14th February 2007, 05:05
This is not going to work. You shouldn't access any widgets from a non-GUI thread.

Why we should not access any widget from a non GUI thread?
And now how can i achieve what i want to do in my program?
Please specify.

jpn
14th February 2007, 08:07
Why we should not access any widget from a non GUI thread?
Thread Support in Qt (http://doc.trolltech.com/4.2/threads.html#qobject-reentrancy):


Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread

Use either signals and slots or custom events to deliver the data to the main GUI thread.

Shuchi Agrawal
14th February 2007, 11:19
hey can u explain a little more how to do tht now? i m not able to understand what to do and basically how to do?

jacek
14th February 2007, 21:33
Take a look at $QTDIR/examples/threads directory. Especially the "mandelbrot" example, which uses signals & slots mechanism to communicate between threads.

Shuchi Agrawal
15th February 2007, 05:19
thanks a lot :-)