Results 1 to 8 of 8

Thread: implementation/design quesiton

  1. #1
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default implementation/design quesiton

    Hi,

    I'm a bit new to C++, but know Qt/QML more or less from PySide. But now I stuck a bit.

    My GUI is written in QML, you select 3 different things and depending on the selection, different *.ics files are parsed, to do that I have a class (Freiertag) with a search method (all in seperate cpp/hpp) which is bound to QML via setContextProperty and I have a ListModel (FreiListModel) also bound to QML. My problem is the following: I don't know how to let the both parts communicate. How to transfer the parsed data (QStringList) from Freiertag to the model? The model has it's own function addEvent(), which I wanted to use.

    So my idea was:
    1. create a new instance of the model in main.cpp.
    2. create instance of Freiertag (which offers parsing function) and pass the model by address to the Freiertag object
    3. bind both via setContextProperty to QML
    4. access the model in Freiertag and use the addEvent() function

    I thought, if the model is passed by address and not const, it's data will change and it can be bound to QML as well.
    But if I pass it in this way, it's can not be accessed from the parser/search function, in the constructor it is possible to call the addEvent() function, but not from anywhere else! I think it's not public in the other scopes, but how to "say" in the header that I want the passed Model available for public access?!

    I think it's more a C++ sepcific question, but I did not find any example and maybe someone has a better idea?!

    AlphaX2

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implementation/design quesiton

    Can you post at least the header of Freiertag, of the model and how you create the two instance you already export?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: implementation/design quesiton

    Thank you for this fast answer!

    Here are the code, I hope it's okay to post it here, if PasteBin is better please let me know!


    Freiertag.hpp

    Qt Code:
    1. #ifndef FREIERTAG_HPP_
    2. #define FREIERTAG_HPP_
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QDebug>
    7.  
    8. #include "FreiListModel.hpp"
    9.  
    10.  
    11. class Freiertag : public QObject {
    12. Q_OBJECT
    13.  
    14. public:
    15. Freiertag(FreiListModel *model, QObject *parent = 0);
    16. //FreiListModel freiModel; // use here the passed model?!
    17. Q_INVOKABLE void suche(QString bland, QString jahr, QString typ);
    18.  
    19. ~Freiertag();
    20.  
    21. private:
    22. void sucheFerien(const QString &bland, const QString &jahr);
    23. void sucheFeiertage();
    24. void sucheAlleFeiertage();
    25. };
    26. #endif /* FREIERTAG_HPP_ */
    To copy to clipboard, switch view to plain text mode 

    FreiListModel.hpp
    Qt Code:
    1. ifndef FREILISTMODEL_HPP_
    2. #define FREILISTMODEL_HPP_
    3.  
    4. #include <QAbstractListModel>
    5. #include <QVariant>
    6. #include <QList>
    7. #include <QString>
    8.  
    9.  
    10. class FreiEvent
    11. {
    12. public:
    13. QString eventName;
    14. QString eventDate;
    15.  
    16. FreiEvent();
    17. FreiEvent(QString event, QString date);
    18. ~FreiEvent();
    19. };
    20.  
    21.  
    22. class FreiListModel : public QAbstractListModel
    23. {
    24. Q_OBJECT
    25.  
    26. enum ModelRoles {
    27. EventRole = Qt::UserRole + 1,
    28. DateRole
    29. };
    30.  
    31. public:
    32. FreiListModel(QObject *parent = 0);
    33.  
    34. int rowCount(const QModelIndex &parent) const;
    35. QVariant data(const QModelIndex &parent, int role=Qt::DisplayRole) const;
    36.  
    37. void addEvent(QString event, QString date);
    38. void printInfo();
    39.  
    40. ~FreiListModel();
    41.  
    42. private:
    43. QList<FreiEvent> resultList;
    44.  
    45. };
    46. #endif /* FREILISTMODEL_HPP_ */
    To copy to clipboard, switch view to plain text mode 

    part of main.cpp
    Qt Code:
    1. //create a new ListModel for the results
    2. FreiListModel *freiModel = new FreiListModel(app);
    3.  
    4. //create a new instance of our object that is doing the most work
    5. Freiertag *frei = new Freiertag(freiModel, app);
    6.  
    7. QDeclarativeView viewer;
    8.  
    9. viewer.rootContext()->setContextProperty("frei", frei);
    10. viewer.rootContext()->setContextProperty("freiModel", freiModel);
    To copy to clipboard, switch view to plain text mode 

    Thank you!

    AlphaX2

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implementation/design quesiton

    That looks good as far as I can tell.

    What you are currently missing (if I understand your goal correctly) is a member in Freiertag that keeps the model pointer around.

    Something like
    Qt Code:
    1. class Freiertag : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. Freiertag(FreiListModel *model, QObject *parent = 0);
    6. //FreiListModel freiModel; // use here the passed model?!
    7. Q_INVOKABLE void suche(QString bland, QString jahr, QString typ);
    8.  
    9. ~Freiertag();
    10.  
    11. private:
    12. void sucheFerien(const QString &bland, const QString &jahr);
    13. void sucheFeiertage();
    14. void sucheAlleFeiertage();
    15.  
    16. private:
    17. FreiListModel *m_model;
    18. };
    To copy to clipboard, switch view to plain text mode 

    And initialize the member like this from the constructor argument:
    Qt Code:
    1. Freiertag::Freiertag(FreiListModel *model, QObject *parent)
    2. : QObject(parent)
    3. , m_model(model)
    4. {}
    To copy to clipboard, switch view to plain text mode 

    Then you have access to the model througout Freiertag's methods.

    Cheers,
    _

  5. #5
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: implementation/design quesiton

    Hmm it seems to be thing I was looking for, because qDebug() << m_model and qDebug() << freiModel gives me in: "FreiListModel(0x70658890)" So the memory seems to be the same, so the model seems to be the same.
    My plan was, to explain it a bit better, to pass the model by reference/adress to Freiertag, but make it not const or something, so that the data can be changed from Freiertag and also the changes are visible in QML so in the end I want m_model and also freiModel (main.cpp) to be the exact same object. Both QML and Freiertag should "share" the same object and so the same data. Hope that makes it more clear what the plan is.

    But in the QML ListView is not any result shown, also if I try to add some directly with m_model->addEvent("Test", "Date"). Not sure - is there a signal that have to be emited, that QML "knows" that something changed?!

    I'm not sure whats wrong.

  6. #6
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: implementation/design quesiton

    Okay, I think I got it. I added a this->reset() in the method addEvent of the model, because this forces the Model/View to update. Maybe not the best solution, but not really clear to me, how to get the dataChanged() signal to work, because it want that I pass two QModelIndex Objects, but how to create them with the right indezes?!

    AlphaX2

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implementation/design quesiton

    Since you are adding something to the model you have to call beginInsertRows() before adding, then add, then call endInsertRows().
    Since you are in a list model, the parent model index for beginInsertRows() is the invalid model index, i.e. QModelIndex()

    Cheers,
    _

  8. #8
    Join Date
    Jun 2013
    Location
    Dresden - Germany
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: implementation/design quesiton

    That's it! Thank you very much! I pass to the method just a QModelIndex and 0 as beginn and <mylistofdata>.length() as end. This works very well!

    Thanks again!

    AlphaX2

Similar Threads

  1. Facade design pattern implementation
    By ehnuh in forum General Programming
    Replies: 5
    Last Post: 2nd November 2012, 16:37
  2. TCP Protocol Implementation
    By keelerjr12 in forum Qt Programming
    Replies: 4
    Last Post: 27th August 2012, 03:22
  3. Qt Multitouch implementation
    By sonulohani in forum Qt Programming
    Replies: 0
    Last Post: 7th June 2012, 06:51
  4. Replies: 3
    Last Post: 5th October 2008, 23:41
  5. Need some help on design and implementation
    By cool_qt in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2008, 21:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.