Results 1 to 10 of 10

Thread: Access multiple c++ data model roles within TableViewColumn

  1. #1
    Join Date
    Jun 2012
    Posts
    58
    Thanks
    13
    Qt products
    Qt4

    Question Access multiple c++ data model roles within TableViewColumn

    In ListView I could access all roles of QAbstractListModel by only mentioning them. TableViewColumn can access only one. If I have the model in qml itself I can access multiple roles by using nested ListElement per ListElement and using syntax like
    Qt Code:
    1. styleData.value.get(0, 1, .....).roleName
    To copy to clipboard, switch view to plain text mode 
    in itemDelegate of TableView
    How do i use QAbstractListModel from within qml TableView/TableViewColumn to access multiple roles provided by the model?

    [P.S: I didn't put a code because I don't have one. Got stuck just starting out on this, but if required I can put some boiler-plate code]

    {quick 2.0, qt 5.1.0, mingw, win7}

  2. #2

    Default Re: Access multiple c++ data model roles within TableViewColumn

    Did you ever get this figured out? I'm needing the exact same solution.

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

    Default Re: Access multiple c++ data model roles within TableViewColumn

    TableViewColumn has a "role" property where you can assign a role from a list model that is going to be displayed in a particular column.
    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.


  4. #4

    Default Re: Access multiple c++ data model roles within TableViewColumn

    Yes, but the problem is, one of my TableViewColumns needs to access multiple "roles" to display its column correctly. For example, if my data model has a "color" and "time" field, and I want to display the time with the background of that color in the same column, it seems I have to choose one role per column and I don't have easy access to both the color and time data in the same column.

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

    Default Re: Access multiple c++ data model roles within TableViewColumn

    You can always create your own delegate that accesses the data from the model directly by calling the role name

    javascript Code:
    1. delegate: Row {
    2. Text { text: role1 }
    3. Text { text: role2 }
    4. }
    To copy to clipboard, switch view to plain text mode 

    or implement a proxy model that will modify the data in such way that your table view only has to access a single role.
    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

    Default Re: Access multiple c++ data model roles within TableViewColumn

    I haven't been able to make that work with TableView. It seems other roles aren't available from within a TableViewColumn.

  7. #7
    Join Date
    May 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Access multiple c++ data model roles within TableViewColumn

    I figured out another way to access multiple roles in a single column. This example doesn't have a c++ model backing it, but I think it would work. I'll try that in the near future.

    The main thing to note is that you have access to the model inside the column. And from there you can query for whatever you want. I would assume that this might have a performance hit with big models.

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.1
    3.  
    4. Item{
    5. id: root
    6. ListModel {
    7. id: libraryModel
    8. ListElement{
    9. title: "A Masterpiece"
    10. author: "Gabriel"
    11. }
    12. ListElement{
    13. title: "Brilliance"
    14. author: "Jens"
    15. }
    16. ListElement{
    17. title: "Outstanding"
    18. author: "Frederik"
    19. }
    20. }
    21. TableView {
    22. id: table
    23. width: parent.width
    24. height: parent.height - 30
    25. TableViewColumn{
    26. role: "title"
    27. title: "Title"
    28. width: (root.width)/3
    29. }
    30. TableViewColumn{
    31. role: "author"
    32. title: "Author"
    33. width: (root.width)/3
    34. }
    35. TableViewColumn{
    36. title: "Author/Title"
    37. width: (root.width)/3
    38. delegate: Item{
    39. Text{
    40. text: model.get(styleData.row).author +"/"+ model.get(styleData.row).title
    41. color: "red"
    42. }
    43. }
    44. }
    45. model: libraryModel
    46. }
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

    Qt 5.2 / Ubuntu 14.04
    Last edited by colby.white; 26th May 2014 at 19:06. Reason: spelling corrections

  8. #8
    Join Date
    May 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Access multiple c++ data model roles within TableViewColumn

    When I used a model from C++, I had to use it as if it was an array. That's probably related to the fact that I set the context property with a QList.

    main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QGuiApplication app(argc, argv);
    4.  
    5.  
    6. QList<QObject*> model;
    7. model.append(new Book(QString("A Masterpiece"), QString("Gabriel")));
    8. model.append(new Book(QString("Brilliance"), QString("Jens")));
    9. model.append(new Book(QString("Outstanding"), QString("Frederick")));
    10.  
    11. QtQuick2ApplicationViewer viewer;
    12. QQmlContext *ctxt = viewer.rootContext();
    13. ctxt->setContextProperty("myModel", QVariant::fromValue(model));
    14.  
    15. viewer.setMainQmlFile(QStringLiteral("qml/main.qml"));
    16. viewer.showExpanded();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    QML:
    Qt Code:
    1. TableView {
    2. id: table
    3. width: parent.width
    4. height: parent.height - 30
    5. TableViewColumn{
    6. role: "title"
    7. title: "Title"
    8. width: (root.width)/3
    9. }
    10. TableViewColumn{
    11. role: "author"
    12. title: "Author"
    13. width: (root.width)/3
    14. }
    15. TableViewColumn{
    16. title: "Author/Title"
    17. width: (root.width)/3
    18. delegate: Item{
    19. Text{
    20. text: model[styleData.row].author +"/"+ model[styleData.row].title
    21. color: "red"
    22. }
    23. }
    24. }
    25. model: myModel
    26. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Access multiple c++ data model roles within TableViewColumn

    Thanks this is working.

  10. #10
    Join Date
    Jan 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Access multiple c++ data model roles within TableViewColumn

    Its flag an error during close the window, do you know why its happen?

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. Combine custom delegate and model data() roles
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2011, 13:43
  3. Replies: 1
    Last Post: 24th February 2011, 06:54
  4. Getting all roles of specified column and/or model
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2010, 12:21
  5. Access from Delegate to Model
    By starcontrol in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2008, 13:26

Tags for this Thread

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.