Results 1 to 8 of 8

Thread: setRoleNames in Qt 5

  1. #1
    Join Date
    Jan 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default setRoleNames in Qt 5

    Hi Guys,

    I try to follow example in forum/internet to using QSqlQueryModel or QSqlTableModel in Qml.
    but in doc that setRoleNames is deprecated, how to set new role in Qt 5?

    rgds

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

    Default Re: setRoleNames in Qt 5

    Subclass and reimplement roleNames().
    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.


  3. #3
    Join Date
    Mar 2014
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: setRoleNames in Qt 5

    Can someone give an example or a code snippet which will show how we can port setRoleNames() API in Qt5?

  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: setRoleNames in Qt 5

    See comment #2.

    Create a sub class of the model.
    Reimplement (overwrite) the roleNames() method.

    Neither creating a subclass nor overwriting a virtual method is specific to Qt but basic C++ idioms.

    Cheers,
    _

  5. #5
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Thumbs up Re: setRoleNames in Qt 5

    Hi,
    Well, I had exaclty the same question as yandi. And I did not find the roleNames() method.
    Post edited, because I though the roleNames() method wouldn't be a member of QSqlQueryModel, but it is indeed via the QAbstractItemModel:
    http://qt-project.org/doc/qt-5/qabst...html#roleNames
    Best,
    Sven
    Last edited by shsdev; 1st May 2014 at 20:10. Reason: My comment was mistaken, corrected

  6. #6
    Join Date
    Oct 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: setRoleNames in Qt 5

    How to reimplement roleNames:

    Qt Code:
    1. QHash<int, QByteArray> ClassName::roleNames() const
    2. {
    3. QHash<int, QByteArray> roles;
    4. roles[ObjectRole] = "object";
    5. return roles;
    6. }
    7.  
    8. protected:
    9. QHash<int, QByteArray> roleNames() const;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2016
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: setRoleNames in Qt 5

    well I did just that and the method never get called, any idea why?

  8. #8
    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: setRoleNames in Qt 5

    Make sure you have the correct signature.

    If you are using a C++11 capable compiler, try adding the override keyword so you'll get an error on signature mismatch.

    Cheers,
    _

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.