Results 1 to 10 of 10

Thread: Problems with QPushButton displaying an icon

  1. #1
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with QPushButton displaying an icon

    Hi

    I tried initializing a QPushButton with an icon ( image file in the resource file ). But the button is not displaying the icon. I have tried changing the layout direction. I also checked the icon size and it is giving me a size of 16 x 16. The original icon size is 40 x 34. So I am assuming that the icon has been assigned to the push button but it is not being displayed. Does anyone out there have any ideas?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPushButton displaying an icon

    Can we see your code? Probably you have an error in your file, and have you bind the resource file to your application properly?

  3. #3
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPushButton displaying an icon

    The code is simple :

    Qt Code:
    1. QPushButton* button = new QPushButton (QIcon (":/options.png"), tr ("Options"), this);
    To copy to clipboard, switch view to plain text mode 

    As far as binding is concerned, I am using Visual Studio.net with Qt integration and the resource file (.qrc) does have the above mentioned icon.

    Thanks

  4. #4
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with QPushButton displaying an icon

    Try like this:

    Qt Code:
    1. QPixmap buttonImage("argazkiak/flecha.bmp");
    2. QIcon Icon;
    3. Icon.addPixmap ( buttonImage, QIcon::Normal, QIcon::Off );
    4. QPushButton *Button = new QPushButton();
    5. Button->setIcon(Icon);
    6. Button->setIconSize(QSize(100,100));
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 16th July 2009 at 16:17. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPushButton displaying an icon

    Are you using other resource files without any problem?
    what does
    Qt Code:
    1. QPixmap p(":/options.png");
    2. qWarning() << p.isNull();
    To copy to clipboard, switch view to plain text mode 
    say?

  6. #6
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPushButton displaying an icon

    Quote Originally Posted by Lykurg View Post
    Are you using other resource files without any problem?
    what does
    Qt Code:
    1. QPixmap p(":/options.png");
    2. qWarning() << p.isNull();
    To copy to clipboard, switch view to plain text mode 
    say?
    I checked out your code and yes I think I am having problems with the resource file. QPixmap is giving me a null value; though I don't know why.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPushButton displaying an icon

    Quote Originally Posted by John82 View Post
    and yes I think I am having problems with the resource file.
    1. How does your main function look like (Q_INIT_RESOURCE?)
    2. did you use prefixes in your qrc file?

  8. #8
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPushButton displaying an icon

    I did not use Q_INIT_RESOURCE earlier because I thought it would be loaded automatically. After adding the line I am now facing a new error while compilation. It is giving me an "unresolved external symbol" error.

    Error message :

    unresolved external symbol "int __cdecl qInitResources_project(void)" (?qInitResources_project@@YAHXZ) referenced in function _main
    My resource file is stored in a folder called Resources.

    Contents :

    <RCC>
    <qresource prefix="Project">
    <file>options.png</file>
    </qresource>
    </RCC>
    Main function :

    Qt Code:
    1. int main (int argc, char* argv[])
    2. {
    3. Q_INIT_RESOURCE (project);
    4.  
    5. // Create a QApplication with passed arguments
    6. QApplication app (argc, argv);
    7.  
    8. // Create the main program window
    9. MainWindow mw;
    10.  
    11. // Main window geometry management
    12. int width = 800;
    13. int height = 640;
    14.  
    15. // Show the main window.
    16. mw.show();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPushButton displaying an icon

    Quote Originally Posted by John82 View Post
    Qt Code:
    1. Q_INIT_RESOURCE (project);
    To copy to clipboard, switch view to plain text mode 
    If you use that, your resource file has to be named project.qrc. And you are using a prefix so also ":/options.png" is wrong. I suggest you read this first.

  10. #10
    Join Date
    Jul 2009
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problems with QPushButton displaying an icon

    My resource file is named "project". After some trial and error, I got the code to work. It seems every time I add files to the .qrc file, I have to reload it in Visual studio. So icons are being read now. Thanks for the help!!

Similar Threads

  1. Displaying icon in tableView
    By JPNaude in forum Qt Programming
    Replies: 3
    Last Post: 14th October 2008, 07:28
  2. Problems styling QScrollBar / QPushButton
    By December in forum Qt Programming
    Replies: 0
    Last Post: 20th January 2008, 06:53
  3. Stretching Icon in QPushButton
    By dsandber in forum Qt Tools
    Replies: 1
    Last Post: 6th October 2007, 16:51
  4. QPushbutton Flat property and icon appearance.
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2006, 08:30
  5. Displaying Icon
    By Seema Rao in forum Qt Programming
    Replies: 4
    Last Post: 9th February 2006, 20:43

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.