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!!!
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().
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!
Re: Add a .jpg image as the background or main panel of my main window
See the QPixmap class, especially its constructors.
Re: Add a .jpg image as the background or main panel of my main window
Ok I added in the main window ui class:
before the automatically generated
Code:
centralWidget
= new QWidget(New311Class
);
and I added
Code:
centralWidget->setPalette(palette);
centralWidget->setAutoFillBackground(true);
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 ?
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.
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
Re: Add a .jpg image as the background or main panel of my main window
Quote:
Originally Posted by
KaKa
If you are saying adding the image to the resource folder, then I am sure I did.
No, it's not enough.
Re: Add a .jpg image as the background or main panel of my main window
From the QGraphicsView reference I saw the following code:
Code:
view.setBackgroundBrush(":/images/backgroundtile.png");
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?
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).
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 !
Re: Add a .jpg image as the background or main panel of my main window
Quote:
Ok I added in the main window ui class:
Quote:
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.
Re: Add a .jpg image as the background or main panel of my main window
Quote:
Originally Posted by
KaKa
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:
Code:
#include <QApplication>
#include <QWidget>
int main(int argc, char **argv){
QPixmap px
("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
wgt.setPalette(p);
wgt.show();
wgt.resize(400, 300);
return app.exec();
}
Maybe you don't have a JPEG image plugin for Qt available?
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?
Code:
QPixmap px
("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
wgt.setPalette(p);
[B][U] ui.addWedgit(wgt); [/U][/B]//But I don't know if ui has addWedgit though(I'll check).
Re: Add a .jpg image as the background or main panel of my main window
Quote:
Originally Posted by
KaKa
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.
Quote:
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.
Quote:
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
Quote:
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?
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!
:) :D