Cannot override attributes/implement handlers in Qt QML form implementation - qt

I'm trying to learn Qt and wrap my head around the UI forms. The doc states that I should be able to implement headers in the implementation file. However, I cannot do this (nor override any attributes assigned in the designer).
Here's my BasicWindowForm.ui.qml:
import QtQuick 2.4
import QtQuick.Controls 2.12
Item {
id: element
width: 400
height: 400
property alias quitButton: quitButton
Rectangle {
id: rectangle
x: 100
width: 200
height: 86
color: "#ffffff"
radius: 10
border.width: 3
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 25
}
Row {
id: row
y: 222
height: 100
layoutDirection: Qt.RightToLeft
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
Button {
id: quitButton
text: qsTr("Quit")
}
}
}
/*##^##
Designer {
D{i:1;anchors_y:20}D{i:2;anchors_width:200;anchors_x:21}
}
##^##*/
Here's how it looks in the design mode:
Here's BasicWindow.qml:
import QtQuick 2.4
BasicWindowForm {
quitButton.text: "Lorem"
quitButton.onClicked: Qt.quit()
}
And main.qml:
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
id: applicationWindow
visible: true
width: 640
height: 480
title: qsTr("Play QtQuick")
BasicWindowForm {
anchors.fill: parent
id: winform
}
}
If I move the handler to the main.qml file, it works. But, as it is here, clicking on the button does nothing, and it still reads "quit".
What am I doing wrong?

Related

Scroll Bar is not working in ScrollView qml

I'm trying to use a scrollbar inside a scrollview. The scrollbar shows up and I can interact with it (hover/pressed), but it doesn't move, and I can't understand why. I wrote my code by following the official documentation and online examples.
Here's the code:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15
Window {
width: 740
height: 580
visible: true
color: "#00000000"
title: qsTr("Hello World")
Rectangle {
id: rectangle
color: "#40405f"
anchors.fill: parent
Button {
id: button
text: qsTr("Menu")
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.leftMargin: 10
anchors.bottomMargin: 466
anchors.topMargin: 74
onClicked: animationMenu.running = true
}
ScrollView {
id: scrollView
width: 0
anchors.left: button.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.leftMargin: 10
anchors.bottomMargin: 10
anchors.topMargin: 10
clip: true
Rectangle {
id: rectangle1
color: "#00000000"
border.color: "#00000000"
border.width: 0
anchors.fill: parent
PropertyAnimation {
id: animationMenu
target: scrollView
property: "width"
to: if(scrollView.width == 0) return 240; else return 0
duration: 800
easing.type: Easing.InOutQuint
}
Column {
id: columnMenu
width: 0
anchors.fill: parent
spacing: 10
Button {
id: button1
text: qsTr("Button")
}
Button {
id: button2
text: qsTr("Button")
}
Button {
id: button3
text: qsTr("Button")
}
Button {
id: button4
text: qsTr("Button")
}
}
}
ScrollBar {
id: vbar
hoverEnabled: true
orientation: Qt.Vertical
size: scrollView.height / rectangle1.height
anchors.top: parent.top
anchors.right: parent.right
anchors.bottom: parent.bottom
wheelEnabled: true
pressed: true
active: true
}
}
}
}
Ok, so I edited the code to a smaller version so that it can be run.
Some advices:
Use anchors or Layouts. Do not use fixed values or some kind of treats, no matter if it works. The long term value of your code will be bad.
You should read carefully the (ScrollView documentatio). Also the Size section and the Touch and Mouse Interaction Section.
I am able to modify your example without the animation.
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.15
Window {
width: 740
height: 580
visible: true
color: "#00000000"
title: qsTr("Hello World")
Rectangle {
id: rectangle
color: "#40405f"
anchors.fill: parent
RowLayout{
anchors.fill: parent
Button {
id: button
text: qsTr("Menu")
width: 100
height: 50
}
ScrollView {
id: scrollView
Layout.fillWidth: true
Layout.fillHeight: true
RowLayout{
implicitHeight: 2000
implicitWidth: 2000
Column {
id: columnMenu
width: 0
anchors.fill: parent
spacing: 10
Repeater{
model: 50
delegate: Button {
text: qsTr("Button")
}
}
}
}
}
}
}
}

How to load and display QML file

