Results 1 to 4 of 4

Thread: Strange Problem

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Strange Problem

    Hi guys, I'm having difficulty understanding how to get some global variables happening in OpenGL. Lets say I have a calculator on which I have a couple of buttons such as 0-9, X, multiply and equals. What I want to do is to be able to declare the variable X somewhere as something e.g. 3.14 - the value of pi for example and then to call that value from a variety of places.

    In OpenGL, if my understanding serves me correctly, all the magic happens inside the PainGL function which basically means that if I initialise this variable within the initialise function, it will go out of scope by the time that we get into the PainGL function and simply dumping it outside all functions produces an error. On LearnCPP website, they suggest to create a global.h with

    Qt Code:
    1. extern int x;
    To copy to clipboard, switch view to plain text mode 

    and also a global.cpp with

    Qt Code:
    1. int x = 5;
    To copy to clipboard, switch view to plain text mode 

    And then to simply include the global.h into whatever header you desire, however upon trying this method, I got the same error as I did when I simply dumped the initialisation outside of all functions inside my GLWidget class. I tried instantiating it inside the main.cpp function and inside mainwindow.cpp, however in both instances, I'm unable to gain access to those objects from within my GLWidget.cpp class.

    Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange Problem

    There is a number of solutions and what was suggested to you is probably the worst one I suggest to do it the regular C++ way -- assuming you have a class somewhere that contains your OpenGL code, store your "x" as a member variable of that class. Then you can access it from any code from that class and if you make the variable public or provide a getter for it, you'll be able to access it from outside the class too. Furthermore if the variable is constant, you can define it as "const int x" in the class or in any header file.

    As for extern -- "extern" means that "somewhere else" there is a variable declared. But it doesn't create that variable. Somewhere else you need to declare a real variable called the same for the mechanism to work.
    Last edited by wysota; 6th February 2013 at 13:29.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Strange Problem

    Thanks, what you say makes sense and I started going that rout but I think in my case it won't work as I need a more global version because if the class is destroyed via for example closing the window, I still need for the variables to be accessible and modifiable. I know it's not the best having to declare globals in this way, but I don't think I have much choice. However, having implemented the global.cpp and global.h files into my program, I have come into the problem of a runtime error:

    Microsoft Visual C++ Runtime Library

    The application has requested the Runtime library to terminate in an unusual way.

    Please contact the application's support team for more information.
    Having copied the code exactly as the instructions suggested with the declaration in the .cpp and the definition in the .h with the 'extern' keyword, or did I get that back to front? Anyway, I can compile the problem without a problem, however as soon as I go to run the program, I get this error. I get the same error when I add the declaration in other places around my program. In other words, if I declare this variable outside my 'main' function, I get the error, if I delcare it inside just before everything else gets executed, the same error occurs. If I declare it inside my OpenGL class outside all the functions that are associated with OpenGL, the same thing happens. I have tried to declare inside the PainGL function for example and the program runs without a problem but outside functions, the declarations are not liked. Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange Problem

    You always have a choice. Nobody said this class had to be your window class. When you create your window you can pass a pointer to some other external object and hold all your persistent data there. This is all basic C++ you know...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Strange problem
    By wirasto in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2009, 12:24
  2. Strange problem
    By pavel in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 22:46
  3. Very Strange problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2008, 10:20
  4. strange problem in QGraphicsView
    By Shawn in forum Qt Programming
    Replies: 3
    Last Post: 29th June 2007, 10:24

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.