I want to create simple TextArea element in QML and I try this code. but when write in Textarea , text's fall out of border.
Are you have simple TextArea or can you help me to improve this code:
FocusScope {
id: focusScope
width: 400; height: 50
property int fontSize: focusScope.height -30
property color textColor: "black"
property string placeHolder: "Type something..."
property string inputExpression: ".*"
property bool isUserInTheMiddleOfEntringText: false
Rectangle {
width: parent.width
height: parent.height
border.color: 'steelblue'
color: focus?'red':'#AAAAAA'
border.width: 3
radius: 0
MouseArea {
anchors.fill: parent
onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
}
}
}
Text {
id: typeSomething
anchors.fill: parent; anchors.rightMargin: 8
verticalAlignment: Text.AlignVCenter
text: placeHolder
color: "gray"
font.italic: true
font.pointSize: fontSize
MouseArea {
anchors.fill: parent
onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
}
}
}
MouseArea {
anchors.fill: parent
onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
}
}
TextEdit {
id: textInput
anchors { right: parent.right; rightMargin: 8; left: clear.left; leftMargin: 8; verticalCenter: parent.verticalCenter }
focus: true
selectByMouse: true
font.pointSize: fontSize
wrapMode: TextEdit.WordWrap
color: textColor
}
Text {
id: clear
text: "\u2717" // 'x'//"\u2713"
color: 'steelblue'
font.pointSize: 25
opacity: 0
anchors { left: parent.left; leftMargin: 8; verticalCenter: parent.verticalCenter }
MouseArea {
anchors.fill: parent
onClicked: { textInput.text = ''; focusScope.focus = true; textInput.openSoftwareInputPanel(); }
}
}
states: State {
name: "hasText"; when: textInput.text != ''
PropertyChanges { target: typeSomething; opacity: 0 }
PropertyChanges { target: clear; opacity: 1 }
}
transitions: [
Transition {
from: ""; to: "hasText"
NumberAnimation { exclude: typeSomething; properties: "opacity" }
},
Transition {
from: "hasText"; to: ""
NumberAnimation { properties: "opacity" }
}
]
}
thank's for help
This code should do what you want :
import QtQuick 2.0
FocusScope {
id: focusScope;
width: 400;
height: textInput.paintedHeight + (2 * textInput.anchors.topMargin);
property alias value : textInput.text;
property alias fontSize : textInput.font.pointSize;
property alias textColor : textInput.color;
property alias placeHolder : typeSomething.text;
Rectangle {
id: background;
anchors.fill: parent;
color: "#AAAAAA";
radius: 5;
antialiasing: true;
border {
width: 3;
color: (focusScope.activeFocus ? "red" : "steelblue");
}
}
TextEdit {
id: textInput;
focus: true
selectByMouse: true
font.pointSize: 20;
wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere;
color: "black";
anchors {
top: parent.top;
topMargin: 10;
left: parent.left;
leftMargin: 10;
right: parent.right;
rightMargin: 10;
}
}
Text {
id: typeSomething;
text: "Type something...";
color: "gray";
opacity: (value === "" ? 1.0 : 0.0);
font {
italic: true
pointSize: fontSize;
}
anchors {
left: parent.left;
right: parent.right;
leftMargin: 10;
rightMargin: 10;
verticalCenter: parent.verticalCenter;
}
Behavior on opacity { NumberAnimation { duration: 250; } }
}
MouseArea {
visible: (!focusScope.activeFocus);
anchors.fill: parent
onClicked: { textInput.forceActiveFocus(); }
}
Text {
id: clear;
text: "\u2717" // 'x' //"\u2713"
color: 'steelblue';
font.pixelSize: 30;
opacity: (value !== "" ? 1.0 : 0.0);
anchors {
right: parent.right;
bottom: parent.bottom;
margins: 5;
}
Behavior on opacity { NumberAnimation { duration: 250; } }
MouseArea {
anchors.fill: parent
onClicked: {
value = "";
focusScope.focus = false;
}
}
}
}
I also fixed many things in you code, like multiple MouseArea for the same use, binding loops, bad anchors/alignements, etc...
Related
I have a Button object defined in Button.qml file, and another qml file Page.qml uses the Button object defined in Button.qml. How do I activate the keyboard input so that I can use keyboard to navigate the buttons in Page.qml. for example enter key, space key and etc.
I've tried using "focus: true", "Keys.onPressed:{}" and "onVisibleChanged: if (visible) sendBbkButton.forceActiveFocus()" both in Button.qml and Page.qml. But it does not activate the focus and keyboard for the two buttons in Page.qml.
Thanks!
Page.qml:
Rectangle {
id: bp
// some code
Button {
id: aButton
text: qsTr("Abort test1")
tooltipText: qsTr("test1")
onClicked: {
// some coding
}
anchors { right: parent.left; bottom: parent.bottom; margins: 10 }
height: contentHeight + 20
width: contentWidth + 40
}
Button {
id: bButton
text: qsTr("Abort test2")
tooltipText: qsTr("test2")
onClicked: {
// some coding
}
anchors { right: aButton.left; bottom: parent.bottom; margins: 10 }
height: contentHeight + 20
width: contentWidth + 40
}
// some code
}
Button.qml:
BorderImage {
id: button
property alias text: label.text
property alias contentWidth: label.contentWidth
property alias contentHeight: label.contentHeight
property alias buttonColor: shade.color
property string tooltipText: ""
signal clicked()
source: Style.buttonBorderImage
border { left: 10; top: 15; right: 10; bottom: 10 }
Highlight { }
width: Math.max( sourceSize.width, label.contentWidth * 1.2)
Rectangle {
id: shade
anchors.fill: parent
radius: 10
}
Rectangle {
id: focusBorder
color: "transparent"
border.color: button.activeFocus ? Style.iRobotGreen : "transparent"
anchors { fill: parent; margins: -1 }
radius: 10
}
Layout.preferredWidth: Math.max( label.implicitWidth + button.border.left
+ button.border.right,
button.implicitWidth )
Text {
id: label
anchors.centerIn: parent
font: Style.refFont1.font
}
MouseArea {
id: mouse
anchors.fill: parent
enabled: true
onClicked: button.clicked()
hoverEnabled: button.tooltipText.length > 0
onEntered: {
if (button.tooltipText.length > 0)
tooltip.show(mouseX + 16, mouseY + 16)
}
onExited: {
tooltip.hide()
}
}
ToolTip {
id: tooltip
enabled: true
text: button.tooltipText
}
}
I have a Combobox (Control 2.0). I write my own combobox like
Rectangle
{
border.width: 1
border.color: "lightsteelblue"
height:dp(40)
width: parent.width
ComboBox {
id:tmCombo
model:combotm.datalist
textRole: "value"
anchors.fill: parent
currentIndex:-1;
contentItem: Text {
leftPadding: 0
rightPadding: tmCombo.indicator.width + tmCombo.spacing
text:tmCombo.currentIndex==-1 ? "":tmCombo.model[tmCombo.currentIndex].value
font: tmCombo.font
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
onCurrentIndexChanged: {
if(currentIndex!=-1) {
var sqlid=model[currentIndex].sqlid;
combotm.getsqlid(sqlid,1,Query.SelectSubParam,Query.Subq,"TMC",1);
TaskResult.taskresult.HatBilgisi_TM=sqlid;
tmsCombo.enabled=true;
}
else {
tmsCombo.enabled=false;
}
tmsCombo.currentIndex=-1;
}
}
}
My problem is that when Combobox first open, half of popup is transparent. Then I close and open combobox again. Everthing is OK. I am working in android platform.
SOLUTION:
I have added import QtQuick.Templates 2.0 as T and chance Popup with T.Popup . That's work
Rigth code is : Combobox with filter input :) I have binded model from C++ side with Q_Property . dp function is my global function in main.qml which is fixed pixel for any device.
ComboBox {
id:trCombo
model:combotr.datalist
textRole: "value"
anchors.fill: parent
currentIndex:-1;
contentItem: Text {
leftPadding: 0
rightPadding: trCombo.indicator.width + trCombo.spacing
text:trCombo.currentIndex==-1 ? "":trCombo.model[trCombo.currentIndex].value
font: trCombo.font
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
popup: T.Popup {
id:mpopup
y: trCombo.height - (trCombo.visualFocus ? 0 : 1)
width: trCombo.width
implicitHeight: listview.contentHeight
topMargin: 6
bottomMargin: 6
// focus: true
closePolicy: Popup.NoAutoClose
contentItem: Item {
Column
{
anchors.fill: parent
spacing: 5
TextField
{
placeholderText: "arama yapın"
width: trCombo.width
height: dp(35)
// color: "red"
focus:true
inputMethodHints: Qt.ImhNoPredictiveText;
onTextChanged:{
//console.log("degisiyor");
process.filtertr(text);
}
onAccepted:{
// console.log("Tasarım Bitti");
isfinished(true);
// text="";
}
}
ListView {
id: listview
clip: true
model: trCombo.popup.visible ? trCombo.delegateModel : null
currentIndex: trCombo.highlightedIndex
width: trCombo.width
height:dp(500)
Rectangle {
z: 10
parent: listview
width: listview.width
height: listview.height
color: "transparent"
border.color: "#bdbebf"
layer.smooth: true
}
ScrollIndicator.vertical: ScrollIndicator { }
}
}
}
background: Rectangle { }
onClosed: {
if(!flag)
{
mpopup.open();
}
else
flag=false;
}
}
delegate: ItemDelegate {
width: trCombo.width
text: trCombo.textRole ? (Array.isArray(trCombo.model) ? modelData[trCombo.textRole] : model[trCombo.textRole]) : modelData
font.weight: trCombo.currentIndex === index ? Font.DemiBold : Font.Normal
highlighted: trCombo.highlightedIndex == index
onClicked: {
isfinished(true);
}
}
onCurrentIndexChanged: {
if(currentIndex!=-1)
{
var sqlid=model[currentIndex].sqlid;
combotr.getsqlid(sqlid,1,Query.SelectSubParam,Query.Subq,"TRC",1);
TaskResult.taskresult.HatBilgisi_TR=sqlid ;
trsCombo.enabled=true;
}
else
trsCombo.enabled=false;
trsCombo.currentIndex=-1;
}
}
I have a component named Tile in Tile.qml, which I want to create by a Repeater. Tile.qml is as follows:
import QtQuick 2.0
Rectangle {
id: tile
property string tileLabel: label.text
property int tileSize: height
width: 50
height: tileSize
color: "green"
border.color: Qt.lighter(color)
anchors.bottom: parent.bottom
Text {
id: label
color: "white";
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.bottom
text: tileLabel
}
}
And my main.qml is as follows:
import QtQuick 2.0
Rectangle {
id: root
width: 552; height: 300
color: "#3C3C3C"
border.color: Qt.lighter(color)
Row {
id: tilesRow
anchors.margins: 8
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
spacing: 4
Repeater {
id: repeater
model: 10
delegate: Tile {
tileSize: Math.random() * 100 + 1
tileLabel: tileSize
}
}
}
Rectangle {
id: button
width: 100
height: 30
color: "gray"
focus: true
Text {
anchors.centerIn: parent
color: "white"
text: "Button"
}
MouseArea {
hoverEnabled: true
anchors.fill: parent
onEntered: { button.color = Qt.lighter("blue")}
onExited: { button.color = "gray" }
onPressed: { button.color = "blue" }
onReleased: { button.color = Qt.lighter("blue") }
onClicked: func()
}
}
}
I need to sort the tiles when the button is clicked so that the tiles are in ascending order by their labels. I can access the labels of the tiles using repeater.itemAt(i).tileSize. How can I animate the movement of tiles as they are moved/swapped?
Small example:
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
width: 800
height: 600
Row {
anchors.centerIn: parent
property var word: ['H','e','l','l','o','!']
id: row
Repeater {
id: repeater
model: row.word.length
delegate: Rectangle {
id: delegate;
width: 100
height: 100
property int pos
color: Qt.rgba(Math.random(),Math.random(),Math.random(),1);
Text {
anchors.centerIn: parent
font.pixelSize: 36
color: "white"
text: row.word[index]
}
Behavior on x {
ParallelAnimation {
PropertyAnimation {
duration: 500
easing.type: Easing.InOutBack
}
SequentialAnimation {
PropertyAnimation {
target: delegate
property: "y"
from: 0
to: delegate.pos == 1 ? 20 : -20
duration: 250
}
PropertyAnimation {
target: delegate
property: "y"
from: delegate.pos == 1 ? 20 : -20
to: 0
duration: 250
}
}
}
}
Behavior on rotation {
RotationAnimation {
direction: RotationAnimation.Clockwise
duration: 300
}
}
}
}
}
Timer {
interval: 1000
running: true
repeat: true
onTriggered: {
var element1 = repeater.itemAt(Math.round(Math.random() * (repeater.count - 1)));
var element2 = repeater.itemAt(Math.round(Math.random() * (repeater.count - 1)));
if(element1 === element2) {
element1.rotation = element1.rotation + 90;
} else {
element1.pos = 1;
element2.pos = 2;
var temp = element1.x;
element1.x = element2.x;
element2.x = temp;
}
}
}
}
Let me start by saying that I am pretty new to QML.
I have a ListView (with model and delegate), it works fine in my model but I would like to change the color (currently color: skin.gray) of the selected item to something else when the item is the currentIndex-item.
ListView {
id: menuBody_listview
width: parent.width
height: parent.height
currentIndex: 0
clip: true
highlight: highlighter
highlightFollowsCurrentItem: true
Behavior on opacity {
NumberAnimation { property: "opacity"; duration: 300; easing.type: Easing.InOutQuad }
}
anchors {
top: menuHeader_listview.bottom
bottom: parent.bottom
}
model: ListModel {
ListElement {
itemIconLeft: 'images/icons/menu/pause.png'
itemText: "Cancel"
itemIconRight: 'images/icons/menu/take-me-home.png'
}
ListElement {
itemIconLeft: 'images/icons/menu/pause.png'
itemText: "Mute"
itemIconRight: 'images/nill.png'
}
ListElement {
itemIconLeft: 'images/icons/menu/repeat.png'
itemText: "Repeate"
itemIconRight: 'images/nill.png'
}
}
delegate: MenuBodyItem {
width: menuBody_listview.width
anchors.horizontalCenter: parent.horizontalCenter
iconLeft: itemIconLeft
message: itemText
iconRight: itemIconRight
}
}
Following is the code for the item which is being populated, ManuBodyItem.qml.
Item {
width: 100
height: 50
property alias iconLeft: menuitem_icon_start.source
property alias message: menuitem_text.text
property alias iconRight: menuitem_icon_end.source
RowLayout {
spacing: 20
anchors.fill: parent
Image {
id: menuitem_icon_start
fillMode: Image.PreserveAspectCrop
anchors {
left: parent.left
verticalCenter: parent.verticalCenter
}
}
Text {
id: menuitem_text
anchors {
left: menuitem_icon_start.right
verticalCenter: parent.verticalCenter
verticalCenterOffset: -2
leftMargin: 20
}
color: skin.gray
font {
family: "TBD"
}
}
Image {
id: menuitem_icon_end
fillMode: Image.PreserveAspectCrop
source: iconRight
anchors {
right: parent.right
verticalCenter: parent.verticalCenter
}
}
}
}
Use ListView's isCurrentItem attached property:
Text {
id: menuitem_text
anchors {
left: menuitem_icon_start.right
verticalCenter: parent.verticalCenter
verticalCenterOffset: -2
leftMargin: 20
}
color: itemDelegate.ListView.isCurrentItem ? "red" : skin.gray
font {
family: "TBD"
}
}
Note that you have to give your root delegate item an ID in order to qualify the expression above:
Item {
id: itemDelegate
RowLayout {
// ...
}
// ...
}
You can see the same approach used in the example I linked to.
From your example:
color: skin.gray is used for the Text element which will change the color of the text and not it's background viz. i understood you want.
You can use a Rectangle element here which can act as a background component to set the background color.
So instead of Item root element in the delegate you can use Rectangle. So MenuBodyItem.qml will look as
Rectangle {
width: 100
height: 50
...
}
Now to set background color to the Rectangle if it is current one you can use ListView.isCurrentItem to check.
So,
Rectangle {
color: ListView.isCurrentItem ? "cyan" : "lightblue"
width: 100
height: 50
}
and now finally you will have to set the clicked item as the current one which can be done in the MouseArea of the Delegate Item
delegate: MenuBodyItem {
width: menuBody_listview.width
anchors.horizontalCenter: parent.horizontalCenter
iconLeft: itemIconLeft
message: itemText
iconRight: itemIconRight
MouseArea {
anchors.fill: parent
onClicked: menuBody_listview.currentIndex = index
}
}
I am creating a custom combobox model in which i need to assign a height of a dropDown list in responsive value. It is on length of items at present so unable to reduce or increase its height as per screen resolution. Here is my code :
Rectangle {
width:150;
height: 60;
Rectangle {
id:comboBox
property variant items: ["Item 1", "Item 2", "Item 3"]
property alias selectedItem: chosenItemText.text;
property alias selectedIndex: listView.currentIndex;
signal comboClicked;
width: 100;
height: 30;
z: 100;
smooth:true;
Rectangle {
id:chosenItem
radius:4;
width:parent.width;
height:comboBox.height;
color: "lightsteelblue"
smooth:true;
Text {
anchors.top: parent.top;
anchors.left: parent.left;
anchors.margins: 8;
id:chosenItemText
text:comboBox.items[0];
font.family: "Arial"
font.pointSize: 14;
smooth:true
}
MouseArea {
anchors.fill: parent;
onClicked: {
comboBox.state = comboBox.state==="dropDown"?"":"dropDown"
}
}
}
Rectangle {
id:dropDown
width:comboBox.width;
height:0;
clip:true;
radius:4;
anchors.top: chosenItem.bottom;
anchors.margins: 2;
color: "lightgray"
ListView {
id:listView
height:500;
model: comboBox.items
currentIndex: 0
delegate: Item{
width:comboBox.width;
height: comboBox.height;
Text {
text: modelData
anchors.top: parent.top;
anchors.left: parent.left;
anchors.margins: 5;
}
MouseArea {
anchors.fill: parent;
onClicked: {
comboBox.state = ""
var prevSelection = chosenItemText.text
chosenItemText.text = modelData
if(chosenItemText.text != prevSelection){
comboBox.comboClicked();
}
listView.currentIndex = index;
}
}
}
}
}
Component {
id: highlight
Rectangle {
width:comboBox.width;
height:comboBox.height;
color: "red";
radius: 4
}
}
states: State {
name: "dropDown";
PropertyChanges { target: dropDown; height:30*comboBox.items.length }
}
transitions: Transition {
NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 }
}
}
}
Please suggest me some solution.