Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange problem with a method in Qt
Forum Updated to NodeBB v4.3 + New Features

Strange problem with a method in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adrian.Aioanei
    wrote on last edited by Adrian.Aioanei
    #1

    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 :)

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      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.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Adrian.Aioanei
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          Maybe something like this:

          class AppData
          {
          public:
              void insertDataToLabels()
              {
                  m_model.addAnimal(Animal("1", "2"));
                  // ...
              }
          private:
              AnimalModel m_model;
          };
          
          1 Reply Last reply
          3
          • A Offline
            A Offline
            Adrian.Aioanei
            wrote on last edited by
            #5

            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
            
            
            1 Reply Last reply
            0
            • A Offline
              A Offline
              Adrian.Aioanei
              wrote on last edited by Adrian.Aioanei
              #6

              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.`
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Why are you creating another application in insertDataToLabels ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved