PDA

View Full Version : Showing Image



InterFiction
24th November 2011, 00:08
I'm trying to get a label to show an image, but it's not show when I run the app. I've got the image file set in the pixmap property. I was wondering too. I'm going to have all these files in a resource file too. I've started adding some of the files to my recourse file. When I click the pixmap thing and click choose recourse the resource file isn't there. It says resource root, but nothings there. If I want to make the label have an image from my resource file am I going to have to do that via code?

Added after 7 minutes:

Also could someone point out how to do this from code?

this isn't right, lol.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QLabel>



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->WindowSkin_Dice_Image->setPixmap(":/Resources/Images/Window_Skin_Colors/Black_Window_Color.png");
}

MainWindow::~MainWindow()
{
delete ui;
}



Errors


mainwindow.cpp:-1: In constructor 'MainWindow::MainWindow(QWidget*)':

error: no matching function for call to 'QLabel::setPixmap(const char [61])'

candidates are: void QLabel::setPixmap(const QPixmap&)

ChrisW67
24th November 2011, 00:50
The error is likely to be in mainwindow.h (the only file included before the error that you wrote/modified). Perhaps a missing semicolon at the end of the class declaration but I have no way of knowing for sure.

The missing image is quite likely because the virtual path of the image within your resource file is not ":/Resources/Images/Window_Skin_Colors/Black_Window_Color.png".

Edit: Line 4... the file is called QLabel not Qlabel
Edit: Line 5 is simply rubbish and will never work. Read The Qt Resource System to see how to include resources in your project. Hint: Use the PRO file.

InterFiction
24th November 2011, 01:00
hmm..all I did was add a resource file, add prefix images, and then add file, and chose the file from the directory they're stored. I'm really new to this, so I appreciate any help..

this is what my window looks like when I double click the qrc file...

/Images
Resources/Images/Window_Skin_Color/Black_Window_Color.png

the file itself reads


<RCC>
<qresource prefix=" /Images">
<file>Resources/Images/Window_Skin_Colors/Black_Window_Skin_Color.png</file>
</qresource>
</RCC>

Here's the code for my header file


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


.pro


#-------------------------------------------------
#
# Project created by QtCreator 2011-11-23T17:10:55
#
#-------------------------------------------------

QT += core gui

TARGET = RPG_Suit
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

RESOURCES += \
Resources.qrc

ChrisW67
24th November 2011, 01:52
Please don't edit posts in such a way as they invalidate the replies. It makes it hard for others the follow.

The new error message is self-explanatory. The prototype for setPixmap (from the error message) is:

void QLabel::setPixmap(const QPixmap&)
You are passing a "const char*" that the compiler cannot magically convert to a QPixmap because there is no QPixmap conversion constructor that takes "const char*". Construct a QPixmap explicitly or use a QString in place of your raw string.

The path to your image file will be ":/Images/Resources/Images/Window_Skin_Colors/Black_Window_Skin_Color.png" if you remove the leading space from the prefix in the QRC file.

InterFiction
24th November 2011, 03:51
This code for instance. My app will run, but there's no image in the label..


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QLabel>
#include <QString>



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

ui->setupUi(this);
QPixmap * Dice_StartSkin = new QPixmap(":/images/Black_Window_Color.png");
ui->WindowSkin_Dice_Image->setPixmap(*Dice_StartSkin);


}

MainWindow::~MainWindow()
{
delete ui;
}

InterFiction
24th November 2011, 05:26
Okay, I got this working, for anyone that's following this for a solution the answer was simple. Your prefix has to match the folder that your images are in. haha...hey, no kidding. Well, that's why I'm in the newb forum...here's the answer

resource file


<RCC>
<qresource prefix="/Resources">
<file>Resources/Images/Window_Skin_Colors/Black_Window_Color.png</file>
</qresource>
</RCC>


main window cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QLabel>
#include <QString>
#include <QtGui/QApplication>



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

ui->setupUi(this);
QPixmap Dice_Window_StartSkin(":/Resources/Resources/Images/Window_Skin_Colors/Black_Window_Color.png");
ui->WindowSkin_Tab1->setPixmap(Dice_Window_StartSkin);
ui->WindowSkin_Tab2->setPixmap(Dice_Window_StartSkin);
}

MainWindow::~MainWindow()
{
delete ui;
}


thanks for pointing me in