Trouble with calling a cpp method from qml
-
Hi every body,
Actually I'm having trouble with merging both qml and c++ .My little application contains two pages at the moment and my problem is:
In the first page (main.qml) whan I created an Item class that contains all the widgets of my window nothing is displayed when I run the program.
I will be grateful if someone helps me to solve this problem.
main.cpp
@#include "enter.h"
#include <QUrl>
#include <QQuickView>
#include <QApplication>
#include <QQmlComponent>
#include <QQmlEngine>
#include <QObject>
#include <QQuickWindow>int main(int argc, char *argv[])
{
QApplication app(argc, argv);qmlRegisterType<enter>("appEntrance", 1, 0, "appEntrance"); qmlRegisterType<HomePage>("home",1,0,"home"); QQuickView view; view.setSource(QUrl::fromLocalFile("qrc:///main.qml")); view.show(); return app.exec();
}@
enter.h
@#ifndef ENTER_H
#define ENTER_H#include <QWidget>
#include "home.h"
class enter :public QWidget
{
Q_OBJECTpublic:
Q_INVOKABLE void openHomePage();};
#endif // ENTER_H
@enter.cpp
@#include "home.h"
#include "enter.h"void enter::openHomePage()
{
HomePage w1;w1.show();
}
@home.h
@#ifndef HOME_H
#define HOME_H#include <QWidget>
class HomePage : public QWidget
{
//I havent defined the any method yet
};#endif // HOME_H
@main.qml
@
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Layouts 1.1Item {
id:fenetrePrincipale
enter{
id:appEntrance
Rectangle {
color: "#fbf79a"
ColumnLayout{
id:conteneur
anchors.fill:parent
ColumnLayout {
Layout.alignment:Qt.AlignCenter
id: columnLayout1
spacing: 60
Image {
id: image1
width: 6
height: 0
Layout.alignment: Qt.AlignCenter
source: "windows8-1-food.jpg"
}Button { Layout.alignment: Qt.AlignCenter width: 200 height: 100 text: "Entrer" style: ButtonStyle { background: Rectangle { implicitWidth: 100 implicitHeight: 25 border.width: control.activeFocus ? 2 : 1 border.color: "#888" radius: 14 gradient: Gradient { GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#f4d76f" } GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ddb936" } } } } clicked: appEntrance.openHomePage() } } }
}
}
}@
home.qml
@import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Layouts 1.1Item
{
HomePage{
id:home
ColumnLayout {
spacing:60
anchors.fill:parent
Button {
id: acceuil
Layout.alignment: Qt.AlignVCenter
width: 167
height: 28
text: qsTr("button1")
activeFocusOnPress: false
}Button { id: candidats Layout.alignment: Qt.AlignVCenter width: 167 height: 28 text: qsTr("button2") } Button { id: interimaires Layout.alignment: Qt.AlignVCenter width: 167 height: 28 text: qsTr("button3") } Button { id: entreprises Layout.alignment: Qt.AlignVCenter width: 167 height: 28 text: qsTr("button4") } Button { id: aPropos Layout.alignment: Qt.AlignVCenter width: 167 height: 28 text: qsTr("button 5") } } }
}
@
-
Multiple fundamental issue here
- Enter type is not defined.
- Why do you want to inherit from QWidget ? You can'd to this to QML 2.0 implementation.
- Your registration is ok. But the way you are using type in QML file is wrong. You are registering as appEntrance. You need to use this in QML.
Since there multiple fundamental issue here, I suggest you look at Integrating C++ and QML. Also check how to extend the Qml with C++. Try with simple example.
-
Thanks Dreehendra ,
but can you explain to me what are my mistakes exactly because I read integrating c++ with qml and how to extend the qml with c++ .The code that I've written is what I've understood from the tutorial (there are some points which are still ambiguous .
- I defined the enter type in "enter.h" is the way in which I defined it wrong?
- I didn't know that i can't inherit my class from QWidget. Should I use QObject instead?
- about the registration actually even in the tutorial I didn't understand how it is done exactly .
What I've understood that appEntrance is the instantiation of the class enter that I've created(you can refer to "main.qml" line number 10)
Please let me know if I was mistaken and it will be great if you explain it better to me .