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] Having trouble linking QML button to C++ class
QtWS25 Last Chance

[solved] Having trouble linking QML button to C++ class

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.4k 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.
  • U Offline
    U Offline
    udev01
    wrote on last edited by
    #1

    First time using Ubuntu SDK and QML. I've spent the entire day attempting to figure this out but every solution that i came accross is seems to be for an older version. I'm trying to figure out how i can link my buttons in QML to functions in C++.

    Tried posting my code in this thread but apparently that's "SPAM". I've uploaded it to pastebin.
    http://pastebin.com/WgJ3y88Y

    qmlRegisterType doesn't work for me:

    @main.cpp:17: error: 'qmlRegisterType' was not declared in this scope qmlRegisterType("com.example.launcher", 1, 0, "Launcher"); ^ @

    Also, I'm using latest version of QT5. And when i include <QtQml> The new error is

    @main.cpp:18: error: no matching function for call to 'qmlRegisterType(const char [21], int, int, const char [9])' qmlRegisterType("com.example.launcher", 1, 0, "Launcher"); ^ @

    Any help with this would be greatly appreciated! Thanks :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      qmlRegisterType is a template function. You need something like

      @qmlRegisterType<Launcher>("com.example.launcher", 1, 0, "Launcher")@

      Where the the name between <> is the name of your C++ class

      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
      0
      • U Offline
        U Offline
        udev01
        wrote on last edited by
        #3

        Thank you for your quick reply and welcome! :) I have replaced with
        @qmlRegisterType<TerminalLauncher>("com.example.launcher", 1, 0, "Launcher");@

        Now the application starts (progress!) but when i click the button, there is an error in the application output:
        @
        qrc:///main.qml:120: ReferenceError: terminal_launcher is not defined
        @

        terminal_launcher is the name of my class.

        [quote author="SGaist" date="1421014029"]Hi and welcome to devnet,

        qmlRegisterType is a template function. You need something like

        @qmlRegisterType<Launcher>("com.example.launcher", 1, 0, "Launcher")@

        Where the the name between <> is the name of your C++ class[/quote]

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Isn't the name of your class TerminalLauncher ?

          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
          0
          • U Offline
            U Offline
            udev01
            wrote on last edited by
            #5

            Yes, it's TerminalLauncher. In main.qml i have:

            @ Button {
            x: 25
            y: 8
            width: 150
            height: 35
            text: "test"
            // onClicked: terminal_launcher.open_terminal();
            onClicked: TerminalLauncher.open_terminal();
            }
            @

            but it gives me

            @qrc:///main.qml:121: ReferenceError: TerminalLauncher is not defined@

            [quote author="SGaist" date="1421015125"]Isn't the name of your class TerminalLauncher ?[/quote]

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              AFACS TerminalLauncher is not an instance

              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
              0
              • U Offline
                U Offline
                udev01
                wrote on last edited by
                #7

                Fixed. I had to define the launcher in main.qml.

                @
                Launcher {
                id: launcher
                }
                @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Good !

                  Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                  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
                  0

                  • Login

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