Variable in other class is not updating.

May 11, 2019 at 5:44pm
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.

1
2
3
4
5
6
void Building::setWindowName(QString value){
    connect(this, SIGNAL (textChanged(QString)), this, SLOT(setWindowName(QString)));
    sett.windowName = value;
    sett.windowID = (const wchar_t*) sett.windowName.utf16();
    qDebug() << "windowName " << sett.windowName; //Shows updated
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Settings : public QObject
{
    Q_OBJECT

public:
    explicit Settings(QObject *parent = nullptr);
    QSettings* sett;

    int toggleButton=0x52;
    int btoggleButton=0x52;// r
    int inventoryKey=0x45; // e
    int minCpsDelay = 10;
    int maxCpsDelay = 15;

    QString windowName = "Minecraft 1.8.8 (Blazingpack.pl)";
    LPCWSTR windowID = (const wchar_t*) windowName.utf16();


1
2
3
4
void Settings::clickcheck()
{
    qDebug() << windowName; //Showed old - not updated variable.
}



I just want to save some data from different classes
Last edited on May 11, 2019 at 11:26pm
May 12, 2019 at 12:12am
not enough code to reproduce
May 12, 2019 at 1:53am
I dont know how to describe this :/

I want to change windowName string declared in Settings class from Building class level.

When I edit and read from Building class level, it shows good, edited variable.

When I read EDITED variable from Settings class, It shows always the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef KEPPOXD_H
#define KEPPOXD_H

#include <QObject>
#include <QDebug>

class keppoxd : public QObject
{
    Q_OBJECT
public:
    explicit keppoxd(QObject *parent = nullptr);
    QString name = "old";

signals:
    void textChanged(QString);

public slots:
    void setWindowName(QString value);
    void test();
};

#endif // KEPPOXD_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef SIEMA_H
#define SIEMA_H

#include <QObject>
#include <QDebug>
#include <keppoxd.h>


class siema : public QObject
{
    Q_OBJECT
public:
    explicit siema(QObject *parent = nullptr);
    keppoxd keppo;

signals:
    void textChanged(QString);

public slots:
    void setWindowName(QString value);

};

#endif // SIEMA_H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "keppoxd.h"

keppoxd::keppoxd(QObject *parent) : QObject(parent)
{

}

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

void keppoxd::test()
{
    qDebug() << "keppo test " << name;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSettings>
#include <QDir>
#include <QDebug>
#include <keppoxd.h>
#include <siema.h>
#include <QQmlContext>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    //QSettings* settings;

    /*if(QFileInfo::exists(QDir::currentPath() + "/my_config_file.ini")){
        qDebug() << "istnieje";
    }
    else{
        qDebug() << "nie istnieje";

        settings = new QSettings(QDir::currentPath() + "/my_config_file.ini", QSettings::IniFormat);
        settings->setValue("test", "value");
        settings->setValue("Button", 1);
        settings->sync();
    } */


    siema siema;
    keppoxd keppo;

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);


    QQmlContext *ctx = engine.rootContext();

    ctx->setContextProperty("Siema", &siema);
    ctx->setContextProperty("Keppo", &keppo);

    return app.exec();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "siema.h"

siema::siema(QObject *parent) : QObject(parent)
{

}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TextInput {
        id: textInput
        x: 48
        y: 58
        width: 80
        height: 20
        text: qsTr("Text Input")
        font.pixelSize: 12
        onTextEdited:{
            Siema.setWindowName(textInput.text);
            Keppo.test();
        }
    }

    Rectangle {
        id: rectangle
        x: 276
        y: 41
        width: 200
        height: 200
        color: "#e30a0a"

        MouseArea {
            id: mouseArea
            x: 0
            y: 0
            width: 200
            height: 200
            onClicked:{
                Siema.setWindowName(textInput.text);
                Keppo.test();
            }
        }
    }
}


Log:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
istnieje
"Text Input"
keppo test  "old"
"Text Input"
keppo test  "old"
"Text Ixnput"
keppo test  "old"
"Text Ixdnput"
keppo test  "old"
"Text Ixdnput"
keppo test  "old"
"Text Ixdnput"
keppo test  "old"
"Text Ixdnput"
keppo test  "old"
Last edited on May 12, 2019 at 2:44am
May 12, 2019 at 3:45am
You make a siema and a keppo in main, but since a siema has a keppo inside it you are actually making two keppos. Presumably you are changing the name of one of them but printing the name of the other.
May 12, 2019 at 11:30am
Then how to change/edit/modify other class variable from other class?
Last edited on May 12, 2019 at 1:15pm
May 12, 2019 at 7:39pm
I don't understand what you are asking.
May 12, 2019 at 7:57pm
I want to change classa.string from classb but I cant and I dont know why.
May 12, 2019 at 8:17pm
Note that in main keppo and siema.keppo are two different objects.
Last edited on May 12, 2019 at 8:18pm
May 12, 2019 at 8:28pm
So if I changed that should work? Or what should I do?
Last edited on May 12, 2019 at 8:33pm
May 12, 2019 at 11:41pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class siema : public QObject
{
    Q_OBJECT
public:
    explicit siema(QObject *parent = nullptr);
    keppoxd *keppo; //a pointer
//...
];

int main(){
    siema siema_;
    keppoxd keppo;
    siema_.keppo = &keppo; //notnow the `siema_' object can communicate to the `keppo' object in main
}
Last edited on May 13, 2019 at 1:13pm
May 13, 2019 at 10:12am
Oh thanks, working!
Topic archived. No new replies allowed.