Results 1 to 9 of 9

Thread: QSettings dont save updated variables by QML

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 19:06.

Similar Threads

  1. Where to save QSettings?
    By arcull in forum Newbie
    Replies: 5
    Last Post: 23rd July 2015, 17:43
  2. Replies: 4
    Last Post: 5th October 2010, 22:47
  3. Save/Load variables to FIle
    By Jordan in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2010, 11:35
  4. QSettings does not save...
    By mtrpoland in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 12: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.