Results 1 to 7 of 7

Thread: setting an image on the form

  1. #1
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default setting an image on the form

    In the constructor of my project's class say myclass.cpp

    i wrote the following code:

    #include "myclass.h"
    #include "ui_myclass.h"

    MyClass::MyClass(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyClass)
    {
    ui->setupUi(this);

    QString imagePath="xyz1.jpeg";
    QImage logo(imagePath);
    }

    The image doesn't get loade on the form as specified by QImage class documentation of QT SDK 4.6.2....
    Whats wrong??? Can anyone please guide???

    I even tried this.....

    #include "myclass.h"
    #include "ui_myclass.h"

    MyClass::MyClass(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyClass)
    {
    ui->setupUi(this);

    QString imagePath="xyz1.jpeg";
    QImage logo;
    logo.load(imagePath);
    }

    Do i have to add anything else to set the image on the form???

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting an image on the form

    Yes.
    Read about the difference between QImage and QPixmap.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    Yayati.Ekbote (15th March 2010)

  4. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setting an image on the form

    The simplest way to display an image is to use a QLabel and a QPixmap:
    According to Qt doc:
    QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen
    Qt Code:
    1. int main(int argc, char** argv)
    2. {
    3. QApplication app(argc, argv);
    4. QPixmap image("image.jpg");
    5. QLabel imageLabel();
    6. imageLabel.setPixmap(image);
    7. label.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to toutarrive for this useful post:

    Yayati.Ekbote (15th March 2010)

  6. #4
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: setting an image on the form

    thanks guys, but this isn't working too...According to toutarrive, i made the following changes..

    I drew a Label on the UI form and wrote as following....


    #include "myclass.h"
    #include "ui_myclass.h"

    MyClass::MyClass(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyClass)
    {
    ui->setupUi(this);

    QPixmap logo("xyz1.jpg");
    ui->Label->setPixmap(logo);
    }

  7. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting an image on the form

    Where is the image located?
    Try using the absolute path.
    Also, you can test QPixmap::isNull() to know if the loading of the image was successful.
    You might want to read about using resources in Qt:
    http://doc.trolltech.com/4.6/resources.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #6
    Join Date
    Jan 2010
    Posts
    37
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: setting an image on the form

    Hi, high_flyer, the image is located in the same folder where the project files are stored.

    My imageExample folder consists of following files:
    1)imageExample.pro
    2)imageExample.cpp
    3)imageExample.h
    4)imageExample.ui
    5)main.cpp
    6)Logo.jpg
    7)ui_imageExample.h
    8)Makefile
    9)Makefile.debug
    10)Makefile.release
    11)imageExample.pro.user

    and two folders debug and release...

    debug contains 5 files and release is empty

  9. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting an image on the form

    put the image file where the exe is.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. setting an image.jpg in a QWidget
    By impeteperry in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2009, 20:00
  2. [Qt4.1] How to insert an image inside a Form?
    By Gonzalez in forum Qt Tools
    Replies: 5
    Last Post: 23rd September 2008, 12:20
  3. How to insert an image in background of a Form?
    By awanish_jmi in forum Qt Tools
    Replies: 2
    Last Post: 8th August 2008, 11:52
  4. Replies: 7
    Last Post: 13th November 2006, 16:44
  5. form setting
    By mickey in forum Qt Tools
    Replies: 5
    Last Post: 27th September 2006, 01:28

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.