Results 1 to 6 of 6

Thread: QML, ListView and Frustration

  1. #1
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default QML, ListView and Frustration

    I have a C++ QWidget project in which I'd like to use QML for a couple of lists I have in the application. However, I find that doing something as simple as highlighting an item that is clicked on is proving to be difficult. I can't wait to see what a frustration this is will be when I go to add a scroll bar to my list. Anyway, I'm sure it's just because I'm new to QML but still, it's infuriating.

    I have a QML file with some sample model data that looks like:

    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Item
    4. {
    5. width: 300
    6. height: 200
    7.  
    8. ListModel
    9. {
    10. id: myModel2
    11.  
    12. ListElement { text: "List Item 1" }
    13. ListElement { text: "List Item 2" }
    14. ListElement { text: "List Item 3" }
    15. ListElement { text: "List Item 4" }
    16. ListElement { text: "List Item 5" }
    17. ListElement { text: "List Item 6" }
    18. }
    19.  
    20. Component
    21. {
    22. id: beerDelegate
    23.  
    24. Rectangle
    25. {
    26. id: beerDelegateRectangle
    27. height: beerDelegateText.height * 1.5
    28. width: parent.width
    29.  
    30. Text
    31. {
    32. id: beerDelegateText
    33. text: "<b>" + modelData + "</b> <i>(" + modelData + ")</i>"
    34. }
    35.  
    36. MouseArea
    37. {
    38. anchors.fill: parent
    39. onClicked:
    40. {
    41. console.log("clicked: " + modelData + " at index: " + index);
    42. beerList.currentIndex = index;
    43. }
    44. }
    45. }
    46. }
    47.  
    48. ListView
    49. {
    50. id: beerList
    51. anchors.fill: parent
    52. model: myModel2
    53. delegate: beerDelegate
    54.  
    55. highlightFollowsCurrentItem: true
    56. highlight: Rectangle
    57. {
    58. width: parent.width
    59. color: "red"
    60. }
    61.  
    62. focus: true
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

    This is adapted from a couple examples I found online. All I'm trying to do with now is get an item to be highlighted in red when I click on it. I thought that by defining the "highlight" property in my ListView (beerlist) and handling "onClicked" it should do it but.. ugh...

    This is incredibly frustration and makes little sense to me right now. Help, please?
    Last edited by zethon; 8th December 2012 at 15:09.

  2. #2
    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: QML, ListView and Frustration

    It seems to me your delegate overdraws the highlight. Change the delegate's item from "Rectangle" to "Item".
    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
    Nov 2012
    Posts
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: QML, ListView and Frustration

    Thank you.

    In general, is QML a viable option for good ol' fashion desktop applications? For example, my main goal with this question was to create a list box similar to a buddy list, with images and rich text. Is this something that would be better done in QML? Or perhaps with something like QListWidget?

    Thanks again.

  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: QML, ListView and Frustration

    Really depends on the context.
    QtQuick has the advantage that is allows you to have all kinds of animations, e.g. when the list's content changes.

    Of course it requires a different skill set, so you'll have to put quite some time into learning it properly.
    If you are already good at using Qt's C++ API you might get further faster using a QListWidget or a QListView with a custom item delegate.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML, ListView and Frustration

    This solution might be helpful to other developers.

    Change the code as per below:

    import QtQuick 1.0

    Item
    {
    width: 300
    height: 200

    ListModel
    {
    id: myModel2

    ListElement { text: "List Item 1" }
    ListElement { text: "List Item 2" }
    ListElement { text: "List Item 3" }
    ListElement { text: "List Item 4" }
    ListElement { text: "List Item 5" }
    ListElement { text: "List Item 6" }
    }

    Component
    {
    id: beerDelegate

    Rectangle
    {
    id: beerDelegateRectangle
    height: beerDelegateText.height * 1.5
    width: parent.width

    Text
    {
    id: beerDelegateText
    text: "<b>" + modelData + "</b> <i>(" + modelData + ")</i>"
    }

    MouseArea
    {
    anchors.fill: parent
    onClicked:
    {
    console.log("clicked: " + modelData + " at index: " + index);
    beerList.currentIndex = index;
    beerList.highlight = highlightBar; // Load this as a seperate component onclick of mouse
    }
    }
    }
    }

    ListView
    {
    id: beerList
    anchors.fill: parent
    model: myModel2
    delegate: beerDelegate

    //highlightFollowsCurrentItem: true
    //highlight: Rectangle
    //{
    // width: parent.width
    // color: "red"
    //}

    focus: true
    }

    // Component to control ListView highlight property
    Component {
    id: highlightBar
    Rectangle {
    //width: 200; height: 50
    color: "lightgrey"
    // y: listView.currentItem.y;
    // Behavior on y { SpringAnimation { spring: 2; damping: 0.1 } }
    }
    }
    }

  6. #6
    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: QML, ListView and Frustration

    Quote Originally Posted by raunvivek View Post
    This solution might be helpful to other developers.
    wysota already explained the solution, simply not painting over the highlight.

    If you really feel the need to resurrect a 3 year old thread just to post some code that is mostly commented out, it would be nice if it would at least be done with proper code tags.

    Even more ideally maybe not post half backed work arounds into threads that have a proper solution already.

    Someone might find this thread, not read the comments and conclude the code you posted is the way to go instead of using the trivial solution provided in the very first comment.

    Cheers,
    _

Similar Threads

  1. listview problem (i need help)
    By rimie23 in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2012, 19:03
  2. should i use listview ?
    By rimie23 in forum Qt Programming
    Replies: 20
    Last Post: 9th May 2012, 22:40
  3. qml listview
    By Le_B in forum Qt Quick
    Replies: 1
    Last Post: 25th May 2011, 13:01
  4. osx dylib frustration
    By gri in forum General Programming
    Replies: 3
    Last Post: 16th May 2009, 15:55
  5. listview
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2009, 12:05

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.