Results 1 to 5 of 5

Thread: a QSettings global variable

  1. #1
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default a QSettings global variable

    hi all,
    I'd like to use a QSettings variable in all the functions of a class.
    I declared inside the header file myapp.h:
    Qt Code:
    1. public:
    2. QSettings settings;
    To copy to clipboard, switch view to plain text mode 

    and in the constructor inside myapp.cpp

    Qt Code:
    1. this->settings = QSettings("./settings/settings.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 

    the compiler returns the error:
    qsettings.h:304: error: ‘QSettings& QSettings::operator=(const QSettings&)’ is private

    any ideas?
    thank you

    ilpaso

  2. #2
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: a QSettings global variable

    QSettings is a class which inherits QObject, so if you declare it like this you will get this error because somewhere in the class body private section Q_DISABLE_COPY macro exists which prevents from copying which eventually can happen and what you are trying to do in your code...

    if you want to avoid this error simply follow the advice from docs which tells to declare QObject derived classes as pointers or use member methods of QSettings class to set all information you want

    take a look here
    http://doc.qt.nokia.com/stable/qobje...nment-operator
    Last edited by kornicameister; 19th February 2011 at 17:25. Reason: updated contents
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  3. The following user says thank you to kornicameister for this useful post:

    ilpaso (20th February 2011)

  4. #3
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: a QSettings global variable

    Quote Originally Posted by kornicameister View Post
    QSettings is a class which inherits QObject, so if you declare it like this you will get this error because somewhere in the class body private section Q_DISABLE_COPY macro exists which prevents from copying which eventually can happen and what you are trying to do in your code...

    if you want to avoid this error simply follow the advice from docs which tells to declare QObject derived classes as pointers or use member methods of QSettings class to set all information you want

    take a look here
    http://doc.qt.nokia.com/stable/qobje...nment-operator
    Thank you kornicameister for your help.
    I tried to use member methods of QSettings class to set all information but I can't set the filename.
    I'd like to use a .ini file in build directory. There is the constructor
    QSettings ( const QString & fileName, Format format, QObject * parent = 0 )
    but I do not find a method to set the fileName.

    Thank you.
    Regards

    ilpaso

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: a QSettings global variable

    Quote Originally Posted by ilpaso View Post
    I declared inside the header file myapp.h:
    Qt Code:
    1. public:
    2. QSettings settings;
    To copy to clipboard, switch view to plain text mode 

    and in the constructor inside myapp.cpp

    Qt Code:
    1. this->settings = QSettings("./settings/settings.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 
    Have you tried to initialise QSettings in your constructor like so ?

    Qt Code:
    1. myClass::myClass(args) : settings("./settings/settings.ini", QSettings::IniFormat)
    2. {
    3. // function body
    4. }
    To copy to clipboard, switch view to plain text mode 

    Or you can do it preferred way and use pointer in your header file and initialise using new:

    Qt Code:
    1. myClass::myClass(args) : settings(new QSettings("./settings/settings.ini", QSettings::IniFormat))
    2. {
    3. // function body
    4. }
    To copy to clipboard, switch view to plain text mode 

  6. The following 2 users say thank you to squidge for this useful post:

    Goddard (6th October 2014), ilpaso (20th February 2011)

  7. #5
    Join Date
    Sep 2010
    Location
    IT
    Posts
    20
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: a QSettings global variable

    Thank you very much squidge! An easy solution for a simple problem (but not for me!)
    It works!

    Bye
    ilpaso

Similar Threads

  1. global variable in QT: getting ISSUES
    By Girija in forum Qt Programming
    Replies: 8
    Last Post: 19th September 2010, 16:15
  2. Setting a global variable
    By Windsoarer in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2010, 22:37
  3. how to declare a global variable right
    By Cruz in forum Newbie
    Replies: 13
    Last Post: 16th February 2010, 16:25
  4. linking a slider to a global variable
    By switch in forum Newbie
    Replies: 6
    Last Post: 5th August 2009, 08:02
  5. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

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.