Create a new item and have it run some code
-
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<XmlConnect> connectRoles(); QDeclarativeListProperty<XmlSearch> 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...
-
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.
-
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 -
Yeah, the constructor is a good place for it in this situation.
-
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?
-
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
}
@