QML Google Suggest: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Snippets]]<br />[[Category:HowTo]]<br />[[Category:Developing_with_Qt::Qt Quick]]<br />[[Category:Developing_with_Qt::Qt Quick::QML]]
[[Category:Snippets]]
[[Category:HowTo]]
[[Category:Developing_with_Qt::Qt Quick]]
[[Category:Developing_with_Qt::Qt Quick::QML]]


[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
[toc align_right="yes" depth="2"]


= Google Suggest =
= Google Suggest =
Line 13: Line 16:
= Implementation =
= Implementation =


<code>// GoogleSuggest.qml<br />import QtQuick 1.1
<code>// GoogleSuggest.qml
import QtQuick 1.1


Rectangle {<br /> id: googleSuggest<br /> property string textToSugget<br /> property string dsParam<br /> property bool timeToDie: false<br /> property int rowHeight: 30<br /> width: parent.width<br /> height: rowHeight*listView.count<br /> anchors.top: parent.bottom<br /> anchors.left: parent.left<br /> anchors.topMargin: 5
Rectangle {
id: googleSuggest
property string textToSugget
property string dsParam
property bool timeToDie: false
property int rowHeight: 30
width: parent.width
height: rowHeight*listView.count
anchors.top: parent.bottom
anchors.left: parent.left
anchors.topMargin: 5


signal itemChoosen(string suggestion);
signal itemChoosen(string suggestion);
Line 21: Line 35:
onTimeToDieChanged: googleSuggest.destroy();
onTimeToDieChanged: googleSuggest.destroy();


XmlListModel {<br /> id: xmlModel<br /> source: &quot;http://suggestqueries.google.com/complete/search?output=toolbar&amp;quot;+<br /> (dsParam!==&quot;&quot; ? &quot;&amp;ds=&quot;''dsParam : &quot;&quot;)''<br /> &quot;&amp;q=&quot;+textToSugget
XmlListModel {
id: xmlModel
source: "http://suggestqueries.google.com/complete/search?output=toolbar"+
(dsParam!=="" ? "&amp;ds="''dsParam : "")''
"&amp;q="+textToSugget


query: &quot;/toplevel/CompleteSuggestion&amp;quot;
query: "/toplevel/CompleteSuggestion"


XmlRole { name: &quot;suggestion&amp;quot;; query: &quot;suggestion/</code>data/string()&quot; }<br /> XmlRole { name: &quot;num_queries&amp;quot;; query: &quot;num_queries/<code>int/string()&quot; }<br /> }
XmlRole { name: "suggestion"; query: "suggestion/@data/string()" }
XmlRole { name: "num_queries"; query: "num_queries/@int/string()" }
}


ListView {<br /> id: listView<br /> anchors.fill: parent<br /> cacheBuffer: 10<br /> highlight: Rectangle { color: Qt.rgba(0,0,1,0.3); radius: 8; z: listView.z+10 }<br /> highlightFollowsCurrentItem: true<br /> interactive: false<br /> currentIndex: <s>1
ListView {
<br /> model: xmlModel<br /> delegate: Rectangle {<br /> id: resultRow
id: listView
<br /> width: parent.width<br /> height: rowHeight<br /> color: (index % 2 === 0 ? &quot;#ffffff&amp;quot; : &quot;#eeeeff&amp;quot;)<br /> border.color: &quot;#000000&amp;quot;<br /> border.width: 1<br /> radius: 8
anchors.fill: parent
<br /> Text {<br /> id: result<br /> anchors.verticalCenter: parent.verticalCenter<br /> anchors.left: parent.left<br /> anchors.leftMargin: 5<br /> anchors.right: numberOfResults.left<br /> anchors.rightMargin: 10<br /> font.pixelSize: 15<br /> font.bold: true<br /> text: suggestion<br /> clip: true<br /> }<br /> Text {<br /> id: numberOfResults<br /> anchors.verticalCenter: parent.verticalCenter<br /> anchors.right: parent.right<br /> anchors.rightMargin: 5<br /> font.pixelSize: 12<br /> font.italic: true<br /> text: num_queries<br /> }
cacheBuffer: 10
<br /> }
highlight: Rectangle { color: Qt.rgba(0,0,1,0.3); radius: 8; z: listView.z+10 }
<br /> MouseArea {<br /> anchors.fill: listView<br /> onMouseYChanged: {<br /> listView.currentIndex = Math.floor((mouseY+rowHeight) / rowHeight)</s> 1;<br /> }<br /> onReleased: {<br /> googleSuggest.itemChoosen(xmlModel.get(listView.currentIndex).suggestion);<br /> timeToDie = true;<br /> }<br /> }<br /> }<br />}<br /></code>
highlightFollowsCurrentItem: true
interactive: false
currentIndex: -1
 
model: xmlModel
delegate: Rectangle {
id: resultRow
 
width: parent.width
height: rowHeight
color: (index % 2 === 0 ? "#ffffff" : "#eeeeff")
border.color: "#000000"
border.width: 1
radius: 8
 
Text {
id: result
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 5
anchors.right: numberOfResults.left
anchors.rightMargin: 10
font.pixelSize: 15
font.bold: true
text: suggestion
clip: true
}
Text {
id: numberOfResults
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 5
font.pixelSize: 12
font.italic: true
text: num_queries
}
 
}
 
MouseArea {
anchors.fill: listView
onMouseYChanged: {
listView.currentIndex = Math.floor((mouseY+rowHeight) / rowHeight)- 1;
}
onReleased: {
googleSuggest.itemChoosen(xmlModel.get(listView.currentIndex).suggestion);
timeToDie = true;
}
}
}
}
</code>


The functions below are used to dynamically load and unload the component. For example to pop up it when the user has entered at last 3 chars or unload it when TextInput.onAccepted signal is rised
The functions below are used to dynamically load and unload the component. For example to pop up it when the user has entered at last 3 chars or unload it when TextInput.onAccepted signal is rised


<code>// gs.js<br />var componentGS;<br />var objectGS;
<code>// gs.js
var componentGS;
var objectGS;


// dsParam = &quot;yt&amp;quot; only for YouTube suggest else it is empty<br />function popupGoogleSuggest ( callerItem, textString, dsParam ) {
// dsParam = "yt" only for YouTube suggest else it is empty
function popupGoogleSuggest ( callerItem, textString, dsParam ) {


deleteGoogleSuggest();<br /> componentGS = Qt.createComponent(&quot;GoogleSuggest.qml&amp;quot;);<br /> objectGS = componentGS.createObject(callerItem);<br /> if (objectGS === null) {<br /> console.log(&quot;Error creating GoogleSuggest.qml&amp;quot;, componentGS.errorString());<br /> } else {<br /> objectGS.textToSugget = textString;<br /> objectGS.dsParam = dsParam;<br /> }<br /> return objectGS;<br />}
deleteGoogleSuggest();
componentGS = Qt.createComponent("GoogleSuggest.qml");
objectGS = componentGS.createObject(callerItem);
if (objectGS === null) {
console.log("Error creating GoogleSuggest.qml", componentGS.errorString());
} else {
objectGS.textToSugget = textString;
objectGS.dsParam = dsParam;
}
return objectGS;
}


function deleteGoogleSuggest()<br />{<br /> if (componentGS[[Image:==null &amp;&amp; objectGS|==null &amp;&amp; objectGS]]==undefined) {<br /> objectGS.timeToDie = true;<br /> }<br />}</code>
function deleteGoogleSuggest()
{
if (componentGS[[Image:==null &amp;&amp; objectGS|==null &amp;&amp; objectGS]]==undefined) {
objectGS.timeToDie = true;
}
}</code>


= Usage =
= Usage =


<code>import QtQuick 1.1<br />import &quot;gs.js&amp;quot; as Functions
<code>import QtQuick 1.1
import "gs.js" as Functions


Rectangle {<br /> id: main<br /> width: 360<br /> height: 360
Rectangle {
id: main
width: 360
height: 360


Rectangle {<br /> id: textContainer<br /> x: 0; y:0; width: 250; height: 30<br /> color: &quot;#ffffff&amp;quot;<br /> border.color: &quot;#000000&amp;quot;<br /> border.width: 1
Rectangle {
id: textContainer
x: 0; y:0; width: 250; height: 30
color: "#ffffff"
border.color: "#000000"
border.width: 1


TextInput {<br /> id: textInput<br /> property GoogleSuggest gsComponent<br /> anchors.centerIn: parent<br /> selectByMouse: true<br /> text: &quot;write here&amp;quot;
TextInput {
id: textInput
property GoogleSuggest gsComponent
anchors.centerIn: parent
selectByMouse: true
text: "write here"


// Used to prevent predictive text to pop up while typing in a TextInput<br /> // component. Else onTextChanged event isn't rised:<br /> // https://bugreports.qt.nokia.com/browse/QTBUG-22298<br /> inputMethodHints: Qt.ImhNoPredictiveText
// Used to prevent predictive text to pop up while typing in a TextInput
// component. Else onTextChanged event isn't rised:
// https://bugreports.qt.nokia.com/browse/QTBUG-22298
inputMethodHints: Qt.ImhNoPredictiveText


function googleSuggest_item_choosen(text)<br /> {<br /> textInput.text=text;<br /> }
function googleSuggest_item_choosen(text)
{
textInput.text=text;
}


Timer {<br /> id: timerPopupGS<br /> running: false<br /> interval: 300<br /> repeat: false<br /> triggeredOnStart: false<br /> onTriggered: {<br /> if (textInput.focus &amp;&amp; textInput.text !== &quot;&quot; &amp;&amp; textInput.text.length &gt; 2) {<br /> // Pop up GoogleSuggest and catch itemChoosen signal<br /> textInput.gsComponent = Functions.popupGoogleSuggest(textContainer, textInput.text, &quot;&quot;);<br /> textInput.gsComponent.itemChoosen.connect(textInput.googleSuggest_item_choosen);<br /> }<br /> if (textInput.text.length&amp;lt;3) Functions.deleteGoogleSuggest();<br /> }<br /> }
Timer {
id: timerPopupGS
running: false
interval: 300
repeat: false
triggeredOnStart: false
onTriggered: {
if (textInput.focus &amp;&amp; textInput.text !== "" &amp;&amp; textInput.text.length > 2) {
// Pop up GoogleSuggest and catch itemChoosen signal
textInput.gsComponent = Functions.popupGoogleSuggest(textContainer, textInput.text, "");
textInput.gsComponent.itemChoosen.connect(textInput.googleSuggest_item_choosen);
}
if (textInput.text.length<3) Functions.deleteGoogleSuggest();
}
}


onTextChanged: {<br /> timerPopupGS.restart();<br /> if (text.length&amp;lt;3) Functions.deleteGoogleSuggest();<br /> }<br /> onAccepted: {<br /> // eventually close VK<br /> closeSoftwareInputPanel();<br /> Functions.deleteGoogleSuggest();<br /> }<br /> onFocusChanged: {<br /> if (!focus) closeSoftwareInputPanel();<br /> if (!textInput.focus) {<br /> Functions.deleteGoogleSuggest();<br /> }<br /> }<br /> }
onTextChanged: {
timerPopupGS.restart();
if (text.length<3) Functions.deleteGoogleSuggest();
}
onAccepted: {
// eventually close VK
closeSoftwareInputPanel();
Functions.deleteGoogleSuggest();
}
onFocusChanged: {
if (!focus) closeSoftwareInputPanel();
if (!textInput.focus) {
Functions.deleteGoogleSuggest();
}
}
}


}<br />}</code>
}
}</code>

Revision as of 11:04, 25 February 2015


[toc align_right="yes" depth="2"]

Google Suggest

This snippet article shows how to make a Google Suggest component to be used in conjunction with a TextInput element.

This is what it looks like:

Google Suggest

Implementation

// GoogleSuggest.qml
import QtQuick 1.1

Rectangle {
 id: googleSuggest
 property string textToSugget
 property string dsParam
 property bool timeToDie: false
 property int rowHeight: 30
 width: parent.width
 height: rowHeight*listView.count
 anchors.top: parent.bottom
 anchors.left: parent.left
 anchors.topMargin: 5

signal itemChoosen(string suggestion);

onTimeToDieChanged: googleSuggest.destroy();

XmlListModel {
 id: xmlModel
 source: "http://suggestqueries.google.com/complete/search?output=toolbar"+
 (dsParam!=="" ? "&amp;ds="''dsParam : "")''
 "&amp;q="+textToSugget

query: "/toplevel/CompleteSuggestion"

XmlRole { name: "suggestion"; query: "suggestion/@data/string()" }
 XmlRole { name: "num_queries"; query: "num_queries/@int/string()" }
 }

ListView {
 id: listView
 anchors.fill: parent
 cacheBuffer: 10
 highlight: Rectangle { color: Qt.rgba(0,0,1,0.3); radius: 8; z: listView.z+10 }
 highlightFollowsCurrentItem: true
 interactive: false
 currentIndex: -1

 model: xmlModel
 delegate: Rectangle {
 id: resultRow

 width: parent.width
 height: rowHeight
 color: (index % 2 === 0 ? "#ffffff" : "#eeeeff")
 border.color: "#000000"
 border.width: 1
 radius: 8

 Text {
 id: result
 anchors.verticalCenter: parent.verticalCenter
 anchors.left: parent.left
 anchors.leftMargin: 5
 anchors.right: numberOfResults.left
 anchors.rightMargin: 10
 font.pixelSize: 15
 font.bold: true
 text: suggestion
 clip: true
 }
 Text {
 id: numberOfResults
 anchors.verticalCenter: parent.verticalCenter
 anchors.right: parent.right
 anchors.rightMargin: 5
 font.pixelSize: 12
 font.italic: true
 text: num_queries
 }

 }

 MouseArea {
 anchors.fill: listView
 onMouseYChanged: {
 listView.currentIndex = Math.floor((mouseY+rowHeight) / rowHeight)- 1;
 }
 onReleased: {
 googleSuggest.itemChoosen(xmlModel.get(listView.currentIndex).suggestion);
 timeToDie = true;
 }
 }
 }
}

The functions below are used to dynamically load and unload the component. For example to pop up it when the user has entered at last 3 chars or unload it when TextInput.onAccepted signal is rised

// gs.js
var componentGS;
var objectGS;

// dsParam = "yt" only for YouTube suggest else it is empty
function popupGoogleSuggest ( callerItem, textString, dsParam ) {

deleteGoogleSuggest();
 componentGS = Qt.createComponent("GoogleSuggest.qml");
 objectGS = componentGS.createObject(callerItem);
 if (objectGS === null) {
 console.log("Error creating GoogleSuggest.qml", componentGS.errorString());
 } else {
 objectGS.textToSugget = textString;
 objectGS.dsParam = dsParam;
 }
 return objectGS;
}

function deleteGoogleSuggest()
{
 if (componentGS[[Image:==null &amp;&amp; objectGS|==null &amp;&amp; objectGS]]==undefined) {
 objectGS.timeToDie = true;
 }
}

Usage

import QtQuick 1.1
import "gs.js" as Functions

Rectangle {
 id: main
 width: 360
 height: 360

Rectangle {
 id: textContainer
 x: 0; y:0; width: 250; height: 30
 color: "#ffffff"
 border.color: "#000000"
 border.width: 1

TextInput {
 id: textInput
 property GoogleSuggest gsComponent
 anchors.centerIn: parent
 selectByMouse: true
 text: "write here"

// Used to prevent predictive text to pop up while typing in a TextInput
 // component. Else onTextChanged event isn't rised:
 // https://bugreports.qt.nokia.com/browse/QTBUG-22298
 inputMethodHints: Qt.ImhNoPredictiveText

function googleSuggest_item_choosen(text)
 {
 textInput.text=text;
 }

Timer {
 id: timerPopupGS
 running: false
 interval: 300
 repeat: false
 triggeredOnStart: false
 onTriggered: {
 if (textInput.focus &amp;&amp; textInput.text !== "" &amp;&amp; textInput.text.length > 2) {
 // Pop up GoogleSuggest and catch itemChoosen signal
 textInput.gsComponent = Functions.popupGoogleSuggest(textContainer, textInput.text, "");
 textInput.gsComponent.itemChoosen.connect(textInput.googleSuggest_item_choosen);
 }
 if (textInput.text.length<3) Functions.deleteGoogleSuggest();
 }
 }

onTextChanged: {
 timerPopupGS.restart();
 if (text.length<3) Functions.deleteGoogleSuggest();
 }
 onAccepted: {
 // eventually close VK
 closeSoftwareInputPanel();
 Functions.deleteGoogleSuggest();
 }
 onFocusChanged: {
 if (!focus) closeSoftwareInputPanel();
 if (!textInput.focus) {
 Functions.deleteGoogleSuggest();
 }
 }
 }

}
}