Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to access C++ class methods in QML
Forum Update on Monday, May 27th 2025

How to access C++ class methods in QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 494 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sushma_MP
    wrote on last edited by A Former User
    #1

    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.0

    Window {
    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();
    

    }

    1 Reply Last reply
    0
    • LorenzL Offline
      LorenzL Offline
      Lorenz
      wrote on last edited by
      #2

      hi,
      what is the exact error message when the crash occurs?

      Developer @ V-Play Engine - http://v-play.net/qt

      V-Play simplifies

      • Game Development with Qt
      • Mobile App Dev with Qt esp. iOS & Android

      What others say
      V-Play scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved