//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
StackView {
id: stackView
anchors.fill: parent
initialItem: page1
}
Item {
id: page1
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 1")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Next")
onClicked: stackView.push(page2)
}
}
}
Item {
id: page2
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 2")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Back")
onClicked: stackView.pop()
}
}
}
}
Hi everybody, I have a problem with the placehoderText property of TextField.
If i do the sequence -> "Next" on page1 -> "Back" on page2 -> "Next" on page1, then page2 is actually displayed but the placeholderText of the TextField is not visible anymore.
Is this a Qt bug or am I doing something wrong?
StackView's goal is not that you tried to use ...
you should use SwipeView if you want prevent destroying items.
for your porpuse you can use a little customized SwipeView:
//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
SwipeView {
id: swipView
anchors.fill: parent
interactive: false;
property int stackIndex: -1;
function popIndex(){
return (--stackIndex);
}
function pushIndex(){
return (++stackIndex);
}
Component.onCompleted: {
if(swipView.contentChildren.length>0)
{
swipView.stackIndex=0;
}
}
Item {
id: page1
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 1")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Next")
onClicked: swipView.currentIndex=swipView.pushIndex();
}
}
}
Item {
id: page2
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 2")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Back")
onClicked: swipView.currentIndex=swipView.popIndex();
}
}
}
}
}
The issue is that by using Items that are parented to the Window like that, they don't get destroyed when they get popped. In the case of page2, that causes it to have a null parent and it resizes to 0x0. That then causes all of the other controls in that page to resize to 0 and that's a problem for the placeholder text (a situation they likely didn't expect).
If instead you use components, they will be sized correctly each time because they are created new at the right size and destroyed as needed.
//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
StackView {
id: stackView
anchors.fill: parent
initialItem: page1
}
Component {
id: page1
Item {
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 1")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Next")
onClicked: stackView.push(page2)
}
}
}
}
Component {
id: page2
Item {
Column {
height: parent.height * 0.2
width: parent.width * 0.5
anchors.centerIn: parent
spacing: height * 0.04
TextField {
height: parent.height * 0.48
width: parent.width
placeholderText: qsTr("Placeholder 2")
}
Button {
height: parent.height * 0.48
width: parent.width
text: qsTr("Back")
onClicked: stackView.pop()
}
}
}
}
}
Related
I created a control, and when I click it, the popup will be displayed on the left and right sides. Like:
There may be many other buttons on the main interface. I hope that when I click the button, the popup will not close, but when I click other external areas, the popup will close.
I thought of using closePolicy, but when I set Popup.NoAutoClose, no matter I click the outer area or the button control, the popup will not be closed. When I set Popup.CloseOnPressOutside, the popup will be closed.
So how to customize the closing behavior of the popup? Or is there any other custom control way to achieve such a requirement(may not be popup)?
Edit
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MenuSpinner{
id: menuId
x: 100
y: 50
}
Column{
x: 500
spacing: 10
Repeater{
model: 3
Button{
width: 100
height: 50
text: index
onPressed: {
console.log("pressed" + index)
}
}
}
}
}
MenuSpinner.qml
import QtQuick 2.0
import QtQuick.Controls 2.5
Rectangle{
width: 300
height: 50
property bool bTextClicked: false
onBTextClickedChanged: {
if(bTextClicked) popup.open()
else popup.close()
}
Rectangle{
width: 100
height: parent.height
x: rect1.width
border.color: "blue"
Text {
id: text
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
text: qsTr("value")
font.pixelSize: 16
}
MouseArea{
anchors.fill: parent
onClicked: {
bTextClicked = !bTextClicked
}
}
}
Popup {
id: popup
leftPadding: 0
rightPadding: 0
topPadding: 0
bottomPadding: 0
width: parent.width
height: parent.height
background: Rectangle {
anchors.fill: parent
color: "transparent"
//border.color: "black"
}
Rectangle{
id: rect1
width: 100
height: 50
Text {
text: qsTr("pop1")
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 16
}
color: "transparent"
border.color: "blue"
}
Rectangle{
id: rect2
x: parent.width - rect1.width
width: 100
height: 50
Text {
text: qsTr("pop2")
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 16
}
color: "transparent"
border.color: "blue"
}
onClosed: {
bTextClicked = false
console.log("close")
}
}
}
One way to do it is to catch mouse clicks on the window itself. Add a function or signal to MenuSpinner that can be called to close the popup. Any clicks to the other buttons should still work.
main.qml
Window {
MouseArea {
anchors.fill: parent
onClicked: {
menuId.closePopup();
}
}
MenuSpinner{
id: menuId
}
}
MenuSpinner.qml
Rectangle{
function closePopup() {
popup.close();
}
Popup {
id: popup
closePolicy: Popup.NoAutoClose
}
}
I am following this example here Using drag and drop with ListView to create an inventory UI. However, I have broken down the components into a main section, Listview and a droparea. Everything is working except, I cannot get the ReferenceError: listView is not defined. Please help
Main area
import QtQuick 2.15
import QtQuick.Controls 2.15
Page{
id:dragRect
Rectangle {
id: root
width: 400
height: 400
ListViewElem{
}
DropAreaElem{}
}
}
ListViewElem
import QtQuick 2.15
ListViewElem {
id: listView
width: parent.width / 2
height: parent.height
property int dragItemIndex: -1
model: ListModel {
Component.onCompleted: {
for (var i = 0; i < 10; ++i) {
append({value: i});
}
}
}
delegate: Item {
id: delegateItem
width: listView.width
height: 50
Rectangle {
id: dragRect2
width: listView.width
height: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
color: "salmon"
border.color: Qt.darker(color)
Text {
anchors.centerIn: parent
text: modelData
}
MouseArea {
id: mouseArea
anchors.fill: parent
drag.target: dragRect2
drag.onActiveChanged: {
if (mouseArea.drag.active) {
listView.dragItemIndex = index;
}
dragRect2.Drag.drop();
}
}
states: [
State {
when: dragRect2.Drag.active
ParentChange {
target: dragRect2
parent: root
}
AnchorChanges {
target: dragRect2
anchors.horizontalCenter: undefined
anchors.verticalCenter: undefined
}
}
]
Drag.active: mouseArea.drag.active
Drag.hotSpot.x: dragRect2.width / 2
Drag.hotSpot.y: dragRect2.height / 2
}
}
}
DropAreaElem
import QtQuick 2.15
Rectangle {
width: parent.width / 2
height: parent.height
anchors.right: parent.right
color: "#aaff0011"
DropArea {
id: dropArea
anchors.fill: parent
onDropped: {
listView.model.remove(listView.dragItemIndex);
listView.dragItemIndex = -1;
}
}
}
You should provide id to your components
import QtQuick 2.15
import QtQuick.Controls 2.15
Page{
id:dragRect
Rectangle {
id: root
width: 400
height: 400
ListViewElem{
id: listView
}
DropAreaElem{
id: dropArea
}
}
}
I was wondering either the following functionality is available in QML: I need for a child object (a text here) to always stay on top of other object, no matter the child/ parent connection. Here is a MWE:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle
{
id: rectMain;
anchors.centerIn: parent
width: parent.width
height: parent.height
color: "white"
Rectangle
{
id: rect1;
width: 200;
height: 200;
x: 100;
y: 100;
color: "red";
Text
{
id: theText;
text: qsTr("text");
anchors.centerIn: parent;
}
}
Rectangle
{
id: rect2;
width: 200;
height: 200;
x: 200;
y: 200;
color: "yellow";
}
}
}
It will show this window:
As you can see the "text" is covered with rec2, as it's a child of rect1, which was created prior to rect2. Is it possible for the text to be always on top of rect2 with current parent/ child connection?
This is the idea I expressed above. But I really can imagine for myself how that could be used. If you could define your real goals we will find another solution, of course.
import QtQuick 2.11
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
width: 400
height: 400
visible: true
title: "Example"
Item {
z: 1
Repeater {
id: rectGenerator
property bool loaded: false
Component.onCompleted: rectGenerator.loaded = true
model: 10
delegate: Rectangle {
width: 100
height: 100
color: Qt.rgba(Math.random(),Math.random(),Math.random(),0.8)
x: Math.round(Math.random() * 300)
y: Math.round(Math.random() * 300)
Drag.active: dragArea.drag.active
MouseArea {
id: dragArea
anchors.fill: parent
drag.target: parent
}
}
}
}
Loader {
z: 2
sourceComponent: Repeater {
model: rectGenerator.model
delegate: Text {
x: rectGenerator.itemAt(index).x
y: rectGenerator.itemAt(index).y
width: 100
height: 100
text: "item " + (index + 1)
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
active: rectGenerator.loaded
}
}
I'm working on a custom ComboBox item with two icons in both sides and a ComboBox in the middle. I want that when any of the icons are clicked the ComboBox dropdown menu opens, but i don't know how to do it.
Here is my code:
// ComboIcon.qml
Rectangle{
color: "#fff"
radius: 10
property alias iconSource: icon.source
property alias comboModel: combo.model
Row{
anchors.fill: parent
Item{
width: parent.width * 0.2
height: parent.height
Image{
id: icon
width: parent.width * 0.7
height: parent.height * 0.7
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
// onClicked: combo.??
}
}
ComboBox{
id: combo
width: parent.width * 0.65
height: parent.height
style: ComboBoxStyle{
background:Rectangle {
color: "#fff"
anchors.fill: parent
}
label: Text {
height: parent.height * 0.7
anchors.verticalCenter: parent.verticalCenter
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHLeft
color: "#6186da"
font.family: "SansSerif"
font.pointSize : 20
fontSizeMode: Text.Fit
text: control.currentText
}
}
}
Item{
width: parent.width * 0.15
height: parent.height
Image{
width: parent.width * 0.4
height: parent.height * 0.4
anchors.centerIn: parent
fillMode: Image.PreserveAspectFit
source: "../assets/images/combo_arrow.png"
}
MouseArea{
anchors.fill: parent
//onClicked: combo.??
}
}
}
}
I was thinking about using something like combo.clicked() or combo.focus = true, but it doesn't seem to work. Any help would be really appreciated,
thanks.
According to the sources, Combobox has an internal property __popup. Since it is internal, it is not guaranteed to be consistent among different versions of Qt. However, since controls 1 can be considered "done" it is quite unlikely that such a property is going to change in future releases.
Using __popup you can write something like that:
import QtQuick 2.2
import QtQuick.Window 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 300
height: 200
RowLayout {
anchors.fill: parent
Image {
fillMode: Image.PreserveAspectFit
Layout.preferredHeight: 64
source: "https://cdn1.iconfinder.com/data/icons/prettyoffice9/128/open-file.png"
MouseArea {
anchors.fill: parent
onClicked: combo.__popup.toggleShow() // <-- showing the popup here!
}
}
ComboBox {
id: combo
model: 3
}
}
}
Finally, a similar approach can be followed for ComboBox from controls 2 where popup is not internal and can be shown by simply changing its visible property, i.e.:
combo.popup.visible = true
The mobile devices move the elements up the the keyboard is called, but there are elements that stay in the same position when the device's keyboard appears like the images below.
How can I keep a Qml item fixed in the same position when device's keyboard appears?
I need that the Rectangle with id: principal stays fixed in the same position.
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
Window {
visible: true
property int larguraTela: 360
property int alturaTela: 640
width: larguraTela
height: alturaTela
maximumWidth: larguraTela
maximumHeight: alturaTela
minimumWidth: larguraTela
minimumHeight: alturaTela
title: "OverStatusBar"
Rectangle {
id: principal
width: parent.width
height: parent.height * 0.15
anchors.top: parent.top
color: "orange"
}
Rectangle {
width: parent.width
height: parent.height * 0.85
anchors.top: principal.bottom
clip: true
Rectangle{
id: retangulo1
width: parent.width
height: parent.height * 0.5
anchors.top: parent.top
color: "grey"
}
Rectangle {
id: retangulo2
width: parent.width
height: parent.height * 0.5
anchors.top: retangulo1.bottom
color: "lightgrey"
TextField {
id: campoTexto
width: parent.width * 0.7
height: parent.height * 0.20
anchors.centerIn: parent
inputMethodHints: Qt.ImhDigitsOnly
}
}
}
}
Ok, after a long research about this topic I have concluded that there is no possible solution, at least until now, that doesn't involve a lot of workaround programming to solve that using cross-platform programming. I have tried a bunch of cross-platform languages with no satisfactory solution that could be implemented. The languages I tried were:
QML
Appcelerator (Titanium)
PhoneGap (Cordova)
Native Script
React Native
My conclusion is that if I want do develop native look and feel apps that work as expected and without bugs, I need to do it using the native programming languages, even if I need to develop it twice in different languages. And that is what I am doing from now: XCode and Android Studio.
If someone would like to take a look at a piece of code to start doing it in QML just access the link clicking here:
I have something very hackish and begging for refinement but I think it is going into the right direction :
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
Window {
visible: true
property int larguraTela: 360
property int alturaTela: 640
width: larguraTela
height: alturaTela
maximumWidth: larguraTela
maximumHeight: alturaTela
minimumWidth: larguraTela
minimumHeight: alturaTela
title: "OverStatusBar"
Rectangle {
id: principal
width: parent.width
height: parent.height * 0.15
anchors.top: parent.top
color: "orange"
}
Timer{
id:resetKeyboard
interval: 500
onTriggered: {
Qt.inputMethod.hide();
Qt.inputMethod.show();
unlock.restart();
}
}
Timer{
id:unlock
interval: 500
onTriggered: {
flickable.updateSlideContent = true;
}
}
Flickable{
id:flickable
width: parent.width
height : slideContent ? parent.height * 0.5 : parent.height * 0.85
anchors.top: principal.bottom
clip: true
contentHeight: parent.height * 0.85
contentY : slideContent ? parent.height*0.35 : 0
property bool updateSlideContent : true
property bool slideContent : false
property bool keyboardVisible : Qt.inputMethod.visible
onKeyboardVisibleChanged: {
if (updateSlideContent) {
slideContent = keyboardVisible;
if (keyboardVisible)
{
updateSlideContent = false;
resetKeyboard.restart();
}
}
}
Rectangle {
anchors.fill: parent
Rectangle{
id: retangulo1
width: parent.width
height: parent.height * 0.5
anchors.top: parent.top
color: "grey"
}
Rectangle {
id: retangulo2
width: parent.width
height: parent.height * 0.5
anchors.top: retangulo1.bottom
color: "lightgrey"
TextField {
id: campoTexto
width: parent.width * 0.7
height: parent.height * 0.20
anchors.centerIn: parent
inputMethodHints: Qt.ImhDigitsOnly
}
}
}
}
}