Results 1 to 9 of 9

Thread: QSettings dont save updated variables by QML

  1. #1
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QSettings dont save updated variables by QML

    Hello, I have an app where user can change a variables - delays, hotkeys etc. I do the "config saver" but it recieve "old" (not updated) variables. "Variable check" in same class working, but in other class shows others.

    Qt Code:
    1. void Building::setWindowName(QString value){
    2. connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    3. sett.windowName = value;
    4. sett.windowID = (const wchar_t*) sett.windowName.utf16();
    5. qDebug() << "windowName " << sett.windowName; //Shows updated
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Settings : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Settings(QObject *parent = nullptr);
    7. QSettings* sett;
    8.  
    9. int toggleButton=0x52;
    10. int btoggleButton=0x52;// r
    11. int inventoryKey=0x45; // e
    12. int minCpsDelay = 10;
    13. int maxCpsDelay = 15;
    14.  
    15. QString windowName = "Minecraft 1.8.8 (Blazingpack.pl)";
    16. LPCWSTR windowID = (const wchar_t*) windowName.utf16();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Settings::clickcheck()
    2. {
    3. qDebug() << windowName; //Showed old - not updated variable.
    4. }
    To copy to clipboard, switch view to plain text mode 


    I just want to save some data from different classes.
    Last edited by Bedopies; 12th May 2019 at 01:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSettings dont save updated variables by QML

    Are you checking the values on the same object?

    I.e. is the Settings instance of your first code snippet the same as the one from the last snippet?

    Cheers,
    _

  3. #3
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSettings dont save updated variables by QML

    Quote Originally Posted by anda_skoa View Post
    Are you checking the values on the same object?
    _
    In my opinion - yes, because I try to change sett.windowName what is declared in Settings class.

    Quote Originally Posted by anda_skoa View Post
    I.e. is the Settings instance of your first code snippet the same as the one from the last snippet?
    Cheers,
    _
    I dont understand question. In first code snippet I want to set Setting's windowName from Building class.
    In 2nd snippet, it shows this variable in Settings class.
    In 3rd snippet, I try to show this changed variable.

    Idk, worth to do it like this? Keep this all "user-changable" variables in one class for QSettings only.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSettings dont save updated variables by QML

    Quote Originally Posted by Bedopies View Post
    I dont understand question.
    Your first code snippet suggests that the Building class has a member called "sett".

    Do you call the "clickcheck()" method on that "sett" object?
    Or are you accidentally calling it on a different Settings object?

    Cheers,
    _

  5. #5
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSettings dont save updated variables by QML

    I called Clickcheck() from QML with
    Qt Code:
    1. Settings.clickcheck();
    To copy to clipboard, switch view to plain text mode 
    and when I pressed mouse area this show me old variable.

    There's another code with same "error":
    Qt Code:
    1. #ifndef KEPPOXD_H
    2. #define KEPPOXD_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6.  
    7. class keppoxd : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit keppoxd(QObject *parent = nullptr);
    12. QString name = "old";
    13.  
    14. signals:
    15. void textChanged(QString);
    16.  
    17. public slots:
    18. void setWindowName(QString value);
    19. void test();
    20. };
    21.  
    22. #endif // KEPPOXD_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef SIEMA_H
    2. #define SIEMA_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <keppoxd.h>
    7.  
    8.  
    9. class siema : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit siema(QObject *parent = nullptr);
    14. keppoxd keppo;
    15.  
    16. signals:
    17. void textChanged(QString);
    18.  
    19. public slots:
    20. void setWindowName(QString value);
    21.  
    22. };
    23.  
    24. #endif // SIEMA_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "keppoxd.h"
    2.  
    3. keppoxd::keppoxd(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. void keppoxd::setWindowName(QString value)
    9. {
    10. connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    11. name = value;
    12. qDebug() << name;
    13. }
    14.  
    15. void keppoxd::test()
    16. {
    17. qDebug() << "keppo test " << name;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QSettings>
    4. #include <QDir>
    5. #include <QDebug>
    6. #include <keppoxd.h>
    7. #include <siema.h>
    8. #include <QQmlContext>
    9. #include <QQmlApplicationEngine>
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13.  
    14. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    15.  
    16. QGuiApplication app(argc, argv);
    17. //QSettings* settings;
    18.  
    19. /*if(QFileInfo::exists(QDir::currentPath() + "/my_config_file.ini")){
    20.   qDebug() << "istnieje";
    21.   }
    22.   else{
    23.   qDebug() << "nie istnieje";
    24.  
    25.   settings = new QSettings(QDir::currentPath() + "/my_config_file.ini", QSettings::IniFormat);
    26.   settings->setValue("test", "value");
    27.   settings->setValue("Button", 1);
    28.   settings->sync();
    29.   } */
    30.  
    31.  
    32. siema siema;
    33. keppoxd keppo;
    34.  
    35. QQmlApplicationEngine engine;
    36. const QUrl url(QStringLiteral("qrc:/main.qml"));
    37. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    38. &app, [url](QObject *obj, const QUrl &objUrl) {
    39. if (!obj && url == objUrl)
    40. QCoreApplication::exit(-1);
    41. }, Qt::QueuedConnection);
    42. engine.load(url);
    43.  
    44.  
    45. QQmlContext *ctx = engine.rootContext();
    46.  
    47. ctx->setContextProperty("Siema", &siema);
    48. ctx->setContextProperty("Keppo", &keppo);
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "siema.h"
    2.  
    3. siema::siema(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. void siema::setWindowName(QString value)
    9. {
    10. connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    11. keppo.name = value;
    12. qDebug() << keppo.name;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. import QtQuick 2.9
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. visible: true
    6. width: 640
    7. height: 480
    8. title: qsTr("Hello World")
    9.  
    10. TextInput {
    11. id: textInput
    12. x: 48
    13. y: 58
    14. width: 80
    15. height: 20
    16. text: qsTr("Text Input")
    17. font.pixelSize: 12
    18. onTextEdited:{
    19. Siema.setWindowName(textInput.text);
    20. Keppo.test();
    21. }
    22. }
    23.  
    24. Rectangle {
    25. id: rectangle
    26. x: 276
    27. y: 41
    28. width: 200
    29. height: 200
    30. color: "#e30a0a"
    31.  
    32. MouseArea {
    33. id: mouseArea
    34. x: 0
    35. y: 0
    36. width: 200
    37. height: 200
    38. onClicked:{
    39. Siema.setWindowName(textInput.text);
    40. Keppo.test();
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    Log:
    Qt Code:
    1. istnieje
    2. "Text Input"
    3. keppo test "old"
    4. "Text Input"
    5. keppo test "old"
    6. "Text Ixnput"
    7. keppo test "old"
    8. "Text Ixdnput"
    9. keppo test "old"
    10. "Text Ixdnput"
    11. keppo test "old"
    12. "Text Ixdnput"
    13. keppo test "old"
    14. "Text Ixdnput"
    15. keppo test "old"
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QSettings dont save updated variables by QML

    void keppoxd::setWindowName(QString value)
    {
    connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    name = value;
    qDebug() << name;
    }

    void siema::setWindowName(QString value)
    {
    connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    keppo.name = value;
    qDebug() << keppo.name;
    }
    I don't think you understand how signals and slots work. You should be calling connect() in the constructor for these classes, not in the slot. I see that the methods are being called from QML, but the connect() calls are in the wrong place. Put each of these connect() calls into the constructors for their classes.

    The next problem is that I do not see where you are emitting the "textChanged()" signals in either of these classes. Even if you have signals and slots connected, if you never emit the textChanged() signal, the slot never gets executed.

    The third problem with your current code is that if you put the connect() statements in the slots, each time the slot is executed there is another connection made, so if the textChanged() signal ever does get emitted you will get repeated execution of the slot (and a another connection) each time the slot is called.

    And the final problem: you have two instances of the class "keppoxd", both of them named "keppo". One of them is a member variable of the class "siema", the other one is an automatic variable in your "main" function. You have told QML to use the one in the main() function as the context property "Keppo". So when you call siema.setWindowName() from QML it is setting the value contained in siema's keppo variable, but the QML call to keppo.test() is displaying the value stored in the keppo instance in main(), which never changes from the default value "old".
    <=== 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.

  7. #7
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSettings dont save updated variables by QML

    So to fix my problem I have to rename "siema" and "keppo" declarations in main?

    I now have sth like this, but still not working.

    Qt Code:
    1. #ifndef KEPPOXD_H
    2. #define KEPPOXD_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <siema.h>
    7.  
    8. class keppoxd : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit keppoxd(QObject *parent = nullptr);
    13. siema siema;
    14.  
    15. signals:
    16. void textChanged(QString);
    17. public slots:
    18. void setWindowName(QString value);
    19.  
    20. };
    21.  
    22. #endif // KEPPOXD_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef SIEMA_H
    2. #define SIEMA_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6.  
    7.  
    8. class siema : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit siema(QObject *parent = nullptr);
    13. QString name = "old";
    14.  
    15. signals:
    16.  
    17. public slots:
    18. void setWindowName(QString value);
    19. void check();
    20.  
    21. };
    22.  
    23. #endif // SIEMA_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "keppoxd.h"
    2.  
    3. keppoxd::keppoxd(QObject *parent) : QObject(parent)
    4. {
    5. connect(this, SIGNAL (textChanged(QString)), &siema, SLOT(setWindowName(QString)));
    6. }
    7.  
    8. void keppoxd::setWindowName(QString value)
    9. {
    10. siema.name = value;
    11. qDebug() << "siema.name value from keppo class " << siema.name;
    12. emit textChanged(value);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "siema.h"
    2.  
    3. siema::siema(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. void siema::setWindowName(QString value)
    9. {
    10. name = value;
    11. }
    12.  
    13. void siema::check()
    14. {
    15. qDebug() << "siema value " << name;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QSettings>
    4. #include <QDir>
    5. #include <QDebug>
    6. #include <keppoxd.h>
    7. #include <siema.h>
    8. #include <QQmlContext>
    9. #include <QQmlApplicationEngine>
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13.  
    14. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    15.  
    16. QGuiApplication app(argc, argv);
    17. //QSettings* settings;
    18.  
    19. /*if(QFileInfo::exists(QDir::currentPath() + "/my_config_file.ini")){
    20.   qDebug() << "istnieje";
    21.   }
    22.   else{
    23.   qDebug() << "nie istnieje";
    24.  
    25.   settings = new QSettings(QDir::currentPath() + "/my_config_file.ini", QSettings::IniFormat);
    26.   settings->setValue("test", "value");
    27.   settings->setValue("Button", 1);
    28.   settings->sync();
    29.   } */
    30.  
    31.  
    32. siema siemaqml;
    33. keppoxd keppoqml;
    34.  
    35. QQmlApplicationEngine engine;
    36. const QUrl url(QStringLiteral("qrc:/main.qml"));
    37. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    38. &app, [url](QObject *obj, const QUrl &objUrl) {
    39. if (!obj && url == objUrl)
    40. QCoreApplication::exit(-1);
    41. }, Qt::QueuedConnection);
    42. engine.load(url);
    43.  
    44.  
    45. QQmlContext *ctx = engine.rootContext();
    46.  
    47. ctx->setContextProperty("Siema", &siemaqml);
    48. ctx->setContextProperty("Keppo", &keppoqml);
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. import QtQuick 2.9
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. visible: true
    6. width: 640
    7. height: 480
    8. title: qsTr("Hello World")
    9.  
    10. TextInput {
    11. id: textInput
    12. x: 48
    13. y: 58
    14. width: 80
    15. height: 20
    16. text: qsTr("Text Input")
    17. font.pixelSize: 12
    18. onTextEdited:{
    19. Keppo.setWindowName(textInput.text);
    20. Siema.check();
    21. }
    22. }
    23.  
    24. Rectangle {
    25. id: rectangle
    26. x: 276
    27. y: 41
    28. width: 200
    29. height: 200
    30. color: "#e30a0a"
    31.  
    32. MouseArea {
    33. id: mouseArea
    34. x: 0
    35. y: 0
    36. width: 200
    37. height: 200
    38. onClicked:{
    39. Keppo.setWindowName(textInput.text);
    40. Siema.check();
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Bedopies; 12th May 2019 at 20:06.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSettings dont save updated variables by QML

    Quote Originally Posted by Bedopies View Post
    I called Clickcheck() from QML with
    Qt Code:
    1. Settings.clickcheck();
    To copy to clipboard, switch view to plain text mode 
    and when I pressed mouse area this show me old variable.
    Without knowing how you exposed the C++ object to QML my guess is that this is a different instance of the Settings class than the one inside Building.

    So you are very likely doing this
    - setting the value on Building.sett
    - checking the value on Setting

    Obviously the latter object won't have the value of the former.

    Quote Originally Posted by Bedopies View Post
    Qt Code:
    1. onTextEdited:{
    2. Siema.setWindowName(textInput.text);
    3. Keppo.test();
    4. }
    To copy to clipboard, switch view to plain text mode 
    That makes no sense.

    You are calling Siema.setWindowName(), which changes a value in the Siema object.
    But then you are calling the test() method on the Keppo object?

    This should probably be
    Qt Code:
    1. onTextEdited:{
    2. Siema.setWindowName(textInput.text);
    3. Siema.test();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #9
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSettings dont save updated variables by QML

    Fixed by:

    Qt Code:
    1. class keppoxd : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit keppoxd(QObject *parent = nullptr);
    6. siema *siema;
    7. ...
    8.  
    9. int main(){
    10. siema siema;
    11. keppoxd keppo;
    12.  
    13. keppo.siema = &siema;
    14. ...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Where to save QSettings?
    By arcull in forum Newbie
    Replies: 5
    Last Post: 23rd July 2015, 18:43
  2. Replies: 4
    Last Post: 5th October 2010, 23:47
  3. Save/Load variables to FIle
    By Jordan in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2010, 12:35
  4. QSettings does not save...
    By mtrpoland in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 13:15

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.