Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: Images/icons. It's not displayed

  1. #1
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Images/icons. It's not displayed

    Hi everybody!
    I'd like to put some icons on my controls (e.g. on a label). I put it into my disigner, here it is displayed, but when I compile my application, all of the images/icons are not displayed! Even the window icon!
    I wrote manually the code (into my ui header):
    Qt Code:
    1. Dialog->setWindowIcon(QIcon("d:/gg.jpeg"));
    To copy to clipboard, switch view to plain text mode 
    No result...
    What is the problem, can you help me?

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

    Default Re: Images/icons. It's not displayed

    setWindowIcon() will certainly not result in an icon appearing on your label... If you use relative paths to icons, I suggest you change them to absolute ones or better yet use a resource file containing all the images. You can create such a file from within Designer and then you'll have to add it to RESOURCES variable in your .pro file.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Images/icons. It's not displayed

    I wrote manually the code (into my ui header):
    You can't write that there, because will get overwritten every time the ui is recompiled.
    It has already been said in other posts. You must first read this: http://doc.trolltech.com/4.2/designe...component.html

    Also, make sure you read this post: http://www.qtcentre.org/forum/f-newb...dow-7002.html;

    To display images in widgets use setPixmap or setIcon.
    setWindowIcon has effect only for windows with title bar. It sets the standard pixmap for the system menu icon.

    Regards

  4. #4
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    2wysota:
    well, setWindowIcon() I used just to put icon as a main window icon.
    About paths, qt-designer, I think, use itself absolute paths. I mean that when I put some icon into designer on a label, designer generate the path itself.

    2marcel:
    I know that my ui header will get overwritten every time the ui is recompiled.
    But I need to put here some code... For example I include here my_class.h and my_class.cpp, I create my_class object here, I connect signals to slots from my_class.. and every time when I change my window and recompile my ui, at the beginning I save some parts of my code (connecting slots, declaring object), and, after recompiling put it down again.

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Images/icons. It's not displayed

    What you have done is called "shooting yourself in the leg" .

    Use the single inheritance approach ( it is the simple one ). I your class add all the custom code that you add in the ui_*.h.

    This is how it needs to be done. Not because I say so, but a lot of people do it.
    The Single Inheritance Approach

    In this approach, we use the Ui::ImageDialog object as we did in the simple case, but instead of setting up the dialog from the application's main function, we subclass QDialog and set up the user interface from within the constructor. Components used in this way expose the widgets and layouts used in the form to the QDialog subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application.
    This approach is used in the Calculator Form example.
    To ensure that we can use the user interface, we need to include the header file that uic generates before referring to Ui::ImageDialog:
    #include "ui_imagedialog.h" The subclass is defined in the following way:
    class ImageDialog : public QDialog
    {
    Q_OBJECT

    public:
    ImageDialog(QWidget *parent = 0);

    private:
    Ui::ImageDialog ui;
    }; The important features of the class are the private ui object which provides the code for setting up and managing the user interface.
    The constructor for the subclass constructs and configures all the widgets and layouts for the dialog just by calling the ui object's setupUi() function. Once this has been done, it is possible to modify the user interface and create items for the combobox:
    ImageDialog::ImageDialog(QWidget *parent)
    : QDialog(parent)
    {
    ui.setupUi(this);

    ui.colorDepthCombo->addItem(tr("2 colors (1 bit per pixel)"));
    ui.colorDepthCombo->addItem(tr("4 colors (2 bits per pixel)"));
    ui.colorDepthCombo->addItem(tr("16 colors (4 bits per pixel)"));
    ui.colorDepthCombo->addItem(tr("256 colors (8 bits per pixel)"));
    ui.colorDepthCombo->addItem(tr("65536 colors (16 bits per pixel)"));
    ui.colorDepthCombo->addItem(tr("16 million colors (24 bits per pixel)"));

    connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    } We can connect signals from the user interface widgets to slots in the dialog in the usual way, taking care to prefix the ui object to each widget used.
    The main advantages of this approach are its simple use of inheritance to provide a QDialog-based interface, and its encapsulation of the user interface widget variables within the ui data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or "compose") them. This approach can be used to create individual tabs from existing forms, for example.
    Read this http://doc.trolltech.com/4.2/designe...component.html.
    Of course, you may also use the multiple inheritance approach.

    Regards

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

    Default Re: Images/icons. It's not displayed

    Quote Originally Posted by LMZ View Post
    2wysota:
    well, setWindowIcon() I used just to put icon as a main window icon.
    About paths, qt-designer, I think, use itself absolute paths. I mean that when I put some icon into designer on a label, designer generate the path itself.
    I suggest you open the file generated by Designer and see for yourself then... Think logically - what sense would it make to use an absolute path for an image? What would happen if you installed the application in another path on a different machine?

  7. #7
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    2marcel:
    thx, it was useful! know I don't write code into ui header and it works nice. thx

    2wysota:
    well, I think I must not put the path for my image by myself, let the disigner do it..
    But, I'll say again, using disigner for putting images make my app not to display it... Into disigner (and into preview-mode) images are displayed, but after compilation and running application images are not displayed..
    In some hours i'll show you the code generated by disigner, know I need to go.

    marcel, wysota:
    thx 4 your help

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

    Default Re: Images/icons. It's not displayed

    Quote Originally Posted by LMZ View Post
    well, I think I must not put the path for my image by myself, let the disigner do it..
    Let me say it again - Designer inserts paths relative to the directory the form is being saved in, so it works when previewing. But when you compile the application under Windows, it is most often moved to a directory called debug or release (depending on the build type), which changes the path thus making Qt fail to load images. This gives you three choices:

    1. Copy images to a proper folder so that relative paths are still valid.
    2. Use Qt resource system as I already suggested.
    3. Register a function (with the form) that will return pixmaps for the form.

  9. #9
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    wysota:
    Ohh, finally I understand what is the problem!!! Thanks a lot.
    But can you explain (or to give link to some docs) the third step, about registering the function?

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

    Default Re: Images/icons. It's not displayed

    It's rarely used, so I suggest you go for solution #2. But to satisfy your curiosity - open a form in Designer and choose "Form settings" from the "Form" menu. There you'll be able to activate the "Pixmap function" option and enter the name of a function that is being passed a QString and returns a pixmap. It could be implemented like this:
    Qt Code:
    1. QPixmap getPixmap(const QString &name){
    2. return QPixmap(QApplication::applicationDirPath()+QDir::separator()+name);
    3. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    ok, thanks, know it's cleare for me.
    and one more question - can I display on my form animated gif? When I add some pictures, the supported format is jpg and some others (I suggest), so within designer I can't add gif..

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

    Default Re: Images/icons. It's not displayed

    You have to enable gif support and recompile Qt. But it's not enough to make animated gifs work. You have to use QMovie.

  13. #13
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    ok, with animations i'll "play" later

    know, I tried to use resources, as you said...
    .qrc file:
    Qt Code:
    1. <!DOCTYPE RCC><RCC version="1.0">
    2. <qresource>
    3. <file>images/icon.jpg</file>
    4. </qresource>
    5. </RCC>
    To copy to clipboard, switch view to plain text mode 
    In my .pro I added RESOURCES = res.qrc .
    And finally, in my constructor i put down
    Qt Code:
    1. this->setWindowIcon(QIcon(":/images/icon.jpg"));
    To copy to clipboard, switch view to plain text mode 
    No result..

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

    Default Re: Images/icons. It's not displayed

    Is the path correct during compilation (debug/release folder issues again)? What does QFile::exists(":/images/icon.jpg") return?

  15. #15
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    well, QFile::exists() return true. What about the path, tutorial says that
    The specified paths are relative to the directory containing the .qrc file.
    And nothing about debug/release folders path..

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

    Default Re: Images/icons. It's not displayed

    What happens if you try to display a PNG file instead of a JPEG? And please test on a QLabel and not using a window icon.

  17. #17
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    No result... I add to /images an png image - image.png
    To res.qrc I add -
    Qt Code:
    1. <file>images/image.png</file>
    To copy to clipboard, switch view to plain text mode 
    And my .cpp:
    Qt Code:
    1. qSpeed->setWindowIcon(QIcon(":/images/image.png"));
    To copy to clipboard, switch view to plain text mode 
    //qSpeed is a label

  18. #18
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Images/icons. It's not displayed

    Have you recompiled your resource?

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

    Default Re: Images/icons. It's not displayed

    First of all, did you run qmake after adding the resource file to the project?

  20. #20
    Join Date
    May 2007
    Posts
    46
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Images/icons. It's not displayed

    marcel, wysota:
    sure

    But, can I put my icon on a label, by using QLabel::setWindowIcon(QIcon(...)) ? Maybe this is the problem?...

Similar Threads

  1. Replies: 20
    Last Post: 15th August 2008, 23:19
  2. JPEG Image isn't displayed!?
    By GodOfWar in forum Qt Programming
    Replies: 9
    Last Post: 16th April 2007, 15:01
  3. QLineEdit - Float number not displayed correct
    By morty in forum Qt Programming
    Replies: 3
    Last Post: 9th November 2006, 00:47
  4. Replies: 6
    Last Post: 10th April 2006, 10:47

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.