I have two QML pages main.qml and Kos.qml,
what i want is when a button in main.qml clicked it load Kos.qml in the screen.
i did try using loader, but it is not working
import QtQuick 2.5
import QtQuick.Controls 2.5
ApplicationWindow {
id: applicationWindow
width: 640
height: 480
color: "#fc4343"
title: qsTr("Tabs")
visible: true
// HALAMAN UTAMA
Page {
anchors.centerIn: parent
anchors.fill: parent
id: page
enabled: true
Loader{
id: kos
active: true
anchors.fill: parent
}
Button {
id: button
x: 198
width: 87
height: 30
text: qsTr("Search")
font.bold: true
anchors.top: borderImage.bottom
anchors.topMargin: 198
anchors.right: toolSeparator.left
anchors.rightMargin: 28
MouseArea{
anchors.fill: parent
onClicked:{
kos.source = "Kos.qml";
}
}
background: Rectangle {
id: background
color: "#ef3644"
}
contentItem: Text {
id: textItem
font: control.font
opacity: enabled ? 1.0 : 0.3
color: "white"
text: "Search"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
}
}
Kos.qml i use PageBackground as a background
import QtQuick 2.4
PageBackground {
id: kos
width: 640
height: 480
Text {
id: element
x: 6
y: 20
width: 24
height: 32
color: "#ffffff"
text: qsTr("<")
font.strikeout: false
styleColor: "#ffffff"
font.underline: false
font.italic: false
font.bold: true
font.pixelSize: 25
MouseArea {
id: mouseArea
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent
}
}
}
did i messed up somewhere?
I tried to run your code. Since I am not aware of what your PageBackground is, I changed that to Rectangle. That works for me. Try to start from the minimal code then add your styles and functions on top of it. Try with below minimal code. Keep both main.qml & Kos.qml in the same directory and ensure both files added to qml resources.
main.qml
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow {
id: applicationWindow
width: 640
height: 480
visible: true
Text {
anchors.centerIn: parent
text: "App window"
}
Loader {
id: loaderId
anchors.fill: parent
source: ""
}
Button {
text: "click me!!"
width: parent.width
anchors.bottom: parent.bottom
onClicked: {
if(loaderId.source == "")
loaderId.source = "Kos.qml"
else
loaderId.source = ""
}
}
}
Kos.qml
import QtQuick 2.9
Rectangle {
id: kos
width: 640
height: 480
color: "grey"
Text {
anchors.centerIn: parent
text: "<b>Loaded Item</b>"
color: "white"
}
}

Adding margins to QML Layout

I'm trying to add padding/margins to my layout so my controls to appear so close to the border of the window. When i set the anchor margins it doesn't appear to actually affect anything.
This is the qml file displayed for the Settings Tab.
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
Page {
id: control
title: qsTr("Settings")
objectName: "SettingsView"
ColumnLayout {
spacing: 20
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
Switch {
text: qsTr("Theme")
checked: root.Material.theme === Material.Dark
Layout.fillWidth: true
LayoutMirroring.enabled: true
onClicked: {
root.Material.theme = checked ? Material.Dark : Material.Ligth
//Settings.currentTheme = root.Material.theme
}
}
}
}
This is the main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
ApplicationWindow {
id: root
visible: true
width: 300
height: 500
// Theme
Material.theme: Material.Dark
Material.accent: "#4096DD"
Material.primary: "#4096DD"
// Controls
header: TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
TabButton {
//text: qsTr("Home")
icon.source: "qrc:/Images/home.svg"
}
TabButton {
//text: qsTr("Settings")
icon.source: "qrc:/Images/settings.svg"
}
}
SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: tabBar.currentIndex
Page1 {
}
SettingsView {
}
}
}
If an Item is affected by a Layout then you must use Layout.margins if you want to set all the margins to the same value, but if you want to set a different margin in each direction you must use Layout.leftMargin, Layout.topMargin, Layout.rightMargin and Layout .bottomMargin, in your case:
ColumnLayout {
spacing: 20
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
Switch {
Layout.leftMargin: 20
Layout.topMargin: 20
Layout.rightMargin: 20
// ...
Update:
Then set the margin at the anchor:
ColumnLayout {
spacing: 20
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.leftMargin: 20
anchors.topMargin: 20
anchors.rightMargin: 20
Switch {
text: qsTr("Theme")
// ...

Margins and anchors not working good in Qt, QML

I'm new at QML and I have a very confusing situation.
So, this is my main.qml file:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
Window {
id:rootWin
visible: true
width: 350
height: 330
ConnectBox
{
id:boxConnect
MouseArea
{
id: connectMouse
hoverEnabled: true
anchors.fill: boxConnect
}
}
Rectangle {
id: randomRec
width: parent.width/2
height: parent.height/6
// x: 50
anchors.top: boxConnect.bottom
// anchors.horizontalCenter: parent
anchors.topMargin: 10
border.color: "dimgray"
border.width: 5
radius: 14
}
}
ConnectBox.qml:
import QtQuick 2.0
Rectangle {
id: connectRec
width: parent.width/2
height: parent.height/6
anchors.centerIn: parent
border.color: "dimgray"
border.width: 5
radius: 14
}
I want it to be randomRec below boxConnect, so it does but it is all left and it only moves when I put for example x: 50 but its not convenient for every size of the window.
First off: I'd advise to put the anchors that are related to each other all on the same file.
But to get the randomRec to center below the connectBox you should use the following anchors:
Rectangle {
id: rect1
width: 100
height: 200
color: "red"
anchors.centerIn: parent
}
Rectangle {
id: rect2
width: 75
height: 50
color: "yellow"
anchors.top: rect1.bottom
anchors.horizontalCenter: rect1.horizontalCenter
}
So I guess you where almost there, with the horizontalCenter: parent line, which should have .horizontalCenter as well. However, I anchored it on rect1 since that is what you want (let's say you might want to move the boxConnect in the future... you would have to find all of the references)

Application Window and Dialog Issue in QML

I am not able to click on the VirtualKeyboard if the TextField is in the dialog and Application Window is the base class.
Following is the code:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.VirtualKeyboard 2.2
ApplicationWindow {
id:appwindow
visible: true
width: 600
height: 500
title: qsTr("Test")
Button{
id:button
text:qsTr("Open")
onClicked:{
dialog.visible=true
dialog.open()
}
}
Dialog{
id:dialog
width:200
height:300
visible:false
TextField {
id: textfield
color: "#2B2C2E"
}
}
InputPanel {
id: inputPanel
z: 89
anchors.bottom:parent.bottom
anchors.left: parent.left
anchors.right: parent.right
visible: Qt.inputMethod.visible
}
}
There will be no issues if I change ApplicationWindow to Window, Is that a QT Bug in v5.9.1?
ApplicationWindow offers a nice extra layer overlay to which you may reparent everything that shall be above the rest of the content - just the right place for your VirtualKeyboard
InputPanel {
id: inputPanel
parent: ApplicationWindow.overlay // <-- This will do the trick
anchors.bottom:parent.bottom
anchors.left: parent.left
anchors.right: parent.right
visible: Qt.inputMethod.visible
}

Resources