Results 1 to 11 of 11

Thread: How can I make an object global?

  1. #1
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How can I make an object global?

    I want to have global objects,so that I can use them inside seperate functions,how can I make them?For example,when I write

    QLabel *label1=new QLabel;

    under "#include sections",over "functions sections",it gives an error while starting debugging although it doesn't give any error while compiling.

  2. #2
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How can I make an object global?

    What do you mean global objects? If you include the file where the object is defined, you can use it. Or maybe you mean declare something on file foo.h and use it on file foo2.h?
    Also what errors the debugger reported?
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  3. #3
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I make an object global?

    Thanks for your answer.

    For example,I want to create a QLabel object(or is it called widget?) called Label1 which can be used(accessed to) from both main function and another function which I created.How can I do this?

    Debugger error:"This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information."
    Last edited by Awareness; 18th March 2010 at 00:29.

  4. #4
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How can I make an object global?

    Read this global variables.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  5. #5
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I make an object global?

    Thanks for your answer.I was thinking a Label is not a variable?

  6. #6
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How can I make an object global?

    You should really read the whole C++ tutorial in the site I gave you, not just the part about global variables.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  7. #7
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I make an object global?

    I have read it,that page doesn't recommend using global variables.But I need to access objects inside functions,how can I do it?

  8. #8
    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: How can I make an object global?

    It would be best if you used the Singleton design pattern.
    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.


  9. #9
    Join Date
    Mar 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I make an object global?

    Create a interface in the class owning the label that lets you modify it in the way you want.
    e.g: updateLabel(QString textForLabel)
    {
    //code to modify Label
    }

    then let your other class access the label through this function?

  10. #10
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I make an object global?

    Declare them outside of main() or put them in a header file
    but it is best not to use them where they can be avoided.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include "myglobals.h"
    4.  
    5. // globals here
    6. int aInteger;
    7.  
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication a(argc, argv);
    12. MainWindow w;
    13. w.show();
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JD2000; 19th March 2010 at 13:59.

  11. #11
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I make an object global?

    (Sorry for my late reply)

    Thanks for your answers.

    JD2000,I tried writing into "globals here section" but it gives an error.It works when I use it in a header file though.

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. Replies: 0
    Last Post: 15th November 2009, 09:40
  3. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 14:28
  4. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 06:33
  5. How to make an object blink in Qt?
    By thirupathiuppu007 in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2008, 10:30

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.