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. [SOLVED] Call cpp method from qml in qt5.3
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Call cpp method from qml in qt5.3

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi, In qt 5.3, i have a cpp class (MainWindow) and i added a puch button on the ui. I want to click on the button, and open a window qml type "Window QML Type":http://qt-project.org/doc/qt-5/qml-qtquick-window-window.html
    I successfully can do that but i want to call a cpp function from qml window directly, how can i do that?
    I know that i can add a button in qml file and call cpp function by "onClicked:", i know it, but i want to call a cpp method directly after loading qml file, without clicking on any button. please help me.
    I want something like that:

    in qml file (mail.qml)
    window{
    id :qmlMainWindow
    color:white

    MainWindow {
    
        id : myCPPObject
    
    }
    
    //something like that
    myCPPObject.cppFunction()
    

    }

    //in mainwindow.h
    Q_INVOKABLE void cppFunction();

    //in main.cpp
    qmlRegisterType<MainWindow>("com.myself", 1, 0, "MainWindow");

    Thanks in advance...

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      If I understood you correctly you could call function in "Component.onCompleted":http://qt-project.org/doc/qt-5/qml-qtqml-component.html#completed-signal handler.

      157

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

        Yes, that works.
        Thank you very much

        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