Results 1 to 11 of 11

Thread: global variable

  1. #1
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy global variable

    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?
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: global variable

    Check the Wait Conditions Example shipped together with Qt. Does it do what you want?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable

    Quote Originally Posted by Shuchi Agrawal View Post
    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:
    Qt Code:
    1. // globalvar.h
    2. ...
    3. extern int freebytes;
    4. extern int usedbytes;
    5. ...
    6.  
    7. // globalvar.cpp
    8. int freebytes = 0;
    9. int usedbytes = 0;
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to jacek for this useful post:

    Shuchi Agrawal (13th February 2007)

  5. #4
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: global variable

    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.


    Threading.zip
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  6. #5
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: global variable

    hi,
    sorry. this global problem is solved but i m not able to see the form after execution. why? no run time error.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable

    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.

  8. #7
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: global variable

    Quote Originally Posted by jacek View Post
    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.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: global variable

    Quote Originally Posted by Shuchi Agrawal View Post
    Why we should not access any widget from a non GUI thread?
    Thread Support in Qt:
    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.
    J-P Nurmi

  10. #9
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: global variable

    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?
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable

    Take a look at $QTDIR/examples/threads directory. Especially the "mandelbrot" example, which uses signals & slots mechanism to communicate between threads.

  12. #11
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: global variable

    thanks a lot :-)
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

Similar Threads

  1. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  3. custom plugin designer property with out a variable?
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:11
  4. Creating a global array in my code???
    By therealjag in forum General Programming
    Replies: 5
    Last Post: 13th March 2006, 11:13
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

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.