Results 1 to 6 of 6

Thread: Qml with C++ extensions

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Qml with C++ extensions

    Hi i am new to QML and trying to run a small QML program using c++ extension in Qt-5.4.0

    here is my code app.qml

    Qt Code:
    1. import QtQuick 2.1
    2. import QtQuick.Controls 1.0
    3.  
    4. Button {
    5. id: generate
    6. text: qsTr("&Generate")
    7. onClicked: generate.btn_clicked()
    8. }
    To copy to clipboard, switch view to plain text mode 

    button.h
    Qt Code:
    1. #ifndef BUTTON_H
    2. #define BUTTON_H
    3.  
    4. #include <QDeclarativeItem>
    5. #include <QObject>
    6. #include <QDebug>
    7. #include <QWidget>
    8.  
    9. class BButton : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. BButton();
    15.  
    16. Q_INVOKABLE void btn_clicked();
    17.  
    18. };
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    button .cpp
    Qt Code:
    1. #include "button.h"
    2.  
    3. BButton::BButton()
    4. {
    5. //nothing to write
    6.  
    7. }
    8. void BButton::btn_clicked()
    9. {
    10. qDebug("THE GUI was clicked");
    11. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "button.h"
    2. #include <qdeclarative.h>
    3. #include <QDeclarativeView>
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. //qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
    11.  
    12. QDeclarativeView view;
    13. view.setSource(QUrl::fromLocalFile("app.qml"));
    14. view.show();
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    While executing using qmlscene the GUI was creating fine but on clicking the button it is showing the following error
    file:///root/Btn_Sig/app.qml:7: TypeError: Property 'btn_clicked' of object Button_QMLTYPE_35(0x161c950) is not a function
    Please tell me is there anything more to add in my code to generate the output or did i miss something.

    Thanks in advance,
    Rohith.G

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml with C++ extensions

    There's a couple of things:

    - Your QML code import QtQuick.Controls, which has a Button type, which is what your QML is instantiating. QtQuick.Controls.Button doesn't have the method you are trying to call
    - QDeclarativeView is for QtQuick 1, you need QQuickView for QtQuick 2
    - Your button class is a QtWidget, you can't incorporate a widget in a QtQuick 2 scene
    - Your button class is not registered with the QML type system

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qml with C++ extensions

    HI anda_skoa,

    Thanks for replying to execute my program successfully which class should i use instead of QWidget,
    Please tell me and if possible can you please post the code that helps to execute my program.

    Thanks in advance,
    Rohith.G

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml with C++ extensions

    Quote Originally Posted by gunturrohith View Post
    Thanks for replying to execute my program successfully which class should i use instead of QWidget,
    Depends on what you want to do.
    QObject could be fine if you only need some methods exported.

    Quote Originally Posted by gunturrohith View Post
    Please tell me and if possible can you please post the code that helps to execute my program.
    I haven't tried executing your program, these findings are purely from looking at the code you've posted.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qml with C++ extensions

    Hi anda_skoa,

    I just need a program to create a button in qml and when i click on the button it should print the qDebug that was present in the slot of C++.

    I am trying to learn how to connect Qml and C++ by using signal and slots that is what my intention.

    Thanks in advance,
    Rohith.G

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qml with C++ extensions

    There are a couple of options to do something like that.

    The easiest way is to create an instance of a QObject derived class in C++ and use QQmlContext::setContextProperty() to make that instance accessible from QML.
    The context itself can be retrieved from the QQmlEngine of the QQuickView.

    Cheers,
    _

Similar Threads

  1. it there UI extensions for maemo?
    By hashb in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th September 2010, 04:01
  2. Open GL extensions
    By Thomas Feldman in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2009, 16:29
  3. QDialog Extensions & resizing
    By Bakuriu in forum Newbie
    Replies: 2
    Last Post: 26th October 2009, 06:14
  4. PHP extensions written in C++/Qt
    By seim in forum Qt Programming
    Replies: 0
    Last Post: 12th February 2009, 00:40
  5. Container Extensions
    By mariok in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 11:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.