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



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


  • Lifetime Qt Champion

    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



  • 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]


  • Lifetime Qt Champion

    Isn't the name of your class TerminalLauncher ?



  • 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]


  • Lifetime Qt Champion

    AFACS TerminalLauncher is not an instance



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

    @
    Launcher {
    id: launcher
    }
    @


  • Lifetime Qt Champion

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


Log in to reply
 

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