I created a qtquick 2.4 file and in the file I used TextInput and ListView. What I want is when user writing something in the TextInput, ListView appears below the TextInput to suggest some words to the user and when user click out of the MouseArea of ListView or TextInput, anywhere in the qtquick file, ListView is being hidden. But my problem is I don't know how should I hide the listview when user click out of the MouseArea of ListView or TextInput? I tried to use Menu component instead of ListView but due to problem in using Menu component, I couldn't use that.
Rectangle{
height: 400;
width: 400;
Rectangle {
id: cbxRoot;
width: 150
height: 45;
color: "white";
border.color: "black";
radius: 5;
property bool cbxtriggered: false;
TextInput
{
id: cbxTextBox;
anchors.top: parent.top;
width: 150
height:45 ;
verticalAlignment: Text.AlignVCenter;
color: "black";
horizontalAlignment: Text.AlignHCenter;
clip: true;
readOnly: false;
MouseArea
{
id: cbxMouseArea;
anchors.fill: parent;
enabled: true;
onClicked:
{
cbxTextBox.focus = true;
if(cbxRoot.cbxtriggered)
{
cbxRoot.cbxtriggered = false;
}
else
{
cbxRoot.cbxtriggered = true;
}
}
}
selectByMouse: true;
Keys.onPressed:
{
cbxRoot.cbxtriggered = true;
}
onTextChanged:
{
var maxChar = 30;
var txtInput = cbxTextBox;
if(txtInput.text.length > maxChar)
{
txtInput.text = txtInput.text.substring(0,maxChar);
txtInput.cursorPosition = maxChar;
}
}
}
ListModel
{
id: cbxListModel;
ListElement { name: "Theme"; number: 0 }
ListElement { name: "Theme"; number: 1 }
ListElement { name: "Theme"; number: 2 }
ListElement { name: "Theme"; number: 3 }
ListElement { name: "Theme"; number: 4 }
}
ListView
{
id: cbxListView;
anchors.top : cbxTextBox.bottom;
visible: cbxRoot.cbxtriggered;
focus: true;
displayMarginBeginning: contentHeight;
displayMarginEnd: contentHeight;
spacing: 2;
model: cbxListModel;
delegate: Text {
text: name + ", " + number
}
clip: false;
width: cbxRoot.width;
height: 70;
}
}
}
focus
state and change visibility according to that. You can force focus withforceActiveFocus()
. If it is not a component you can add aMouseArea
outside theTextInput
and use that to unfocus it. Which one is your case? – BaCaRoZzo