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. Create a new item and have it run some code
Forum Updated to NodeBB v4.3 + New Features

Create a new item and have it run some code

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 2.0k Views 1 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.
  • K Offline
    K Offline
    keba
    wrote on last edited by
    #1

    Hi,

    I'm trying to create an alternative implementation of XmlListModel (see http://qt-project.org/forums/viewthread/22556/). Unfortunately, I'm totally new to Qt/QML, and still very new to programming. So, I have created this new QML object that I want to run some code. Now I'm trying to have some code to run every single time my object is called from QML. This is the class:
    @
    class XmlListModel2 : public QDeclarativeItem
    {
    Q_OBJECT
    Q_PROPERTY(QString source READ source WRITE setSource)
    Q_PROPERTY(QString query READ query WRITE setQuery)
    Q_PROPERTY(QDeclarativeListProperty<XmlConnect> connectRoles READ connectRoles)
    Q_PROPERTY(QDeclarativeListProperty<XmlSearch> searchRole READ searchRole) //
    public:
    XmlListModel2(QDeclarativeItem *parent = 0);

     QString source() const;
     void setSource(const QString &source);
     QString query() const;
     void setQuery(const QString &query);
    
     QDeclarativeListProperty&lt;XmlConnect&gt; connectRoles();
     QDeclarativeListProperty&lt;XmlSearch&gt; searchRole();
    

    private:
    void test(void);
    QList<XmlConnect *> m_connectRoles;
    QList<XmlSearch *> m_searchRole; //
    QString m_source;
    QString m_query;
    };
    @

    Now, in XmlListModel2.cpp, I have:
    @
    void XmlListModel2::test(void){
    qWarning("Test: " + m_query.toAscii());
    }
    @

    Except the problem is, nothing happens. I should create some output saying:
    @
    Test: name/string()
    @

    How can I get this to work?

    And while you're at it, if you know the answer: How do I have it create a model? I mean XmlConnect should not be one element in a list, but instead create a list (like XmlRole). I would really appreciate some help. When I get it to work, I will post the result for everyone...

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You have not shown any code that would invoke the test() method, so it's impossible to say why it fails for you. Do you call it in class constructor? Or do you want to invoke it from QML?

      Also, output stream for the app might not be connected to the place you are looking for output in. I cannot assess which is the cause of your problem, though, please provide more information.

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        keba
        wrote on last edited by
        #3

        Hi,

        thank you very much for your answer. Well, I haven't shown any code that invokes test() because I didn't know I had to write it. I'm so new to all this. Sorry. I would like test() to be called whenever I create a XmlListModel2 in QML, e.g.:
        @
        XmlListModel2 {
        id: something
        source: "file.xml"
        query: "name/string()"

        XmlConnect { ... }
        XmlSearch { ... }
        ....
        

        }
        @
        I guess that means I want to call it in class constructor? Thanks

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Yeah, the constructor is a good place for it in this situation.

          (Z(:^

          1 Reply Last reply
          0
          • K Offline
            K Offline
            keba
            wrote on last edited by
            #5

            Well, that makes a lot of sense to me. But what do I need to do to make it work? I'm sorry: I'm so new to this that I have no clue which part of the code is the constructor and what I need to add in order to call my function test(). Can you just tell me please?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You may need to consider doing some easier examples at the beginning.

              Here is what your constructor probably looks like:
              @
              XmlListModel2::XmlListModel2(QDeclarativeItem *parent) : QDeclarativeItem(parent)
              {
              test(); // just add this line
              }
              @

              (Z(:^

              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