QML: Move ScrollView to bottom when its height is changed? - qt

I am trying to make a message composer widget. I want to scroll to the bottom of the ScrollView when its height is changed so that the user can keep up with what they are writing. How can I achieve this sort of functionality?
Here is my code for the ScrollView I am using:
ScrollView {
id: scrollView
anchors.left: parent.left
anchors.right: clearTextBtn.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.rightMargin: 10
clip: true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
hoverEnabled: true
onHoveredChanged: {
if (hovered) {
borderWidth = 2
cursorShape = Qt.IBeamCursor
} else {
borderWidth = focus ? 2 : 0
cursorShape = Qt.ArrowCursor
}
}
onFocusChanged: {
if (focus) {
borderWidth = 2
} else {
borderWidth = 0
}
}
TextEdit {
id: textEdit
width: scrollView.width
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignTop
anchors.bottomMargin: 0
anchors.topMargin: 0
clip: true
color: textColor
anchors.left: parent.left
wrapMode: Text.WordWrap
anchors.leftMargin: 0
padding: 10
selectByMouse: true
Label {
id: placeholderTxt
text: qsTr("Compose Message...")
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
verticalAlignment: Text.AlignTop
anchors.topMargin: 10
anchors.rightMargin: 223
anchors.leftMargin: 10
visible: textEdit.length == 0 && !textEdit.activeFocus
color: "#a3a3a3"
}
}
}

Try this :
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
Window {
id: box
width: 640
height: 180
visible: true
title: qsTr("ScrollBar")
Flickable {
id: inputWrapper
anchors.fill: parent
contentHeight: input.implicitHeight
contentWidth: input.implicitWidth
ScrollBar.vertical: ScrollBar {
id: scrollBar
policy: ScrollBar.AlwaysOn
anchors.left: box.right
}
Keys.onUpPressed: scrollBar.decrease()
Keys.onDownPressed: scrollBar.increase()
clip: true
flickableDirection: Flickable.VerticalFlick
function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}
TextEdit {
id: input
anchors.fill: parent
text: ""
focus: true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: inputWrapper.ensureVisible(cursorRectangle)
} // TextEdit
} // Flickable
}

Related

QML: SplitView hides all children except last

