PDA

View Full Version : Scalling window(widget) - any ideas?



obquty
29th April 2010, 20:41
Hi,
it is any way to resize window to lower width and height?
For example i have window witch have many images,buttons,and other and everything is placed in QVBoxLayout and QHBoxLayout.Images have customizable width and height. Window now have 1280px width and 960px height.
Now i want to run my application in window 640px x 480px.
My question : It is any way to scale window automatically? I know about setWindowSize but images have greater width and height with compare to my window sizes so window is autmatically greater:/
I don't want to scale images and other components manually because there are about 50 diffrent images and components.
Thank you for reply and sorry for my english.

wysota
29th April 2010, 21:06
Do you want those images scaled? If not, then derive your window (or the central widget in case you are using QMainWindow as your window) from QScrollArea.

obquty
3rd May 2010, 14:43
Hi,
yes i do.:/

I have something like this
http://img191.imageshack.us/img191/7213/obqut1.png
So i want to run this application on netbook where maximum resolution is for example 1024px x 756px
Now i work on resolution 1280px x 1024px and it's fine but i want to run this application on lower resolution and see all on my small netbook.
I use window.setFixedSize(800,600) but look what i have:/
http://img140.imageshack.us/img140/566/obqut2.jpg
So it is any easy way to do this autmatically???

ChrisW67
4th May 2010, 02:49
Your problem seems to be that the right hand end of the graph panels has been rendered underneath the control panels (and off the right of the window) on the smaller window. This implies that the graph panels are not part of a layout and you have manually placed them on the larger window. You fix this by putting the panels and controls into an appropriate layout and then leaving Qt to do the work.

You should read the Layout Management overview (http://doc.trolltech.com/4.6/layout.html) and the docs for QGridLayout in Assistant

obquty
4th May 2010, 22:43
Thx for reply:)
My program is based on the QVBoxLayout, i don't want to write it again and using GridLaout. Mayby it is other easy way to fix it?
Look how i " use Layout Management", i think it mayby can show my problem and somebody show me the right way:/


QVBoxLayout *main_layout=new QVBoxLayout;

QHBoxLayout *first_line=new QHBoxLayout;
QHBoxLayout *second_line=new QHBoxLayout;
QHBoxLayout *third_line=new QHBoxLayout;

And then i add Widgets to first,second and thirs layout.
I add images for any of this layouts where and then i draw grap on this images.
Look how i add images.


QLabel *place_for_image1=new QLabel;
QImage image1 = QImage(1000,200, QImage::Format_RGB32);
image1.load("C:/Project/image1.jpg");
place_for_image1->setPixmap(QPixmap::fromImage(image1));

QLabel *place_for_image2=new QLabel;
QImage image2 = QImage(1000,200, QImage::Format_RGB32);
image2.load("C:/Project/image2.jpg");
place_for_image2->setPixmap(QPixmap::fromImage(image2));

QLabel *place_for_image3=new QLabel;
QImage image3 = QImage(1000,200, QImage::Format_RGB32);
image3.load("C:/Project/image3.jpg");
place_for_image3->setPixmap(QPixmap::fromImage(image3));

And then i add it to the layouts :
...

firstLine->addWidget(place_for_image1);
secondLine->addWidget(place_for_image2);
secondLine->addWidget(place_for_image3);
.....
Then i add other widgets(dials,labels,lcd)

Finally i build Main Layout and set it.


main_layout->addLayout(first_line);
main_layout->addLayout(second_line);
main_layout->addLayout(third_line);
setLayout(main_layout);

Mayby now anybody can help me with my problem,but without big changes in code.

wysota
4th May 2010, 23:12
Mayby now anybody can help me with my problem,but without big changes in code.
Maybe if you post the image correctly (and I do mean correctly, see the site rules). Right now it is hard to say anything about your application.

obquty
5th May 2010, 22:16
Thank you "wysota" for your post but i'm sure -you can't help me. Why? Because you want to judge everything else but not my program. Please , if you want to really help wrote something to help me with my problem.
If i have something bad,please show me this point and tell me how to fix it. Anybody can tell "It's easy, your program is bad" , but it isn't all. Show what and how i can fix it. If you want do the same what you do in last post,please keep it for yourself.
Human is learning all of his life,so if you want help -help,if you don't -go wherever you want but not here.
Thank you for attention.

wysota
5th May 2010, 22:35
Because you want to judge everything else but not my program.
(...)
Human is learning all of his life,so if you want help -help,if you don't -go wherever you want but not here.
Thank you for attention.

Or maybe because I'm a moderator of this board and it is my duty to enforce the rules of the site (http://www.qtcentre.org/rules), I will stay here and insist you obey the rules.

