PDA

View Full Version : How to add images to a Listview in QML?



harish
2nd February 2012, 12:31
Hi all, i am new to Qml and now i am trying an application in which:when a button is pressed a list of images should be displayed in a QListView.

What i had tried so far is:

I had created a button in .qml file and when clicked on it the function call goes to .cpp file to display the listview with images.
I was able to do it in .qml file itself but i am looking to display it by making a function call to .cpp.

My code:

.qml file
Button { id: button1 x: 79 y: 299 text: “Click here for listview” onClicked: { Abcd.apple() } }

.cpp file:

#include“abcd.h”
#include<QListWidgetItem>
#include<QHBoxLayout>
#include<QMainWindow>

void Abcd::apple()
{ QListWidget *listWidget = new QListWidget();
listWidget->addItem(new QListWidgetItem(QIcon(“c:/apple.jpg”)));
listWidget->addItem(new QListWidgetItem(QIcon(“c:/ball.jpg”)));
listWidget->addItem(new QListWidgetItem(QIcon(“c:/cat.jpg”)));
listWidget->addItem(new QListWidgetItem(QIcon(“c:/dog.jpg”)));

QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(listWidget);
QMainWindow w;
w.setCentralWidget(listWidget);
w.showMaximized(); }

From my code i was able to get the function call to the .cpp file when tried with some other samples….but when trying to display the image i was not able to do it..

Anyone help me with this so that i can find the solution….

Regards,
Harish.M