Is there any way to use image placeholder in qml? - qt

Currently I am getting images from server , so sometimes it takes some time to load or sometimes if there is no image on the server ,the image place is showing blank. So i need to show something/default image as long as the server image is not loaded.
I tried to do this, if there is no image on the server from where i'm loading image then I try to show a image from my local.
Is there any way to show a default image if the image is not loaded properly from server.
Image {
id:img
height: 100
width: 300
Component.onCompleted: {
if(there is no image on server){
img.source="assets/images/someimage.png"
}
else{
img.source="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80"
}
}
}

A possible solution is to place an Image on top of the main Image and when it is loaded hide the initial Image:
Image {
id: img
width: 300
height: 100
source: "https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80"
Image{
id: default_img
anchors.fill: parent
source: "assets/images/someimage.png"
visible: img.status != Image.Ready
}
}

Related

Showing a busy indicator or a loading image while loading large QML file

I'm working on a QML based app. where I dynamically load the content. However when running the application it takes quite a long time (5-10 secs), so I need to show any loading screen or indicator while the whole content is being loaded. Can anyone suggest me how to do it ?
For example, after I login in my application it took some time to load the next page so within that oeriod of time i want to show the loading screen.
App {
id: app
height: 400
width: 200
Rectangle {
id: rectangle
Button {
id: button
text: qsTr("GO TO NEXT PAGE")
onClicked:stackView.push("page2.qml")
}
Image {
id: image
fillMode: Image.PreserveAspectFit
source: "default-app.png"
}
}
}
Suppose this is my code then where can i use loader ? I never used it before
You can use the status from a Loader component (I'm guessing you are using that since you are loading "dynamically"). Then use that in a BusyIndicator.
Loader {
id: loader
asynchronous: true
...
BusyIndicator {
anchors.centerIn: parent
running: loader.status == Loader.Loading
}
}
Heck, the Qt docs for BusyIndicator should have get you going!

How to remove bounds of the Flickable QML component?

I want to display a large amount of content, for example, a grid of multiple images inside a window that is smaller than the content, similar to a geographical map but instead of a map, I want my own components as the "map". For this minimal working example, let's take for the content a grid of images with a total size of 1000x1000 with a window into this content of only 300x300.
I have tried 2 different approaches, but I will only go into detail of the first approach as that is the one that got me closest to my desired result:
I have tried the Flickable component but the content cannot be moved outside the predefined bounds, making the user unable to move the view in order to display all the parts of the content. So the simplest solution that I'm thinking about now is if I could remove these bounds from the Flickable component, but how?
I have also tried the Map component but it requires a "plugin" and I was unable to figure out how to use this component with my own content of an image grid.
The content that I want to show is something this
Grid {
columns: 5
spacing: 2
Repeater {
model: ListModel {
ListElement {
path: 'test1'
}
ListElement {
path: 'test2'
}
// ...
ListElement {
path: 'test25'
}
}
Rectangle {
width: 200
height: 200
Image {
anchors.fill: parent
source: 'file:' + path
}
}
}
}
I tried, putting this inside the Flickable like this
Flickable {
anchors.centerIn: parent
width: 300
height: 300
contentWidth: 1000
contentHeight: 1000
clip: true
// INSERT CUSTOM GRID COMPONENT HERE
}
This results in a 300x300 view inside the content as expected, but once you start to flick through the content to view different parts of it, you are stopped by the bounds preventing you from seeing anything outside these bounds. You can see it while dragging but once you release the view of the content is reset to these bounds.
So how do I remove these bounds? (Or is there a different component more suitable for my application?)
Here is a gif that shows how the content can be dragged passed the bounds, but once released it will only go up to the bounds and not further
I found the issue, I set the contentWidth and contentHeight of the Flickable incorrectly, this example works fine. The contentWidth and contentHeight determine the bounds in which you can flick.

Using light effect (brightness) in qml

I'm trying to make a light effect that appears from my image whenever i click on it, i have tried the following:
Image {
id: image1
source: "../../blue_dot.png"
BrightnessContrast {
id:myBright
x: image1.width/2
y: image1.height/2
visible: false
}
MouseArea{
anchors.fill: parent
onPressed: {
myBright.brightness = 1
myBright.visible = true
}
}
}
but unfortunately nothing appears whenever i click on my image, Any ideas on how can i make a light effect out of an image using qml?
That's not the way to use a GraphicalEffect. Have a look a the official Qt documentation.
You have to :
Set the visibility of your image to false and the visibility of the effect to true
Set the width and height of the graphical effect to the height and width of the image (anchors.fill: image is fine)
Set the source property of the graphicalEffect to your image's id
OnClicked, ajust the contrast/brightness properties of the GraphicalEffect (default: 0, 0)
Someting like this:
Image {
id: image1
source: "../../blue_dot.png"
visible: false
}
BrightnessContrast {
id:myBright
anchors.fill: image1
source: image1
}
MouseArea{
anchors.fill: parent
onPressed: {
myBright.brightness==0? myBright.brightness= 0.2:myBright.brightness=0
}
}
Seems that some required properties are not set:
BrightnessContrast {
id:myBright
x: image1.width/2
y: image1.height/2
visible: false
// New properties:
source: image1
width: 100
height: 100
}
Width and height I choose randomly. You should replace my values with correct ones. But mb better will be to use something like "anchors.fill: image1".

Ubuntu SDK: How do I implement a “onClick” for an image in QML?

I have a background image for my page, and I want to implement a sort of page refresh when the background is clicked. However, I didn't find any actions for the image element in QT quick.
What's the right way to implement this?
You need to use MouseArea to handle click events.
Image {
source: "myimage.png"
MouseArea {
anchors.fill: parent
onClicked: {
// do what you want here
}
}
}

How do I remove the BusyIndicator after you read a feed

I have a problem with a BusyIndicator, what happens is that I have appointed BusyIndicator that when loading the feed is removed, but in my file. QML images do not appear in the feed, I wonder if there is any way to tell the BusyIndicator that when the image is displayed BusyIndicator remove.
Well I did this, you can give it a try.
Image
{
id : image1
source:"...your source here"
}
BusyIndicator
{
id:busy1
anchors.centerIn: parent
width:50
height:50
visible: image1.status == Image.Loading
running: image1.status == Image.Loading
}
//indicator only visible and run when image is loading. After image is being loaded or when Image.Ready, the busy indicator will disappear.
You can do something like this
Image{
id:remoteImage
source: "http://www.example.com/m.jpg"
onProgressChanged: {
if(progress==1.0)
busyIndicator.visible=false;
}
}

Resources