Strange problem with a method in Qt



  • Hey all :D

    I have a strange problem. I have created a QML interface (the interface is the default interface with tow buttons in footer) and in QML i have created same Labels and i populate them with same string from C++.
    Here is what i am doing in main function.

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        AnimalModel model;
        model.addAnimal(Animal("1", "2"));
        model.addAnimal(Animal("1", ""));
        model.addAnimal(Animal("1", "2"));
        model.addAnimal(Animal("1", "2"));
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", &model);
        view.setSource(QUrl("qrc:/main.qml"));
    

    When i do this in main evreathing works ok, but if i make an other class and i make a method like this:

     void AddData::insertDataToLabels()
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        AnimalModel model;
        model.addAnimal(Animal("1", "2"));
        model.addAnimal(Animal("1", ""));
        model.addAnimal(Animal("1", "2"));
        model.addAnimal(Animal("1", "2"));
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", &model);
        view.setSource(QUrl("qrc:/main.qml"));
    
    

    And after that i call that method in main like this:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        AppData data;
        data.insertDataToLabels();
        return app.exec();
    }
    
    

    Works but the default buttons from the footer doesn't work anymore.
    If i use the objects directly in main like above works ok, if i make a class with method and put the code from main buttons doesn't work anymore.
    Is there a reason why my the buttons doesn't work if i call that method? The labels are also populated and i don't have any error or wornings.
    thank you :)


  • Moderators

    Hi!

     void AddData::insertDataToLabels()
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        AnimalModel model;
    

    Here, model is local to insertDataToLabels(). Once this functions returns, model becomes invalid. As your context property points to an invalid object after this, the behavior of your program becomes undefined.



  • Ohh yes..how stupid I am.
    The problem is with insert data. I dont't have any other choice. I need a class that write have a method that write data.
    Do you have any idea about how can i solve this @Wieland ?
    Thank you for your replay.


  • Moderators

    Maybe something like this:

    class AppData
    {
    public:
        void insertDataToLabels()
        {
            m_model.addAnimal(Animal("1", "2"));
            // ...
        }
    private:
        AnimalModel m_model;
    };
    


  • I have try but also i have the model header included in my AppData i receive this erors:

    D:\Qt_prog\textQtInterface\model.h:6: error: C2011: 'Animal': 'class' type redefinition
    D:\Qt_prog\textQtInterface\model.h:20: error: C2011: 'AnimalModel': 'class' type redefinition
    D:\Qt_prog\textQtInterface\adddata.h:11: error: C2079: 'AddData::m_model' uses undefined class 'AnimalModel'
    

    The AppData looks like this:

    #ifndef ADDDATA_H
    #define ADDDATA_H
    
    #include"model.h"
    
    class AddData
    {
    public:
         void insertDataToLabels();
    private:
         AnimalModel m_model;
    };
    
    #endif // ADDDATA_H
    
    


  • I have solved the above problem (i have double included a header).
    Also with this things set the interface also in stock.

    So the problem is the same.
    If i do something like this in main:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        qmlRegisterType<TerminalLauncher>("com.example.launcher", 1, 0, "Launcher");
        AppData data;
       data.insertDataToLabels();
        return app.exec();
    }
    ``
    The buttons freeze, but if i put all the code for "model" object in main works ok.`

  • Lifetime Qt Champion

    Hi,

    Why are you creating another application in insertDataToLabels ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.