Results 1 to 3 of 3

Thread: QVariant

  1. #1
    Join Date
    Jul 2017
    Posts
    3
    Thanks
    2

    Arrow QVariant

    Hello every1!

    I have this code:

    Qt Code:
    1. class Model
    2. {
    3. public:
    4. Model(const QString& path);
    5. double getVehicleWeight()
    6. {
    7. m_settings->beginGroup("vehicle");
    8. const QStringList childKeys = m_settings->childKeys();
    9. foreach (const QString &childKey, childKeys)
    10. {
    11. //Here I want to return a double using QVariant, each line of my config file have (ex: weight = 60)
    12. }
    13. m_settings->endGroup();
    14. }
    15. private:
    16. void initSettings(const QString& path);
    17. protected:
    18. std::unique_ptr<QSettings> m_settings;
    19. };
    20.  
    21. Model::Model(const QString& path)
    22. {
    23. initSettings(path);
    24. }
    25.  
    26. void Model::initSettings(const QString& path)
    27. {
    28. m_settings = std::make_unique<QSettings>(path, QSettings::IniFormat);
    29. }
    30.  
    31. int main()
    32. {
    33. Model obj("/d/users/F27279C/Desktop/TESTINI/TestINI/New folder");
    34. qDebug() << "PLS WORK!";
    35. obj.getVehicleWeight();
    36. }
    To copy to clipboard, switch view to plain text mode 
    I know the ,,foreach" will go through all my document, but i dont want that
    I want ot make a getter for every line of my config file which will return the value from it
    All the values are double
    so what I ask is:
    - how can I access a specific line of my config for ex weight or speed?
    - how can I return a double using QVariant?

    thx for help!
    Last edited by high_flyer; 25th July 2017 at 10:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVariant

    If you want help, try not to make it hard for people to read your post.
    Don't use sms style text, use proper english.
    Enclose code in the code sections.

    Since you are using QSettings, what is the problem?
    QSettings allows you to access each key directly through the value() method.
    QSettings::value() returns your values as QVariant already.
    I would strongly recommend you read QSettings docs first.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QVariant

    I know the ,,foreach" will go through all my document, but i dont want that
    Why would you want to go through -all- of the child keys in your settings when you know the one you want is named "weight"?

    Qt Code:
    1. double getVehicleWeight() const
    2. {
    3. return m_settings->value( "vehicle/weight" ).toDouble();
    4. }
    To copy to clipboard, switch view to plain text mode 

    beginGroup() and endGroup() are unnecessary, and by eliminating them you can make the getVehicleWeight() method const which might help the compiler optimize your code more efficiently.
    Last edited by d_stranz; 25th July 2017 at 19:00.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QVariant::QVariant(Qt::GlobalColor)' is private
    By alenn.masic in forum Qt Programming
    Replies: 7
    Last Post: 22nd February 2013, 17:29
  2. QVariant error
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2012, 01:46
  3. Replies: 4
    Last Post: 4th January 2011, 13:07
  4. Replies: 1
    Last Post: 4th December 2009, 18:03
  5. QVariant question
    By yunpeng880 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2009, 01:43

Tags for this Thread

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.