PDA

View Full Version : Add a .jpg image as the background or main panel of my main window



KaKa
17th March 2007, 10:29
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!!!

wysota
17th March 2007, 12:05
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().

KaKa
17th March 2007, 17:49
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!

wysota
17th March 2007, 18:28
See the QPixmap class, especially its constructors.

KaKa
19th March 2007, 08:46
Ok I added in the main window ui class:


QPalette palette;
palette();
palette.setBrush(QPalette::Window, QPixmap(":/Resources/IntroPic.png"));

before the automatically generated

centralWidget = new QWidget(New311Class);

and I added


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 ?

wysota
19th March 2007, 09:35
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.

KaKa
19th March 2007, 10:28
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

wysota
19th March 2007, 11:07
If you are saying adding the image to the resource folder, then I am sure I did.
No, it's not enough.

KaKa
19th March 2007, 11:11
From the QGraphicsView reference I saw the following code:


QGraphicsView view;
view.setBackgroundBrush(":/images/backgroundtile.png");
view.setCacheMode(QGraphicsView::CacheBackground);

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?

wysota
19th March 2007, 12:24
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).

KaKa
20th March 2007, 06:00
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 !

high_flyer
20th March 2007, 09:02
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.

wysota
20th March 2007, 09:57
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:


#include <QApplication>
#include <QWidget>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget wgt;
QPalette p = wgt.palette();
QPixmap px("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
p.setBrush(QPalette::Window, QBrush(px));
wgt.setPalette(p);
wgt.show();
wgt.resize(400, 300);
return app.exec();
}

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

KaKa
20th March 2007, 12:35
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?


QWidget wgt;
QPalette p = wgt.palette();
QPixmap px("/usr/share/icons/crystalsvg/48x48/actions/kde.png");
p.setBrush(QPalette::Window, QBrush(px));
wgt.setPalette(p);
ui.addWedgit(wgt); //But I don't know if ui has addWedgit though(I'll check).

wysota
20th March 2007, 13:01
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/designer-using-a-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?

KaKa
23rd March 2007, 16:16
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