Results 1 to 6 of 6

Thread: Showing Image

  1. #1
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing Image

    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.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPixmap>
    4. #include <QLabel>
    5.  
    6.  
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. ui->WindowSkin_Dice_Image->setPixmap(":/Resources/Images/Window_Skin_Colors/Black_Window_Color.png");
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    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&)
    Last edited by InterFiction; 24th November 2011 at 01:17.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Showing Image

    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.
    Last edited by ChrisW67; 24th November 2011 at 00:54. Reason: updated contents

  3. #3
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing Image

    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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    .pro

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2011-11-23T17:10:55
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = RPG_Suit
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. mainwindow.cpp
    15.  
    16. HEADERS += mainwindow.h
    17.  
    18. FORMS += mainwindow.ui
    19.  
    20. RESOURCES += \
    21. Resources.qrc
    To copy to clipboard, switch view to plain text mode 
    Last edited by InterFiction; 24th November 2011 at 01:15.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Showing Image

    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:
    Qt Code:
    1. void QLabel::setPixmap(const QPixmap&)
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing Image

    This code for instance. My app will run, but there's no image in the label..

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPixmap>
    4. #include <QLabel>
    5. #include <QString>
    6.  
    7.  
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13.  
    14. ui->setupUi(this);
    15. QPixmap * Dice_StartSkin = new QPixmap(":/images/Black_Window_Color.png");
    16. ui->WindowSkin_Dice_Image->setPixmap(*Dice_StartSkin);
    17.  
    18.  
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by InterFiction; 24th November 2011 at 03:51.

  6. #6
    Join Date
    Nov 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing Image

    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

    Qt Code:
    1. <RCC>
    2. <qresource prefix="/Resources">
    3. <file>Resources/Images/Window_Skin_Colors/Black_Window_Color.png</file>
    4. </qresource>
    5. </RCC>
    To copy to clipboard, switch view to plain text mode 

    main window cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPixmap>
    4. #include <QLabel>
    5. #include <QString>
    6. #include <QtGui/QApplication>
    7.  
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14.  
    15. ui->setupUi(this);
    16. QPixmap Dice_Window_StartSkin(":/Resources/Resources/Images/Window_Skin_Colors/Black_Window_Color.png");
    17. ui->WindowSkin_Tab1->setPixmap(Dice_Window_StartSkin);
    18. ui->WindowSkin_Tab2->setPixmap(Dice_Window_StartSkin);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

    thanks for pointing me in
    Last edited by InterFiction; 24th November 2011 at 05:26.

Similar Threads

  1. Showing image at end of mp4 video
    By fazich in forum General Programming
    Replies: 1
    Last Post: 13th February 2011, 15:53
  2. Image showing
    By nmkarvekar in forum Qt Programming
    Replies: 10
    Last Post: 17th December 2009, 10:52
  3. Image-based widgets showing is too slow in Qt 4.2.1
    By mkrentovskiy in forum Qt Programming
    Replies: 11
    Last Post: 8th August 2007, 22:20
  4. Replies: 3
    Last Post: 14th March 2007, 08:09
  5. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.