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. Call base object method from QML
Forum Updated to NodeBB v4.3 + New Features

Call base object method from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.3k Views 3 Watching
  • 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
    St.Stanislav
    wrote on 13 Nov 2020, 09:17 last edited by
    #1

    Hello all! I have the following situation: I use FileDialog from Qt.labs.platform 1.1 and pass FileDialog.file to C++ QObject. Before I have used one object with methods and everything works well (QML calls method with QUrl argument which, in turn, calls QString case)

    Q_INVOKABLE void saveToFile(QUrl url) {saveToFile(url.toLocalFile())};
    Q_INVOKABLE void saveToFile(QString string){//some processing};
    

    Now I have base class with common method and virtual abstract one

    Q_INVOKABLE void saveToFile(QUrl url) {saveToFile(url.toLocalFile())};
    Q_INVOKABLE virtual void saveToFile(QString string) = 0;
    

    So in the derived class I have different definitions of the method void saveToFile(QString string). Following this idea, I try to call saveToFile from QML for the derived objects. But the override method is calling with converting (QML url to Qstring directly). How can I avoid this situation and call base method from QML? Of course I can give different names, but I'm interested in common case and, maybe, good practices.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 13 Nov 2020, 09:46 last edited by
      #2

      In order to call the QUrl overload, you need to pass a url variable instead of string. Here is - somewhat hacky - solution:

      FileDialog {
      property url myUrl: file
      }
      // JS
      yourCppObject.saveToFile(myUrl)
      

      Not tested - I hope it will work though :P If not, then different method names is the right solution.

      (Z(:^

      S 1 Reply Last reply 13 Nov 2020, 10:24
      1
      • S sierdzio
        13 Nov 2020, 09:46

        In order to call the QUrl overload, you need to pass a url variable instead of string. Here is - somewhat hacky - solution:

        FileDialog {
        property url myUrl: file
        }
        // JS
        yourCppObject.saveToFile(myUrl)
        

        Not tested - I hope it will work though :P If not, then different method names is the right solution.

        S Offline
        S Offline
        St.Stanislav
        wrote on 13 Nov 2020, 10:24 last edited by St.Stanislav
        #3

        @sierdzio Hello and thank you for the fast reply! Looks like it failed to be workaround :) Moreover, it says in documentation that FileDialog.file is already url type. So looks like there is no difference for passing this argument.

        One more thing that I have tried and it works: redefine the method in the derived class with QUrl argument and call parent method directly:

        void Derived::saveToFile(QUrl filename) {
            Base::saveToFile(filename);
        }
        

        This workaround works and seems to be ok in my case.

        K 1 Reply Last reply 13 Nov 2020, 11:04
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 13 Nov 2020, 10:25 last edited by
          #4

          Interesting! OK, nice that you've found a solution :-)

          (Z(:^

          1 Reply Last reply
          0
          • S St.Stanislav
            13 Nov 2020, 10:24

            @sierdzio Hello and thank you for the fast reply! Looks like it failed to be workaround :) Moreover, it says in documentation that FileDialog.file is already url type. So looks like there is no difference for passing this argument.

            One more thing that I have tried and it works: redefine the method in the derived class with QUrl argument and call parent method directly:

            void Derived::saveToFile(QUrl filename) {
                Base::saveToFile(filename);
            }
            

            This workaround works and seems to be ok in my case.

            K Offline
            K Offline
            KroMignon
            wrote on 13 Nov 2020, 11:04 last edited by KroMignon
            #5

            @St-Stanislav said in Call base object method from QML:

            void Derived::saveToFile(QUrl filename) {
            Parent::saveToFile(filename);
            }

            This is a C++ 'issue', you have to tell C++ compiler you want to keep previous definition:

            class BaseClass : public QObject
            {
                Q_OBJECT
            public:
                Q_INVOKABLE void saveToFile(QString string){//some processing};;
            }
            
            class DerivedClass : public BaseClass
            {
                Q_OBJECT
             
            public:
                using BaseClass::saveToFile;
                Q_INVOKABLE void saveToFile(QUrl url) {saveToFile(url.toLocalFile()); };
            }
            

            ==> http://www.cs.technion.ac.il/users/yechiel/c++-faq/hiding-rule.html

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            S 1 Reply Last reply 13 Nov 2020, 11:43
            3
            • K KroMignon
              13 Nov 2020, 11:04

              @St-Stanislav said in Call base object method from QML:

              void Derived::saveToFile(QUrl filename) {
              Parent::saveToFile(filename);
              }

              This is a C++ 'issue', you have to tell C++ compiler you want to keep previous definition:

              class BaseClass : public QObject
              {
                  Q_OBJECT
              public:
                  Q_INVOKABLE void saveToFile(QString string){//some processing};;
              }
              
              class DerivedClass : public BaseClass
              {
                  Q_OBJECT
               
              public:
                  using BaseClass::saveToFile;
                  Q_INVOKABLE void saveToFile(QUrl url) {saveToFile(url.toLocalFile()); };
              }
              

              ==> http://www.cs.technion.ac.il/users/yechiel/c++-faq/hiding-rule.html

              S Offline
              S Offline
              St.Stanislav
              wrote on 13 Nov 2020, 11:43 last edited by
              #6

              @KroMignon Yes, I have caught it and solved it in the same way, but I wondered if it's any chance to call base class method from QML directly :)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KroMignon
                wrote on 13 Nov 2020, 11:53 last edited by KroMignon
                #7

                @St-Stanislav said in Call base object method from QML:

                if it's any chance to call base class method from QML directly :)

                What do you mean?

                myCppClass.saveToFile("TEST"); // => should call saveToFile(QString) 
                myCppClass.saveToFile(Qt.resolvedUrl("test.qml")); // => should call saveToFile(QUrl)
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                S 1 Reply Last reply 13 Nov 2020, 12:15
                0
                • K KroMignon
                  13 Nov 2020, 11:53

                  @St-Stanislav said in Call base object method from QML:

                  if it's any chance to call base class method from QML directly :)

                  What do you mean?

                  myCppClass.saveToFile("TEST"); // => should call saveToFile(QString) 
                  myCppClass.saveToFile(Qt.resolvedUrl("test.qml")); // => should call saveToFile(QUrl)
                  
                  S Offline
                  S Offline
                  St.Stanislav
                  wrote on 13 Nov 2020, 12:15 last edited by
                  #8

                  @KroMignon Well, looks like your solution is also not for me (cause of several methods with one name I have used the second solution from the link you posted). I get why this methods are called, get what solution I can use, but I was wondering if this problem can be solved from QML side :)
                  I have created tiny example project, but have no idea how to post it here correctly.

                  1 Reply Last reply
                  0
                  • KH-219DesignK Offline
                    KH-219DesignK Offline
                    KH-219Design
                    wrote on 13 Nov 2020, 17:18 last edited by
                    #9

                    I'm not sure if I understand things with total clarity.

                    However, if I understand, then these both exist in a base class:

                    /* (1) */ Q_INVOKABLE void saveToFile(QUrl url) {saveToFile(url.toLocalFile())};
                    /* (2) */ Q_INVOKABLE virtual void saveToFile(QString string) = 0;
                    

                    And some QML code is triggering entry/execution into (2) instead of (1).

                    In that case, I wonder why not just remove Q_INVOKABLE from (2) so that it is not-invokable from QML?

                    www.219design.com
                    Software | Electrical | Mechanical | Product Design

                    1 Reply Last reply
                    0

                    1/9

                    13 Nov 2020, 09:17

                    • Login

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