Results 1 to 11 of 11

Thread: Push Button with image?

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Push Button with image?

    I am trying to display an image on a Push button using the icon property.

    Here is the compiled ui file:

    Qt Code:
    1. pushButton_3 = new QPushButton(tab);
    2. pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
    3. pushButton_3->setGeometry(QRect(500, 30, 211, 131));
    4. QIcon icon2;
    5. icon2.addFile(QString::fromUtf8("resource/image.jpg"), QSize(), QIcon::Normal, QIcon::Off);
    6. pushButton_3->setIcon(icon2);
    7. pushButton_3->setIconSize(QSize(100, 100));
    To copy to clipboard, switch view to plain text mode 

    The image is displayed inside Qt creator, but the compiled executable does not have the image in the button.

    I have also tried creating a resource file and including images to the resource file.

    Some help would be great!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Push Button with image?

    Try with a png file and with an absolute file path. If it works then either you are missing an image plugin for jpeg in the environment where you run the application or the file you are trying to load can't be found or accessed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    steve.bush (17th March 2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Push Button with image?

    THANK YOU!!

    Using the complete path worked!

  5. #4
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Push Button with image?

    If you want to use the resource system, is it not that there is a ":/" missing? In my application I use resiurce files like this:
    QFile qfi(":/texts/res/usageNotes.txt");
    This works:
    Qt Code:
    1. QIcon icon;
    2. QPixmap qpm;
    3.  
    4. if(qpm.load(":/img/myImage.jpg"))
    5. {
    6. icon.addPixmap(qpm);
    7. ui->pushButton->setIcon(icon);
    8. }
    9. else ui->label->setText("nincs betöltve");
    To copy to clipboard, switch view to plain text mode 

    Otherwise I have the guess that the absolute path works because the executable file is not generated in the same directory as the code, so the executable file whilst working is trying to find the image on the relative path from itself, where is actually nothing, as there might be even no directory like that.
    Last edited by szisziszilvi; 17th March 2011 at 12:44.
    Szilvi

  6. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    379
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Push Button with image?

    Simplest way to set image on the Push Button::
    btn = (QPushButton *)ui->btn;
    btn->setIconSize((QSize(56,68)));
    QPixmap* pixmap1 = new QPixmap(":/images/orange.bmp");
    QIcon icon1(*pixmap1);
    btn->setIcon(icon1);

    I think this might help you..

  7. #6
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Push Button with image?

    Qt Code:
    1. btn = (QPushButton *)ui->btn;
    To copy to clipboard, switch view to plain text mode 

    Why this cast ? (which is an old C-style cast anyway).

  8. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    379
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Push Button with image?

    Anyway its works fine. Finding the simplest way to use yet more powerful

  9. #8
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Push Button with image?

    ui->btn->setIconSize((QSize(56,68))); isn't work?

    and in qt don't use old c-style cast. you have static_cast and object_cast
    Try read Qt documentation before ask stupid question.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Push Button with image?

    You shouldn't need any casts here. Be it implicit or explicit, C or C++ style.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    379
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Push Button with image?

    btn = (QPushButton *)ui->btn;
    btn->setIconSize((QSize(56,68)));

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Push Button with image?

    Quote Originally Posted by Gokulnathvc View Post
    btn = (QPushButton *)ui->btn;
    btn->setIconSize((QSize(56,68)));
    But why the cast? Not that this is relevant to the subject (it was about a missing file) but since we're already here, let's solve this situation too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. [QT]Push button-no action
    By nqn in forum Newbie
    Replies: 4
    Last Post: 30th May 2010, 20:08
  2. A very small push button
    By Gnurou in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2009, 04:04
  3. problem in Displaying image on push button
    By durgarao in forum Qt Tools
    Replies: 4
    Last Post: 2nd January 2009, 11:27
  4. shape of push button
    By Seema Rao in forum Qt Programming
    Replies: 23
    Last Post: 2nd April 2008, 02:05
  5. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 17:31

Tags for this Thread

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.