Your posting of the image was in direct violation with the site rules which are there for a reason. You inlined an image from a 3rd party site and based your post on it. Now the image is gone and your post doesn't make sense anymore. So if you want to get helped by anyone post the image properly or the 3rd party site (or yourself, I don't know which of you discarded the image) will remove the image again making another post of yours useless.

ChrisW67
6th May 2010, 04:32
See attached image.

The black marks boxed in green are the tick marks of the graph image protruding from underneath each control panel (in orange). The control panels are not the same height as the corresponding graph image, implying that they are probably not in the same QHBoxLayout as the graph. The overlaying of the control on the graph implies that the control panels are not in the same layout. The control panels also vary in width, something that would be fixed for free by using the grid layout. You can do it with your nested layouts but you need to look at how they relate.

The QGridLayout change should be straightforward, just create a single different layout object and then a minor tweak on the addWidget() or addLayout() calls. Any other tweak you need to get relative sizes straight you would have to do either way.

Attempting to put a fixed 1000 pixel wide image into a box that is less than 1000 pixels wide is a classic case for a scroll area. I guess you could scale the pixmap manually as the space contracts.

obquty
6th May 2010, 21:11
Thank you for reply ChrisW67 and for tips how to attach images wysota. My english is very poor so i think i don't understood this point of rules.

ChrisW67 Yes,you have right.A Widdgets like QDial,QLcdNumber are not in the same QHBoxLayout.
It looks like in attachment one:)))

4597

I want to tell you how work my program,and what i want to do:/
I draw graph one each other of 3 main images using my function drawGraph();
The fuction it is a loop where i go from 0px to end of the image.(image.width()) The image width is 1000.
For each pixel( x=0....x=10....x=200...x=999,x=980) i calculate second value "y". Then i put pixel on(x,y)
If i scale image to lower width before i run the function,my graph don't paint at all. So i want to use funtcion graph with image in his originally size,and after that i want to scale this image but with painted graph on it.
I hope so you know what i have on my mind and how fix this problem.

Please look on the images you attached in previous post.
The right panel for each of levels is scalled ok.
But the main images and the images witch which is answer for linear cale are simply cut.

wysota
6th May 2010, 23:10
I think the main question is - what happens if the image doesn't fit into the widget. Do you want to scale the image, crop (cut) it (and on which end) or is it allowed to have scrollbars that can be used to move each image back and forth? If you want the image scaled then you need to make the widget that displays it fixed size and reimplement its paintEvent to render the image scaled. Something like the following:


class MyGraphWidget : public QWidget {
public:
MyGraphWidget(QWidget *parent = 0) : QWidget(parent){}
void setImage(const QImage &img) { original_image = img; update(); }
protected:
void paintEvent(QPaintEvent *pe){
QImage img = original_image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPainter p(this);
p.drawImage(0, 0, img);
}
private:
QImage original_image;
};

The implementation is actually inefficient as the graph is rescaled every time the widget is redrawn but I'm sure you get the idea.

obquty
8th May 2010, 01:40
Hi,
yes yes yes i think exactly about something like this.:) I don't finish yet but now i am very close.
One of the last question....

Program is based on QWidget.
I want to run application in 3/4 of screen width and height.
So i get the screen resolution and use function setFixedSize(screen_width*0,75,screen_height*0.75) ;
It works but now i want to scale images using to my window.
My originally images looking good in window 1000x600
So if i scalle image to half width and half height of it, it will be good looking in window 500x300.
Every time when i run my application i want to get resolution of the screen,then set size for the window using function setFixedSize and scale images but with proportion of gain screen size.
Please look at the example code:
MAIN.CPP

Widget main_window;
QDesktopWidget *d = QApplication::desktop();
int w_screen = d->width();
int h_screen = d->height();
main_window.setFixedSize(w_screen*0.75,h_screen*0. 75);

How to get acces to "w_screen" and "h_screen" to use this variables in widget.cpp??
I want to do something like that:

WIDGET.CPP

...
double width_value=(double)w_screen/1000; //getting the proportion for images
double height_value=(double)h_screen/600; //
....
QImage new_main_image1=main_image1.scaled(main_image1.wid th()*width_value, main_image1.height()*height_value, Qt::KeepAspectRatio, Qt::SmoothTransformation);
image_view->setPixmap(QPixmap::fromImage(new_main_image1));
...

I don't know how to get w_screen and h_screen in this place.If i will have them else should be easier.
Mayby my idea to fit window and this way to automatically scalling is not good( a little complicated) but i want to finish it in this form.