Insert my own QML Button into a GridLayout - qt

I´m new to Qt and want to have some Buttons (made in a seperate .qml file with Rectangle...) in a GridLayout. Unfortunately it seems like it doesn´t positions the Buttons to different cells. Everything is in the first row/column. Is there a variable for the ID necessary, like an array? Or QML dynamic objects?
Here the Button:
Item {
property string textOnButton: "Test"
property int w: 130
property int h: 100
Rectangle{
id: emptyRectangle
width: parent.w
height: parent.h
Text {
text: parent.parent.textOnButton + parent.index
anchors.horizontalCenter: parent.parent.horizontalCenter
topPadding: 6
font.pixelSize: 30
}
}
}
And here the GridLayout where the Item should be placed in a few times:
GridLayout{
id:grid1
rows: 3
Layout.margins: 20
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
columnSpacing: 10
rowSpacing: 10
EmptyButton{
id: eins
textOnButton: "HELP"
}
EmptyButton{
id: zwei
textOnButton: "HELP"
}
}
Later I want to use different colors/text on the button and switching different lights for instance.
Thanks for any help!!

You need to set the width and height of the GridLayout and all of the other items
GridLayout{
width: 500
height: 500
id:grid1
rows: 3
Layout.margins: 20
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
columnSpacing: 10
rowSpacing: 10
EmptyButton{
id: eins
textOnButton: "HELP"
}
EmptyButton{
id: zwei
textOnButton: "HELP"
}
}

Related

Can not see full contents of dropdownlist qml

I use combobox in my application. I get trouble when the text of combobox is long. I can't see full content of some options. I want to see full content of text when drop down list open.
This is my code:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
anchors.fill: parent
anchors.topMargin: parent.height * 40 /100
anchors.bottomMargin: parent.height * 40 /100
anchors.leftMargin: parent.width * 40/100
anchors.rightMargin: parent.width * 40/100
ComboBox {
model: ["hello", " text not full text not full"]
font.pointSize: 20
}
}
}
When i click to combobox i just see "text not full text no..." instead of seeing "text not full text not full".
Set a width for the ComboBox that is large enough for the largest text you expect to have. For example:
ComboBox {
model: ["hello", " text not full text not full"]
font.pointSize: 20
width: 240
anchors.centerIn: parent
}
If you want a more exact value, try TextMetrics:
ComboBox {
id: comboBox
model: ["hello", " text not full text not full"]
font.pointSize: 20
width: textMetrics.width
anchors.centerIn: parent
}
// ...
TextMetrics {
id: textMetrics
text: "text not full text not full"
font: comboBox.font
}

Multiline text item is not clipped inside QML ListView

I have a QML page that with a GridLayout that contains the page title, ListView and close button:
GridLayout {
columns: 1
rows: 5
anchors.fill: parent
<page title item>....
ListView
{
spacing: 15
model: logModel
delegate: Item {
implicitWidth: parent.width
implicitHeight: grid.height
RowLayout
{
id: grid
spacing: 0
width: parent.width
height: commentLabel.implicitHeight
<icon>....
Label {
id: commentLabel
Layout.fillWidth: true
text: comment
wrapMode: Label.Wrap
}
}
}
ScrollIndicator.vertical: ScrollIndicator { }
}
<close button>...
}
When I scroll the list, the first and the last visible item in the list may go beyond the list bounder and intersect the page title or close button:
How to prevent this and make the items clipped?
EDIT1:
I tried to add
clip: true
to ListView, delegate Item, RowLayout and Label, but with no success. According to docs, ListView with clip property set to true should clip its content, should not it?
I found a similar question where clip property is the answer, but it is not clear why it does not work with my ListView.
My QT version is 5.13.2.
Set clip:true in the ListView component.
ListView
{
clip:true
}

QML Rectangle: Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead

i am making customized controls in the qt using the qml
tried with the previous versions of qt expect 5.11 it is working , i don't know what i need to change can any one please help
Rectangle{
width: parent.width - 30
height: 25
anchors.leftMargin: 15
anchors.left: parent.left
color: "transparent"
visible: (!auto_start)
RowLayout{
anchors.fill: parent
Text{
text: "Frame Rate:"
anchors.leftMargin: 10
anchors.left: parent.left
font.pointSize: 13
font.family: fontFamily.name
}
Text{
id: framesValueLabel
text: "0 fps"
font.bold: true
anchors.right: parent.right
anchors.rightMargin: 10
font.pointSize: 13
font.family: fontFamily.name
}
}
}
with previous versions it is working fine now the qt5.11 it is working but showing lot of warning errors in the console
You should not combine the anchors with the Layouts since they both fulfill similar tasks that is positioning items.
As the warning indicates:
Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.
Maybe in previous versions Qt was not so smart to detect these possible errors so I did not point you out.
So the solution is to use Layout.alignment, Layout.leftMargin and Layout.rightMargin:
Rectangle{
width: parent.width - 30
height: 25
anchors.leftMargin: 15
anchors.left: parent.left
color: "blue"
visible: (!auto_start)
RowLayout{
anchors.fill: parent
Text{
text: "Frame Rate:"
Layout.leftMargin: 10
Layout.alignment : Qt.AlignLeft
font.pointSize: 13
font.family: fontFamily.name
}
Text{
id: framesValueLabel
text: "0 fps"
font.bold: true
Layout.alignment : Qt.AlignRight
Layout.rightMargin: 10
font.pointSize: 13
font.family: fontFamily.name
}
}
}

Alignment of item of a rowlayout which is in a Groupbox

I am developing a UI where I have made two buttons and two labeltextfield. The problem is that the alignment I cant do of the inner item in rowlayout. And the buttons are also not anchored to the base of the textfield. I have already set the anchor.bottom to textfield.bottom.
I set the Layout.Alignment = Qt.AlignBottom but isnt working
GroupBox{
id: gb
RowLayout{
id:rl
Button{
id: btn1
text:"Btn1"
Layout.preferredHeight: 30
Layout.preferredWidth: 150
anchors.bottom: tlfield.bottom
}
Button{
id: btn2
text:"Btn2"
Layout.preferredHeight: 30
Layout.preferredWidth: 150
anchors.bottom: tlfield.bottom
}
LabeledTextFieldVertical
{
id: tlfield
fieldLabel.text: "text"
fieldText.text: check
fieldText.maximumLength: 15
Layout.preferredWidth: 130
fieldText.height:30
Layout.alignment: Qt.AlignBottom
shouldFillWidth: false
}
}
}
Layouts and anchoring items are two different mechanisms, see Qt Quick Layouts and Positioning with Anchors for further reading.

Mask a long/wide user input in QML

I am developing a Qt program using QML in which a user has to enter a variable-sized input (up to 50 chars). Since the program window is not big enough I cannot accommodate a 50 char-wide input rectangle. I would like the input box ("inputNameField" below) to act as a mask over the text so that the characters that are out of the input box are not visible. Here is my base code:
Rectangle
{
id: inputNameBox
onVisibleChanged: if (visible) textNameInput.forceActiveFocus()
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
color: 'grey'
radius: 5
height: parent.height/8
width: parent.width/4
TextInput
{
id: textNameInput
autoScroll: true
anchors.margins: inputNameBox.radius
anchors.fill: inputNameBox
font.pixelSize: inputNameBox.height/2
maximumLength: 50
horizontalAlignment: TextInput.AlignHCenter
verticalAlignment: TextInput.AlignVCenter
}
}
I have tried using the inputNameField as an OpacityMask over the textNameInput to no avail.
EDIT: duplicate of Custom TextEdit, how to hide TextInput when it goes wide

Resources