Results 1 to 3 of 3

Thread: Type information in QML TableView

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Type information in QML TableView

    I have a QML TableView which is dynamically setting up its columns according to a c++ model. I want to use different column delegates for different types. Is there any possibility to get the typeName of a model property from within QML? I have a workaround by using a typeName provided (as a string) by my model, but I'd rather directly have the typeName from QML. Is that possible at all?


    Here's my code so far:
    Qt Code:
    1. import QtQuick 2.5
    2. import QtQuick.Controls 1.4
    3. import QtQmlTricks 1.0
    4. import "qrc:/import/QtQmlTricks/" as QmlTricks
    5.  
    6.  
    7.  
    8. TableView {
    9. // model: must be bound to an external model, which has to be properly filled before TableView is created
    10. model: mainWindow.mainWindowTableModel
    11. id: dynamicTableViewBasis
    12. property var tableModel: dynamicTableViewBasis.model
    13. signal clickedEntry(var id, string role, var value)
    14.  
    15. width: parent ? parent.width : 0
    16.  
    17. resources:
    18. {
    19. var roleList = dynamicTableViewBasis.tableModel.userRoleNames
    20. var temp = []
    21. for(var i=0; i<roleList.length; i++)
    22. {
    23. var role = roleList[i]
    24. if (!dynamicTableViewBasis.tableModel.isColumnHidden(role)) {
    25. var columnHeaderName = dynamicTableViewBasis.tableModel.columnHeaderName(role)
    26. if (columnHeaderName === "") {
    27. columnHeaderName = role
    28. }
    29. if (dynamicTableViewBasis.tableModel.columnTypeName(role) == "bool") {
    30. temp.push(columnComponentBool.createObject(dynamicTableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidth(role)}))
    31. } else {
    32. temp.push(columnComponentText.createObject(dynamicTableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidth(role)}))
    33. }
    34. }
    35. }
    36. return temp
    37. }
    38.  
    39. Component {
    40. id: columnComponentText
    41. TableViewColumn {
    42. delegate: Text {
    43. id: textDelegate
    44. text: styleData.value ? styleData.value : ""
    45. color: styleData.textColor ? styleData.textColor : ""
    46. elide: styleData.elideMode ? styleData.elideMode : ""
    47.  
    48. MouseArea {
    49. anchors.fill: parent
    50. onClicked: {
    51. var id = dynamicTableViewBasis.tableModel.data(dynamicTableViewBasis.tableModel.index(styleData.row,0),dynamicTableViewBasis.tableModel.roleForName("myId"))
    52. var role = dynamicTableViewBasis.getColumn(styleData.column).role
    53. var value = styleData.value
    54. dynamicTableViewBasis.clickedEntry(id, role, value)
    55. }
    56. }
    57. }
    58. }
    59. }
    60. Component {
    61. id: columnComponentBool
    62. TableViewColumn {
    63. delegate: CheckBox {
    64. id: checkBoxDelegate
    65. text: ""
    66. checked: styleData.value ? styleData.value : undefined
    67. onClicked: {
    68. var id = dynamicTableViewBasis.tableModel.data(dynamicTableViewBasis.tableModel.index(styleData.row,0),dynamicTableViewBasis.tableModel.roleForName("myId"))
    69. var role = dynamicTableViewBasis.getColumn(styleData.column).role
    70. var value = checkBoxDelegate.checked
    71. dynamicTableViewBasis.clickedEntry(id, role, value)
    72.  
    73. }
    74. }
    75. }
    76. }
    77. }
    To copy to clipboard, switch view to plain text mode 

  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: Type information in QML TableView

    You could try the JavaScript typeof operator.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sedi (23rd November 2015)

  4. #3
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Type information in QML TableView

    Thank you, that was exactly what I was looking for! Unfortunately typeof does not differentiate too much between types - it's java-after all. A QDate is just an "object", as is a QTime. So I will probably stick to the manual type information approach via c++. But I will definitely need the typeof operator in other contexts.
    Thanks a lot!

Similar Threads

  1. Replies: 0
    Last Post: 22nd November 2015, 23:43
  2. What is Type information when creating Class
    By vinothrajendran in forum Qt Tools
    Replies: 1
    Last Post: 20th March 2015, 13:11
  3. Replies: 3
    Last Post: 1st April 2013, 15:26
  4. Print out graphics item type information
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2012, 20:53
  5. Replies: 2
    Last Post: 4th September 2010, 07:18

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.