I am new to QML and I am trying to implement a horizontal splitview with 2 children. The problem that I am having is that despite setting maximum and minimum widths for the children, the last child always takes up the entire split view and all the others are hidden and have to be manually opened. I have tried defining minimum and maximum widths using Layout.maximum/minimumwidth (which dont work at all) and have tried using fillwidth on the first child of the splitview. Nothing seems to work. I even copied and pasted the code from the qml doc page from splitview and it did the same thing. Here is my code:
import QtQuick 2.0
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.11
import "../buttons"
import "../customWidgets"
Rectangle {
id: conversationsPage
anchors.fill: parent
height: 455
width: 800
SplitView {
id: splitView
anchors.fill: parent
orientation: Qt.Horizontal
Rectangle {
id: sideBar
Layout.minimumWidth: 200
Layout.preferredWidth: 300
Layout.maximumWidth: 500
Layout.fillWidth: true
color: "#b9b9b9"
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
anchors.topMargin: 0
clip: true
Rectangle {
id: sideBarTopBar
y: 0
z: 2
height: 44
color: "#e868ff"
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: 0
anchors.rightMargin: 0
SearchBar {
id: conversationSearchBar
anchors.left: parent.left
anchors.right: newConversationBtn.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
anchors.topMargin: 10
anchors.leftMargin: 10
anchors.rightMargin: 10
}
IconBtn {
id: newConversationBtn
width: 35
height: 35
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 10
btnIconSource: "../images/icons/plus.svg"
}
}
ScrollView {
id: conversationsListScroll
anchors.left: parent.left
anchors.right: parent.right
anchors.top: sideBarTopBar.bottom
anchors.bottom: parent.bottom
z: 1
anchors.topMargin: 0
ColumnLayout {
id: conversationListLayout
x: 0
y: 0
width: conversationsListScroll.width
clip: true
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
ConversationTab {
Layout.fillWidth: true
}
}
}
}
Rectangle {
id: conversationView
Layout.fillWidth: false
Layout.minimumWidth: 300
Layout.maximumWidth: 500
color: "#ff0000"
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.topMargin: 0
anchors.bottomMargin: 0
}
}
Do you guys have any idea why the split view isnt working the way I want it to?
I noticed a couple of things, first, in SplitView from Quick Controls 2, you must use the SplitView attached property instead of the Layout attached property.
Secondarily, I also noticed that you have anchors specified inside the direct children of the SplitView, which have no effect and can be removed. I am not sure, but it seems like the child with SplitView.fillWidth: true should not have a maximum width set, as both children having maximum widths can prevent the SplitView from filling its parent Rectangle fully (you may still have a use case for this, but I removed it for this reason).
Here is the code with these recommendations:
SplitView {
id: splitView
anchors.fill: parent
orientation: Qt.Horizontal
Rectangle {
id: sideBar
SplitView.minimumWidth: 200
SplitView.preferredWidth: 300
SplitView.fillWidth: true
color: "#b9b9b9"
clip: true
// children here...
}
Rectangle {
id: conversationView
SplitView.fillWidth: false
SplitView.minimumWidth: 300
SplitView.maximumWidth: 500
color: "#ff0000"
}
}

How to change property of QML file from a QML file?

I created a switch in my settingsPage.qml file that can be turned on and off. When this is done, I want to change the color of a rectangle from homePage.qml file.
How do I create a connection between two qml files?
homePage.qml
import QtQuick 2.0
import QtQuick.Controls 2.15
import "../controls"
import QtQuick.Layouts 1.0
Item {
id: item1
Rectangle {
id: rectangle
color: "#2c313c"
anchors.fill: parent
property color windowBackground: #495163
...
I want to change the property windowBackground with the switch from settings.qml
Rectangle {
id: lightmode
width: 220
height: 60
visible: true
color: "#2c303b"
radius: 10
border.width: 0
Layout.fillHeight: false
anchors.topMargin: 20
anchors.leftMargin: 20
Switch {
id: customTitleBar1
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
display: AbstractButton.IconOnly
onToggled: {
//backend.showHideRectangle(customTitleBar.checked)
}
font.wordSpacing: 0
padding: 0
font.pointSize: 15
rightPadding: 0
Label {
id: labelTextName2
x: 120
width: 200
color: "#e8e9ec"
text: qsTr("Light Mode")
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.rightMargin: -170
font.pointSize: 14
}
spacing: 20
}
Layout.fillWidth: true
Layout.alignment: Qt.AlignLeft | Qt.AlignTop
}
(I am using Py Creator, Pyside2, and Qt Quick Control widgets.)
EDIT: Add complete code:
Code
settingsPage.qml:
import QtQuick 2.0
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.11
Item {
id: item1
Rectangle {
id: rectangle
color: "#2c313c"
anchors.fill: parent
Rectangle {
id: rectangleTop
height: 69
color: "#20232b"
radius: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.rightMargin: 50
anchors.leftMargin: 50
anchors.topMargin: 40
Label {
id: labelTextName
x: 10
color: "#f1f2f3"
text: qsTr("Settings")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.topMargin: 22
anchors.bottomMargin: 22
anchors.leftMargin: 10
anchors.rightMargin: 10
font.pointSize: 14
}
}
Rectangle {
id: rectangleTop1
color: "#20232b"
radius: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
anchors.topMargin: 131
anchors.leftMargin: 50
anchors.rightMargin: 50
GridLayout {
id: gridLayout
anchors.fill: parent
rows: 2
columns: 2
anchors.rightMargin: 10
anchors.leftMargin: 10
anchors.bottomMargin: 10
anchors.topMargin: 10
Rectangle {
id: rectangle1
width: 220
height: 60
visible: true
color: "#2c303b"
radius: 10
border.width: 0
Layout.fillHeight: false
Layout.alignment: Qt.AlignLeft | Qt.AlignTop
Layout.fillWidth: true
anchors.leftMargin: 20
anchors.topMargin: 20
Switch {
id: lightmode
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
display: AbstractButton.IconOnly
spacing: 20
font.wordSpacing: 0
rightPadding: 0
padding: 0
font.pointSize: 15
onToggled: {
}
Label {
id: labelTextName1
x: 120
width: 200
color: "#e8e9ec"
text: qsTr("Light Mode")
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.rightMargin: -170
font.pointSize: 14
}
}
}
Rectangle {
id: other
width: 220
height: 60
visible: false
color: "#2c303b"
radius: 10
border.width: 0
Layout.fillHeight: false
anchors.topMargin: 20
anchors.leftMargin: 20
Switch {
id: customTitleBar1
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
display: AbstractButton.IconOnly
onToggled: {
//backend.showHideRectangle(customTitleBar.checked)
}
font.wordSpacing: 0
padding: 0
font.pointSize: 15
rightPadding: 0
Label {
id: labelTextName2
x: 120
width: 200
color: "#e8e9ec"
text: qsTr("other")
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.rightMargin: -170
font.pointSize: 14
}
spacing: 20
}
Layout.fillWidth: true
Layout.alignment: Qt.AlignLeft | Qt.AlignTop
}
}
}
}
}
The homePage.qml:
import QtQuick 2.0
import QtQuick.Controls 2.15
import "../controls"
import QtQuick.Layouts 1.0
Item {
id: item1
Rectangle {
id: rectangle
color: "#2c313c"
anchors.fill: parent
//property color backgroundColor: globalObject.switchValue ? "#ffffff" : "#000000"
Rectangle {
id: rectangleTop
height: 69
color: "#495163"
//color: backgroundColor
radius: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.rightMargin: 50
anchors.leftMargin: 50
anchors.topMargin: 40
Label {
id: labelTextName
x: 10
color: "#e1e2e6"
text: qsTr("Welcome")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.topMargin: 22
anchors.bottomMargin: 22
anchors.leftMargin: 10
anchors.rightMargin: 10
font.pointSize: 14
}
}
Rectangle {
id: rectangleVisible
width: 540
color: "#1d2128"
radius: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: rectangleTop.bottom
anchors.bottom: parent.bottom
anchors.bottomMargin: 40
anchors.rightMargin: 50
anchors.leftMargin: 50
anchors.topMargin: 10
GridLayout {
anchors.fill: parent
columnSpacing: 5
anchors.bottomMargin: 61
anchors.topMargin: 119
anchors.rightMargin: 10
anchors.leftMargin: 10
rows: 2
columns: 3
CustomButton{
id: btnChangeName
height: 70
text: "Bitcoin"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.leftMargin: 10
Layout.maximumWidth: 200
Layout.fillWidth: true
Layout.preferredHeight: 70
Layout.preferredWidth: 250
onClicked: {
backend.setBtc()
backend.setAsOfLabel()
}
}
CustomButton {
id: btnChangeName1
height: 70
text: "Ethereum"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.preferredWidth: 250
Layout.preferredHeight: 70
Layout.maximumWidth: 200
Layout.fillWidth: true
onClicked: {
backend.setEth()
backend.setAsOfLabel()
}
}
CustomButton {
id: btnChangeName2
height: 70
text: "Binance Coin"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.rightMargin: 10
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
Layout.preferredWidth: 250
Layout.preferredHeight: 70
Layout.maximumWidth: 200
Layout.fillWidth: true
onClicked: {
backend.setBnb()
backend.setAsOfLabel()
}
}
CustomButton {
id: btnChangeName3
height: 70
text: "XRP"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.leftMargin: 10
Layout.preferredWidth: 250
Layout.preferredHeight: 70
Layout.maximumWidth: 200
Layout.fillWidth: true
onClicked: {
backend.setXrp()
backend.setAsOfLabel()
}
}
CustomButton {
id: btnChangeName4
height: 70
text: "sBDO"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.preferredWidth: 250
Layout.preferredHeight: 70
Layout.maximumWidth: 200
Layout.fillWidth: true
onClicked: {
backend.setsBdo()
backend.setAsOfLabel()
}
}
CustomButton {
id: btnChangeName5
height: 70
text: "BDO"
font.pointSize: 12
colorPressed: "#232435"
colorMouseOver: "#2b2c42"
colorDefault: "#252639"
Layout.rightMargin: 10
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
Layout.preferredWidth: 250
Layout.preferredHeight: 70
Layout.maximumWidth: 200
Layout.fillWidth: true
onClicked: {
backend.setBdo()
backend.setAsOfLabel()
}
}
}
Label {
id: labelDate
y: 295
height: 25
color: "#55aaff"
text: qsTr(" ")
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.bottomMargin: 5
anchors.rightMargin: 10
anchors.leftMargin: 10
font.pointSize: 12
}
Label {
id: priceSet
height: 70
color: "#cbcbde"
text: qsTr("Please select the following:")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
wrapMode: Text.Wrap
font.pointSize: 25
anchors.rightMargin: 50
anchors.leftMargin: 50
anchors.topMargin: 5
Component.onCompleted:{
}
}
Label {
id: asOfLabel
height: 70
color: "#cbcbde"
text: qsTr(" ")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
wrapMode: Text.Wrap
anchors.rightMargin: 30
font.pointSize: 16
anchors.topMargin: 48
anchors.leftMargin: 30
}
}
}
Connections{
target: backend
function onPrintTime(time){
labelDate.text = time
}
function onBdoInfo(argument){
priceSet.text = argument
}
function onDiffsbdoInfo(argument){
priceSet.text = argument
}
function onAsOfLabel(argument){
asOfLabel.text = argument
}
function onAssetBTC(argument){
priceSet.text = argument
}
function onAssetBNB(argument){
priceSet.text = argument
}
function onAssetXRP(argument){
priceSet.text = argument
}
function onAssetsBDO(argument){
priceSet.text = argument
}
function onAssetBDO(argument){
priceSet.text = argument
}
function onAssetETH(argument){
priceSet.text = argument
}
}
}
Since you haven't provided a complete code example, it's impossible to give an exact answer here. If your homePage instance is a parent of the settings.qml, then you could just reference it by id. That means if your code is something like this:
// HomePage.qml
Item {
id: homepage
Settings {}
}
then your Settings object can see the properties of HomePage, and you can do this:
// Settings.qml
Rectangle {
Switch {
onToggled: {
homePage.doSomething()
}
}
}
Or you can do it the reverse way and expose a property in Settings that HomePage can access:
// Settings.qml
Rectangle {
property alias switchToggled: switch.toggled
Switch {
id: switch
}
}
// HomePage.qml
Item {
id: homepage
property color backgroundColor: settings.switchToggled ? 'red' : 'blue'
Settings { id: settings}
}
Or for a third option, you can have both HomePage and Settings access data from some external source, like a C++ or Python object, or some QML singleton. That would look something like this:
// Settings.qml
Rectangle {
Switch {
onToggled: {
globalObject.switchValue = toggled
}
}
}
// HomePage.qml
Item {
id: homepage
property color backgroundColor: globalObject.switchValue ? 'red' : 'blue'
}

StackLayout in QML

Requirement: I am building a Settings app in QML, in which the I have divided screen into a grid. On the left hand side of the Grid, there are buttons : Home, Connectivity, Settings and Quit. and on the right hand side, corresponding display should be drawn. Currently, I have added a rectangle, and when I click on buttons like Home, Settings, connectivity etc.. . Code written inside the rectangle of StackLayout is executed successfully.
Instead of writing code in a rectangle, i want to write code in a separate file like settings.qml, connectivity.qml.
How to call the different file by clicking on buttons and setting current Index
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.3
ApplicationWindow {
id:main1
visible: true
x:0
y:20
width: Screen.width
height: Screen.height
title: qsTr("Settings")
GridLayout {
id: gridLayout
width: parent.width
height:main1.height
columns: 2
Rectangle {
id: left_rect
width: Screen.width/4
height: gridLayout.height
color:"yellow"
Button {
id: button
text: qsTr("Home")
anchors.right: parent.right
anchors.rightMargin: 5
anchors.left: parent.left
anchors.leftMargin: 5
anchors.top: parent.top
anchors.topMargin: 5
onClicked: {
layout.currentIndex = 0
}
}
Button {
id: button1
x: 1
y: -4
text: qsTr("Connectivity")
anchors.topMargin: 59
anchors.leftMargin: 5
anchors.left: parent.left
anchors.top: parent.top
anchors.rightMargin: 5
anchors.right: parent.right
onClicked: {
layout.currentIndex = 1
}
}
Button {
id: button2
x: 5
y: -8
text: qsTr("Settings")
anchors.topMargin: 112
anchors.leftMargin: 5
anchors.left: parent.left
anchors.top: parent.top
anchors.rightMargin: 5
anchors.right: parent.right
onClicked: {
layout.currentIndex = 2
}
}
Button {
id: button3
x: 6
y: -16
text: qsTr("Quit")
anchors.topMargin: 172
anchors.leftMargin: 5
anchors.left: parent.left
anchors.top: parent.top
anchors.rightMargin: 5
anchors.right: parent.right
onClicked: {
Qt.quit()
}
}
}
Rectangle {
id: right_rect
width: ( Screen.width/4 )*3
height: Screen.height
color:"green"
StackLayout {
id: layout
anchors.fill: parent
currentIndex: 0
Rectangle {
color: 'teal'
implicitWidth: 200
implicitHeight: 200
}
Rectangle {
color: 'plum'
implicitWidth: 300
implicitHeight: 200
}
Rectangle {
color: 'orange'
implicitWidth: 300
implicitHeight: 200
}
}
}
}
Where you currently have
Rectangle {
color: 'teal'
implicitWidth: 200
implicitHeight: 200
}
replace with
qmlClassName {
id: someId
}

QML ScrollBar combined with ListView

I'm new to QML and QT so don't blame me if this question is going to sound stupid for most of you but I've search all over the internet without any luck in founding an answer.
What I'm trying to do:
I'm having a ScrollView which has inside of it a ScrollBar and a ListView.
I want that at the moment when I'm scrolling the ListView elements to also move the bar from ScrollBar. In other words, I want to use the ScrollBar as an overall view of your current position, you are not supposed to touch that, its only purpose is for viewing.
My Code:
ScrollView{
implicitHeight: 100
implicitWidth: 50
anchors.fill: parent
ScrollBar.horizontal: ScrollBar{
id: hbar
active: true
policy: ScrollBar.AlwaysOn
anchors {
left: parent.left
top: parent.top
right: parent.right
}
background: Rectangle {
implicitWidth: 100
implicitHeight: 50
opacity: enabled ? 1 : 0.3
color: hbar.down ? "#red" : "black"
}
contentItem: Rectangle {
implicitWidth: 6
implicitHeight: 100
radius: width / 2
color: hbar.pressed ? "#81e889" : "#c2f4c6"
}
}
ListView {
id: listViewParent
height: listViewID.height/10*6
contentHeight: height*2
contentWidth: width*2
clip: false
interactive: false
keyNavigationWraps: true
anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left
anchors.leftMargin: 0
enabled: true
scale: 1
transformOrigin: Item.Center
anchors.verticalCenter: parent.verticalCenter
boundsBehavior: Flickable.DragAndOvershootBounds
flickableDirection: Flickable.HorizontalFlick
highlightMoveDuration: 0
cacheBuffer: 300
snapMode: ListView.SnapToItem
layoutDirection: Qt.LeftToRight
orientation: ListView.Vertical
model: 1
delegate:
ListView {
width: parent.width;
height: parent.height;
spacing: listViewID.width/8/9
model: MovieListModel {}
orientation: ListView.Horizontal
id: listid
delegate:
Rectangle {
property int recDynamicHeight: listViewID.height/10*6
property int recOriginalHeight: listViewID.height/10*6
property int recDynamiclWidth: listViewID.width/7
property int recOriginalWidth: listViewID.width/7
id: rectPer
width: recDynamiclWidth
height: recDynamicHeight
Image {
id: image1
anchors.fill: parent;
source: model.imgUrl
}
Text {
property bool isVisible: false
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: textid
text: model.title
font.bold: true
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9
leftPadding: listViewID.width/8/9
}
Text {
anchors.topMargin: listViewID.width/8/9
color: "#ffffff"
anchors.fill: parent
visible: textid.isVisible
id: yearId
text: model.year
horizontalAlignment: Text.AlignLeft
font.pixelSize: listViewID.width/8/9
topPadding: listViewID.width/8/9*2
leftPadding: listViewID.width/8/9
}
MouseArea {
anchors.fill: parent
onPressed: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamicHeight += rectPer.recOriginalHeight/10;
rectPer.recDynamiclWidth += rectPer.recOriginalWidth/10;
console.log(textid.isVisible);
textid.isVisible = true;
textid.visible = textid.isVisible;
console.log(sideButtonID.x);
console.log(sideButtonID.y);
console.log(model.year + " clicked");
}
onClicked: {
console.log("INDEX: " + model.id)
load_page(PageType.movie_detailed_view, model.title, model.description, model.imgUrl, model.type, model.year)
}
onReleased: {
rectPer.recDynamicHeight = rectPer.recOriginalHeight;
rectPer.recDynamiclWidth = rectPer.recOriginalWidth;
textid.isVisible = false;
textid.visible = textid.isVisible;
}
}
}
}
}
}
Layout:
You could try use a Flickable instead of a ScrollView and spawn a ListView there(delegates: Rectangles). Then, spawn the Scrollbar inside the ListView

QML ListView::contentWidth is wider than actual content

Trying to implement ListView's content scroll by clicking on a button. When scrolling towards the end of the view ListView's content does not stop at the end of the last picture it overscrolls. Below I provided the minimum working example as well as the preview what goes wrong. Just change the .img path to make it work on your PC. I was looking for some help in sources of ListView and its inherited parent Flickable but nothing that could help to resolve the problem. How to make it stop at the end of the last picture?
import QtQuick 2.14
import QtQuick.Window 2.14
Window {
visible: true
width: 1024
height: 300
Item {
id: root
anchors.fill: parent
property var imagesUrlModel: ["file:///C:/Users/mikha/OneDrive/Изображения/toyota.jpg",
"file:///C:/Users/mikha/OneDrive/Изображения/toyota.jpg"
]
property int _width: 0
Component {
id: imageDelegate
Image {
id: image
sourceSize.height: 300
source: modelData
fillMode: Image.Stretch
}
}
Rectangle {
id: leftButton
anchors.top: root.top
anchors.bottom: parent.bottom
anchors.topMargin: 15
anchors.leftMargin: 10
anchors.left: parent.left
color: "green"
width: 25
MouseArea {
anchors.fill: parent
onClicked: {
listView.contentX = listView.contentX > 0
? listView.contentX - 50 > 0 ? listView.contentX - 50 : 0
: 0
}
}
}
Rectangle {
id: rightButton
anchors.top: root.top
anchors.bottom: parent.bottom
anchors.topMargin: 15
anchors.rightMargin: 10
anchors.right: parent.right
color: "green"
width: 25
MouseArea {
anchors.fill: parent
onClicked: {
listView.contentX = listView.contentX < listView.contentWidth
? listView.contentX + 50
: listView.contentWidth
//wrong content width
}
}
}
ListView{
id: listView
clip: true
boundsBehavior: Flickable.StopAtBounds
anchors.topMargin: 15
anchors.left: leftButton.right
anchors.right: rightButton.left
anchors.top: root.top
anchors.bottom: parent.bottom
spacing: 5
orientation: ListView.Horizontal
model: root.imagesUrlModel
delegate: imageDelegate
}
}
}
In your example just change listView.contentWidth to listView.contentWidth-listView.width in onClicked event for rightButton. But that's not enough. You should check whether the listView.contentX+50 is not overflowing listView.contentWidth-listView.width before you update the listView.contentX. In such case you need to update listView.contentX with difference between listView.contentWidth and listView.width.
Here it is:
listView.contentX = listView.contentX+50 <= listView.contentWidth-listView.width
? listView.contentX + 50
: listView.contentWidth - listView.width
I used another approach with repeater and scrollview and it has worked!
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Window {
visible: true
width: 1024
height: 300
Item {
id: contentItem
anchors.fill: parent
Rectangle {
id: rightButton
anchors.top: contentItem.top
anchors.bottom: contentItem.bottom
anchors.rightMargin: 10
anchors.right: contentItem.right
color: "green"
width: 25
MouseArea {
anchors.fill: parent
onClicked: {
var allowedWidth = scrollView.flickableItem.contentWidth - scrollView.viewport.width
if(row.width < scrollView.viewport.width){
return
}
var offset = scrollView.flickableItem.contentX + 50
if(offset <= allowedWidth){
scrollView.flickableItem.contentX += 50
}
else {
scrollView.flickableItem.contentX = allowedWidth
}
}
}
}
ScrollView {
id: scrollView
anchors.left: contentItem.left
anchors.right: rightButton.left
anchors.top: contentItem.top
anchors.bottom: contentItem.bottom
clip: true
verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
property var imagesUrlModel: [
"file:///C:/Users/mikha/OneDrive/Изображения/toyota.jpg",
"file:///C:/Users/mikha/OneDrive/Изображения/toyota.jpg"
]
Row {
id: row
spacing: 15
Repeater {
id: repeater
anchors.left: parent.left
anchors.right: parent.right
model: scrollView.imagesUrlModel
delegate: Component {
id: imageDelegate
Image {
id: image
sourceSize.height: 300
source: modelData
fillMode: Image.Stretch
}
}
}
}
}
}
}

Resources