Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to add images to a Listview in QML?
Forum Updated to NodeBB v4.3 + New Features

How to add images to a Listview in QML?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mkhpad
    wrote on last edited by
    #1

    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->setSortingEnabled(true);
    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

    [EDIT: code formatting, please wrap in @-tags, Volker]

    Harish.M

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Lots and lots of issues here.

      First, format your code nicely, using '@' tags and inserting new line after every or most ';'.

      Next, you are mixing QtWidgets and QML, which is not the usual course of things. If you want to add custom functionality to QML, you have to use relevant QDeclarative* classes.

      Another thing is, you are creating QMainWindow on a stack, which means that this object will be deleted when the scope ends. in your case, that's the closing bracket ('}').

      Also, void Abcd::apple() - is this method a slot, or declared with Q_INVOKABLE? OTherwise, you will not be able to invoke it from QML.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mkhpad
        wrote on last edited by
        #3

        I will try what you have said sir if this works thanks a lot..:)

        Harish.M

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved