Hi everyone,
i've been reading the forums for some time but now i need some help 
I tried to boil it down to the most simples case to show it here:
I have a qml file simply taken from one of the official tutorials.
It's a simple button. lets call it button2.
I have a cpp code, that generates a window and a button, button1.
Once the button1 is pressed, QDeclarativeView is used to view the qml file and a window pops up showing button1.
Pressing button2 (the qml button) should connect to a cpp slot that will show a message on terminal.
Everything works fine, but when i click on button2 i get a segfault.
Here is the code.
I was not sure if i'm supposed to post the entire code (with all the indcludes and stuff).
I hope its ok to post the code in such a way.
//Button.qml
import QtQuick 1.0
Rectangle
{
id: button
width: 75; height: 50
Text
{
id: buttonLabel
anchors.centerIn: parent
text: "click here"
}
MouseArea
{
id: buttonMouseArea
anchors.fill: parent
onClicked: buttonClick()
}
signal buttonClick()
onButtonClick:
{
qmlSlot.cppMethod("Hello from QML") //this is the cpp slot i whant to call
}
}
//Button.qml
import QtQuick 1.0
Rectangle
{
id: button
width: 75; height: 50
Text
{
id: buttonLabel
anchors.centerIn: parent
text: "click here"
}
MouseArea
{
id: buttonMouseArea
anchors.fill: parent
onClicked: buttonClick()
}
signal buttonClick()
onButtonClick:
{
qmlSlot.cppMethod("Hello from QML") //this is the cpp slot i whant to call
}
}
To copy to clipboard, switch view to plain text mode
//myqml.h
{
Q_OBJECT
public:
private slots:
void showQmlSlot(); //this is the slot to view the qml file
Q_INVOKABLE
void cppMethod
(const QString &msg
) { qDebug() << "Called the C++ method with" << msg;
}//this is the cpp slot to be called within the qml file
private:
};
//myqml.h
class MyClass : public QWidget
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
private slots:
void showQmlSlot(); //this is the slot to view the qml file
Q_INVOKABLE void cppMethod(const QString &msg) {
qDebug() << "Called the C++ method with" << msg;
}//this is the cpp slot to be called within the qml file
private:
QPushButton *click;
};
To copy to clipboard, switch view to plain text mode
//myqml.cpp
{
click->setGeometry(50, 40, 75, 30);
connect(click, SIGNAL(clicked()),
this, SLOT(showQmlSlot()));
}
void MyClass::showQmlSlot()
{
MyClass myClass;
QDeclarativeView *view = new QDeclarativeView();
view
->setSource
(QUrl::fromLocalFile("Button.qml"));
view->rootContext()->setContextProperty("qmlSlot", &myClass);
view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
view->show();
}
//myqml.cpp
MyClass::MyClass(QWidget *parent)
: QWidget(parent)
{
click = new QPushButton("Click me", this);
click->setGeometry(50, 40, 75, 30);
connect(click, SIGNAL(clicked()),
this, SLOT(showQmlSlot()));
}
void MyClass::showQmlSlot()
{
MyClass myClass;
QDeclarativeView *view = new QDeclarativeView();
view->setSource(QUrl::fromLocalFile("Button.qml"));
view->rootContext()->setContextProperty("qmlSlot", &myClass);
view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
view->show();
}
To copy to clipboard, switch view to plain text mode
I just dont get where the error comes from.
Funny enought, if i use the QDeclarativeView within the main.cpp,
like shown for instance here
http://www.developer.nokia.com/Commu...ation_from_QML
it works fine...
Thanks for helping me
Bookmarks