Results 1 to 9 of 9

Thread: Qt and Templates

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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 ...

  3. #3
    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 

  4. #4
    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 ...

  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

    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.