Results 1 to 16 of 16

Thread: Add a .jpg image as the background or main panel of my main window

  1. #1
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Red face Add a .jpg image as the background or main panel of my main window

    I am using Qt4 Visual Studio 2005 QT designer embedded project(commercial trial version).

    I have a main window, and I want to add a .jpg image as the window's background, or simply have it as the main part(biggest portion) of the window.

    Can any one give me a hint of what classes/ functions or what QT tool from the designer tool box I can use to add this image?

    Thanks a lot!!!

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Set it as the QPalette::Window role of the palette (QPalette) for your main window or central widget depending on the effect you want to achieve. You'll obviously need QWidget::palette() and QWidget::setPalette().

  3. #3
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add a .jpg image as the background or main panel of my main window

    Thanks a lot! Actually I sort of know that I should use pallete.

    It is just I don't see any function like "importImage" or "or addImage" or functions like that. So I don't know how to pass the address(the image file) onto the window.

    Sorry to be so troublesome, but could you explain a bit more ?

    Thanks!

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    See the QPixmap class, especially its constructors.

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

    KaKa (17th March 2007)

  6. #5
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: Add a .jpg image as the background or main panel of my main window

    Ok I added in the main window ui class:
    Qt Code:
    1. QPalette palette;
    2. palette();
    3. palette.setBrush(QPalette::Window, QPixmap(":/Resources/IntroPic.png"));
    To copy to clipboard, switch view to plain text mode 

    before the automatically generated
    Qt Code:
    1. centralWidget = new QWidget(New311Class);
    To copy to clipboard, switch view to plain text mode 

    and I added

    Qt Code:
    1. centralWidget->setPalette(palette);
    2. centralWidget->setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 
    After that automatically generated code.
    And I added the .png file under the prc directory, that is, under the Resources folder under the project.

    And the pictrue didn't show up.

    If you have time, could you help me out a bit ?

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Did you add the image to the resource file? Try using an absolute path from your filesystem first to check if the method works at all.

  8. #7
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: Add a .jpg image as the background or main panel of my main window

    So you mean adding the entries in an xml formated sqr file? I don't think I did. But I am trying now. Doesn't seem to be the way I thought it work though.

    If you are saying adding the image to the resource folder, then I am sure I did.

    I tried the absolute path, it doesn't work , unfortunately...:S

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Quote Originally Posted by KaKa View Post
    If you are saying adding the image to the resource folder, then I am sure I did.
    No, it's not enough.

  10. #9
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add a .jpg image as the background or main panel of my main window

    From the QGraphicsView reference I saw the following code:

    Qt Code:
    1. view.setBackgroundBrush(":/images/backgroundtile.png");
    2. view.setCacheMode(QGraphicsView::CacheBackground);
    To copy to clipboard, switch view to plain text mode 

    I did almost the same thing, just that my "view" is a pointer and I used -> instead of . (since that graphics view was automatically generated.) And I used a direct path of 125 chars. however the compiler gave an error message saying that cannot convert from const char[125] tp cpmst QBrush &;

    How can that be possible?

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Do you use a QGraphicsView? You didn't mention that before...

    If the compiler complains this way, make sure you actually pass a QBrush object (this involves calling appropriate constructors).

  12. #11
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add a .jpg image as the background or main panel of my main window

    Well before I didn't use, but now since I am seeking a way to work arround, I use this QGraphicsView to make life easier. And since the sample code doesn't work,

    I added a Brush to the QGraphicsView, and in the Brush I initialized a Painter, that painter initializzes by passing file name. But the picture still didn't show up.

    Never minde...I will just try to work it out myself. Thanks so much alreay on helping me so much !

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Ok I added in the main window ui class:
    After that automatically generated code.
    Are you by any chance changing the uic genrated code??
    If you are - DONT!
    It will be overwritten every time you rebuild your project.
    You have to subclass the uic genrated class if you want to change it.

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Quote Originally Posted by KaKa View Post
    Never minde...I will just try to work it out myself. Thanks so much alreay on helping me so much !
    This works quite fine for me:

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. QWidget wgt;
    7. QPalette p = wgt.palette();
    8. QPixmap px("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
    9. p.setBrush(QPalette::Window, QBrush(px));
    10. wgt.setPalette(p);
    11. wgt.show();
    12. wgt.resize(400, 300);
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Maybe you don't have a JPEG image plugin for Qt available?

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

    shank (23rd April 2007)

  16. #14
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Add a .jpg image as the background or main panel of my main window

    To wysota:
    Thanks for the code...if you notice, or maybe if I wrote more clearly, you will find on my previous posts that I was using the exact logic and codings as you did. It just didn't show up.

    Problem is that your main application has a show() function there, but I can't really in the uic class in anywhere tell the things I added to show. and I was thinking about adding a paintevent somewhere...but using that will only help me show the code I added, cuz there is no function like, show, or exec for that uic class. Even if there were, it was hard to integrate my code and the uic and to let them show together.

    Maybe:
    1. You are right, I don't have JPEG image plug in...what is that? I can I have it?

    2. High_flyer is right, every time I build the project, my changes are being overwritten.

    So to High_flyer:
    Could you tell me how to sub class that uic class?

    Do I do this in the substantial(which had an object of this uic class: ui) class?

    Qt Code:
    1. QWidget wgt;
    2. QPalette p = wgt.palette();
    3. QPixmap px("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
    4. p.setBrush(QPalette::Window, QBrush(px));
    5. wgt.setPalette(p);
    6. [B][U] ui.addWedgit(wgt); [/U][/B]//But I don't know if ui has addWedgit though(I'll check).
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Add a .jpg image as the background or main panel of my main window

    Quote Originally Posted by KaKa View Post
    To wysota:
    Thanks for the code...if you notice, or maybe if I wrote more clearly, you will find on my previous posts that I was using the exact logic and codings as you did.
    The point is my code is verified as working.

    Problem is that your main application has a show() function there, but I can't really in the uic class in anywhere tell the things I added to show.
    2. High_flyer is right, every time I build the project, my changes are being overwritten.

    So to High_flyer:
    Could you tell me how to sub class that uic class?

    Do I do this in the substantial(which had an object of this uic class: ui) class?
    Never touch the class generated by uic.

    The ui file is only a simple class containing positions and attributes of widgets, nothing more. You should then create a new class that inherits QWidget (or its subclass) and that has the ui class as its member or inherit the ui class and call the setupUi(this) method from the ui class. This will setup all the widgets. Then you can manipulate them from within the subclass. It's all in the manual:
    http://doc.trolltech.com/4.2/designe...component.html

    1. You are right, I don't have JPEG image plug in...what is that? I can I have it?
    How do you know you don't have it? Qt has it by default. Can you display any jpg image on a label?

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

    KaKa (23rd March 2007)

  19. #16
    Join Date
    Mar 2007
    Posts
    30
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: Add a .jpg image as the background or main panel of my main window

    Actually your replies really help a lot! I think it should work fine now according to information you provided me. But I will try that out when I come back from work.

    Once again, thanks so much for replying so fast and so in detail!


Similar Threads

  1. move parent window to the front.
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2007, 09:41
  2. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 11:21

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.