How to access C++ class methods in QML
-
Hi ,
I am taking Qt project in that one cpp class and trying to open file as below code and it is working fine
sampleopen.cpp
``
void sampleopen::readfile()
{const QString filepath = QFileDialog::getOpenFileName(this, "Select a file to open...", QDir::homePath()); if (!filepath.isEmpty()) { qDebug()<<"In if loop"<<endl; }
}
but the problem is the same piece of code i am trying to access from QML project by registering cpp class it is getting crashed, seems like it is unable to take that path.if i set path it is working fine but i don't want to set particular path to open i need to browse folder to open file which i need. i dono what is the mistake in this can anyone help me to get solution for this... below code is cpp class wch is linked with QML IntermediateTry.cpp
void IntermediateTry::test()
{
qDebug()<<"inside test..."<<endl;const QString filepath = QFileDialog::getOpenFileName(this, "Select a file to open...", QDir::homePath()); if (!filepath.isEmpty()) { qDebug()<<"In if loop"<<endl; }
}
main.qml
import QtQuick 2.7
import QtQuick.Window 2.2
import com.intermediate 1.0Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")IntermediateTry{ id: inter } MouseArea { id: mouse anchors.fill: parent onClicked: { inter.test() } }
}
main.cpp
#include "intermediatetry.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<IntermediateTry>("com.intermediate",1,0,"IntermediateTry");QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec();
}
-
hi,
what is the exact error message when the crash occurs?