Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to set the properties of child from main(Qml/C++)

How to set the properties of child from main(Qml/C++)

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 636 Views
  • 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.
  • K Offline
    K Offline
    ktrsathish
    wrote on last edited by
    #1

    I am new to Qt. I am trying to enable and disable the image from main window with 2 seconds delay but always it is always taking the last property which i implemented. Please find my sample code below. It could be great help if you could provide the solution asap.

    main.cpp

    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    QObject *object = engine.rootObjects().first();
    
    QObject *test= object->findChild<QObject*>("image");
    
    if(test)
    {
        QThread::msleep(2000);
    
        QQmlProperty::write(test, "visible", "false");
       QThread::msleep(2000);
    
        QQmlProperty::write(test, "visible", "true");
        QThread::msleep(2000);
    
        QQmlProperty::write(test, "visible", "false");
        QThread::msleep(2000);
    
        QQmlProperty::write(test, "visible", "true");
    

    }
    return app.exec();
    }

    main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2

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

    TestForm{}
    }
    TestForm.ui.qml

    import QtQuick 2.4

    Item {
    width: 400
    height: 400

    Image {
        id: image
        x: 67
        y: 98
        width: 100
        height: 100
        source: "test.png"
        objectName: "test"
    }
    

    }

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Avoid placing the sleep. Since you are placing sleep, your screen is getting chance to refresh itself. If you want to check like this, use QTimer & try updating the property every 2 seconds & then check.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      K 1 Reply Last reply
      0
      • dheerendraD dheerendra

        Avoid placing the sleep. Since you are placing sleep, your screen is getting chance to refresh itself. If you want to check like this, use QTimer & try updating the property every 2 seconds & then check.

        K Offline
        K Offline
        ktrsathish
        wrote on last edited by ktrsathish
        #3

        @dheerendra Thanks for your input. I have tested with timer long back and it is working fine. My actual requirement is Qt will run in one thread and input for Qt will run in another thread and it will cyclicly enable or disable the image based on the user input. When i call cyclic from another thread with user input to display the image and it is not enabled based on the input. this is just an example why it is not updating the data when i call continuously. When we toggle through timer it is working if I call with sequenctially with delay it is not working. May i know the reason?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Are you using signals/slots across thread for communication ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          K 1 Reply Last reply
          0
          • dheerendraD dheerendra

            Are you using signals/slots across thread for communication ?

            K Offline
            K Offline
            ktrsathish
            wrote on last edited by
            #5

            @dheerendra Yes, I am using signals and slots. post the signals from one thread and implemented the slots in Qt thread. having the same problem. Why it is not working when we give QQmlProperty::write(test, "visible", "false"); and QQmlProperty::write(test, "visible", "true"); continuously. why it is taking always the last option.

            1 Reply Last reply
            0

            • Login

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