How to insert images into QListWidget?
I am new to QT, I am facing some problems in inserting images to list view. This is my current code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListWidget* list = new QListWidget();
QListWidgetItem *item1 =开发者_Go百科 new QListWidgetItem(QIcon(":\temp\boat.png"), "BlueHills", list);
list->insertItem(0, item1);
QListWidgetItem *item2 = new QListWidgetItem(QIcon("C:\\Documents and Settings\\admin\\Desktop\\icons\\car.png"), "Sunset", list);
list->show();
}
The problem I have is only the text is being displayed.
How to insert image1
how to insert image2
I took your code and pasted it in a brand new QtCreator project and it works. I would suggest you double check your images' paths.
The path for the image of your first item should be ":/temp/boat.png". Make sure that your image is clearly defined in your resource file. I would therefore suggest you to test with an image located in the root of C, for instance "C://mypic.png"
.
You can add a resource file from within QtCreator using the New File or Project dialogue. The Qt Resource File is located under Files and Classes -> Qt
The resulting file may look something like this:
<RCC>
<qresource prefix="images_section">
<file alias="MyBoatImage">boat.png</file>
</qresource>
</RCC>
If you choose to write it out, you need to add it your .pro file as such:
RESOURCES += resource.qrc
Whichever way you create the resource file, you can access its resources through something like: ":/images_section/MyBoatImage"
精彩评论