Results 1 to 9 of 9

Thread: Qt and Templates

  1. #1
    Join Date
    Mar 2006
    Location
    Moldova
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt and Templates

    Hi.

    First, sorry for my bad knowledge of C++ templates

    I have 2 classes: Street and Manager.

    Qt Code:
    1. class Street
    2. {
    3. public:
    4. Street();
    5. ~Street();
    6.  
    7. QMap<QString, QString> values; // ex: "id" => "1", "name" => "Paris"
    8. ....
    9. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class Manager
    2. {
    3. public:
    4. Manager();
    5. ~Manager();
    6.  
    7. QMap<QString, QString> values; // ex: "id" => "1", "name" => "John"
    8. ....
    9. };
    To copy to clipboard, switch view to plain text mode 

    Now i have 2 QAbstractTableModel subclasses: one that works with streets and one for managers.

    Qt Code:
    1. class StreetTableModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit StreetTableModel(QObject *parent = 0);
    7. ....
    8.  
    9. private:
    10. QList<Street*> records; // Here we store the streets
    11. ....
    12. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ManagerTableModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit ManagerTableModel(QObject *parent = 0);
    7. ....
    8.  
    9. private:
    10. QList<Manager*> records; // Here we store the managers
    11. ....
    12. };
    To copy to clipboard, switch view to plain text mode 

    In the future there will be a lot of objects like street and manager and i don't want to create each time a subclass of QAbstractTableModel.

    Can you please show me a way to define a subclass of QAbstractTableModel using templates ? I tried some variants, but a don't get it how it must work.

    I want to be able for example to create a model like:

    Qt Code:
    1. TableModel<Street> *model = new TableModel();
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot.
    All Rights Reserved ...

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and Templates

    Try something like

    Qt Code:
    1. template<typename T>
    2. class TableModel : public QAbstractTableModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit TableModel(QObject *parent = 0);
    8. ....
    9.  
    10. private:
    11. typename QList<T *> records;
    12. ....
    13. };
    To copy to clipboard, switch view to plain text mode 

    It's dry coded and un-tested, but I'd expect it to behave like you want:

    Qt Code:
    1. TableModel<Street> *model = new TableModel<Street>();
    To copy to clipboard, switch view to plain text mode 

    Note that this might mean a lot of the code needs to be templated, which means you're going to have to put all that implementation in a header file. Be sure to document the requirements on T. Also, given the nature of the example classes you give, you can consider not using pointers in your list. This will probably make management of the records much simpler. If that's possible, use
    Qt Code:
    1. typename QList<T> records;
    To copy to clipboard, switch view to plain text mode 
    instead.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qt and Templates

    I would suggest not to use template for your requirement, and use C++ polymorphism. Here is an example, the main point is to use BasicValue class as a interface for future classes / objects

    Qt Code:
    1. class BasicValue
    2. {
    3. public:
    4. BasicValue();
    5. virtual ~BasicValue();
    6.  
    7.  
    8. QMap<QString, QString> values; // ex: "id" => "1", "name" => "Paris" (anything)
    9. ....
    10. };
    11.  
    12.  
    13. class Street : public BasicValue {};
    14. class Manager : public BasicValue {};
    15. class FutureItem : public BasicValue {};
    16.  
    17.  
    18. class ValueTableModel : public QAbstractTableModel
    19. {
    20. Q_OBJECT
    21.  
    22.  
    23. public:
    24. explicit ValueTableModel(QObject *parent = 0);
    25. ....
    26.  
    27.  
    28. private:
    29. QList<BasicValue*> records;
    30. ....
    31. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and Templates

    That is a nicer solution. I gave what OP wants, you gave what OP needs.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt and Templates

    Some time ago I was thinking about a template based approach to the problem myself too but the downside was that you can have models with different number of columns or different types of data. Neither of the approaches presented here actually solve the problem. I think it is possiblt to do it both using templates and polymorphism. As for the latter, we go in direction of QStandardItemModel so maybe there is no point in doubling what it already provides (you can see that the solution presented in this thread looks strikingly like QStandardItemModel with BasicValue being QStandardItem and Street being a subclass of the latter).

    As for the template based approach I think we'd need to provide information to the template class about ways of accessing data in the model for instance by passing a container holding the names or types of data in each column. An example instantiation could then look like this:
    Qt Code:
    1. class RecordDescription {
    2. public:
    3. //...
    4. const_iterator begin() const;
    5. const_iterator end() const;
    6. //...
    7. };
    8.  
    9. TemplateBasedModel<RecordType, RecordDescription> model;
    To copy to clipboard, switch view to plain text mode 
    or:
    Qt Code:
    1. TemplateBasedModel<RecordType> model(recordDescription);
    To copy to clipboard, switch view to plain text mode 
    with "modelDescription" being an object that can read and write data to RecordType instances.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2006
    Location
    Moldova
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and Templates

    Quote Originally Posted by franz View Post
    Try something like
    Qt Code:
    1. template<typename T>
    2. class TableModel : public QAbstractTableModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit TableModel(QObject *parent = 0);
    8. ....
    9.  
    10. private:
    11. typename QList<T *> records;
    12. ....
    13. };
    To copy to clipboard, switch view to plain text mode 

    I get errors on line 11 in your code:

    error: expected nested-name-specifier
    error: invalid declarator before 'records'

    This is my class (i get the same errors in my class):
    Qt Code:
    1. template<typename TPL>
    2. class JSONTableModel : public QAbstractTableModel
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit JSONTableModel(QObject *parent = 0);
    8.  
    9. int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
    10. int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
    11.  
    12. QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
    13. bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
    14. QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
    15. Qt::ItemFlags flags ( const QModelIndex & index ) const;
    16.  
    17. int getTotalRecordsCount();
    18. int getFetchedRecordsCount();
    19. QString getParam(QString);
    20. QString getHeaderKey(int);
    21. void setParam(QString, QString);
    22.  
    23. private:
    24. typename QList<TPL *> records;
    25. QMap<QString, QString> headers;
    26. QMap<QString, QString> params;
    27. QNetworkAccessManager *manager;
    28. QModelIndex updatingIndex;
    29. QString url;
    30. int totalRecords;
    31.  
    32. signals:
    33. void dataLoaded();
    34. void requestStarted();
    35.  
    36. public slots:
    37. void loadRecords();
    38. void replyFinished(QNetworkReply*);
    39. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by jsmax; 25th September 2011 at 13:00.
    All Rights Reserved ...

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt and Templates

    "typename" is not needed in this case, its needed when the template parameter has a nested type and you want to use it:
    Qt Code:
    1. template<class T>
    2. class Test{
    3. public:
    4. Test(){
    5. // T::NestedClass t; // this wont compile
    6. typename T::NestedClass t; // this is ok
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2006
    Location
    Moldova
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and Templates

    QObject classes does not support templates

    During compilation i get this:

    Error: Template classes not supported by Q_OBJECT
    All Rights Reserved ...

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt and Templates

    They do, only that you can't use Q_OBJECT macro with a template class. You need to subclass twice -- first introduce Q_OBJECT, define all the signals and slots you want and then subclass again and introduce the template.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. help with templates please
    By GrahamLabdon in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 17:29
  2. templates
    By mickey in forum General Programming
    Replies: 5
    Last Post: 16th January 2008, 15:25
  3. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  4. templates and Q_OBJECT
    By TheKedge in forum General Programming
    Replies: 12
    Last Post: 15th September 2006, 05:41
  5. Templates : Help Please
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 14th February 2006, 13:50

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
  •  
Qt is a trademark of The Qt Company.