October 26, 2011

zibo zibo
Lab Rat
2 posts

Highlight problem for ListView entry when button pressed

 

Hello.
I’m new in QLM and I have a problem with some task related to QML ListView. I need to make entries highlighted when pointed by mouse and selected when clicked. Everything works ok untill I press a button on some entry an than move to another – hover doesn’t work for other entries. It looks that pressed button blocks other elements (no onEntered, onExited and other signals of those elements). Here is a similar, simplified code (and without selection action for now):

ListDelegate.qml

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.      id: delegate
  5.      width: ListView.view.width
  6.      height: 40
  7.  
  8.      Rectangle {
  9.           id: background
  10.           anchors.fill: parent
  11.           color: mArea.containsMouse ? "thistle" : "lightsteelblue"
  12.      }
  13.      Text {
  14.           id: label
  15.           anchors.top: parent.top
  16.           anchors.topMargin: 2
  17.           anchors.left: parent.left
  18.           anchors.leftMargin: 2
  19.           font.family: "Helvetica";
  20.           font.pixelSize: 13;
  21.           font.bold: true
  22.           color: mArea.containsMouse ? "blue" : "red"
  23.           text: name + ":\n\t" + number + "\n"
  24.      }
  25.      MouseArea {
  26.           id: mArea
  27.           anchors.fill: parent
  28.           hoverEnabled: true
  29.      }
  30. }

ContactModel.qml

  1. import QtQuick 1.0
  2.  
  3. ListModel {
  4.      ListElement {
  5.           name: "Bill Smith"
  6.           number: "555 3264"
  7.      }
  8.      ListElement {
  9.           name: "John Brown"
  10.           number: "555 8426"
  11.      }
  12.      ListElement {
  13.           name: "Sam Wise"
  14.           number: "555 0473"
  15.      }
  16. }

main.qml

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.      width: 180; height: 200
  5.      
  6.      ListView {
  7.           id: list
  8.           width: 180; height: 200
  9.           clip: true
  10.           interactive: false
  11.           model: ContactModel {}
  12.           delegate: ListDelegate {}
  13.           focus: true
  14.      }
  15. }

Is there any solution (the simpler – the better :) )?

4 replies

October 27, 2011

mbrasser mbrasser
Ant Farmer
452 posts

Hi,

Are you pressing the mouse button and then moving into another entry with the mouse button still pressed? In Qt (and QML), accepting a mouse press will “grab” all further mouse events until the button is released. In this case that means that the MouseArea you pressed in will get all mouse events (and the MouseAreas in other entries will get none), until the button is released.

Regards,
Michael

October 27, 2011

zibo zibo
Lab Rat
2 posts

Yes, it is exactly what I am doing. Is there any solution to stop sending all events only to that single MouseArea?

Thanks

October 28, 2011

mbrasser mbrasser
Ant Farmer
452 posts

I don’t know of one unfortunately — hopefully others can chime in with some possible solutions.

Regards,
Michael

October 28, 2011

Andre Andre
Area 51 Engineer
6031 posts

AFAIK, there is no way to do that directly. The only way I can think of, is to not use a mouse area per item, but a mouse area that overlays the whole list instead. You can then use indexAt to get the indexes from your starting to your end point, so you should be able to calculate which items should be selected from that. The actual selection then needs to be set as a property of the items, I would say, so the delegates can highlight them.

Note that I did not try to do this myself.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ QML XHR Https Check      problem with PNG in QML under wince? ››

You must log in to post a reply. Not a member yet? Register here!