AFAIR it's enough to set the label as the center widget, apply a pixmap to it and set the alignment in both directions to center. I think the problem here is that the author doesn't use a layout for the central widget at all.
AFAIR it's enough to set the label as the center widget, apply a pixmap to it and set the alignment in both directions to center. I think the problem here is that the author doesn't use a layout for the central widget at all.
Shawn (27th May 2007)
I thought so first, but it seems that he just wants it centered horizontally. The y coord is always 0.apply a pixmap to it and set the alignment in both directions to center
Shawn (27th May 2007)
Yes, i don't use central widget
because i need to draw MANY small images at their certain positions, and then show all of them as a whole structure.
this is also the reason why i kept drawing that label, because there may be many same images.
sorry that i didn't make it clear at the beginning.
and, why my code doesn't work? I don't know where this bug is.
Last edited by Shawn; 27th May 2007 at 08:41.
Then I suggest implementing a custom layout that positions the labels at their preset values( relative to parent).
By doing this you don't have to readjust the positions of the labels in resize event.
You can achieve this by modifying the Flow Layout example in the Qt Examples ( in the Layouts category ).
Regards
Shawn (27th May 2007)
dear marcel:
can you help finding out why my code doesn't work?
I have already reimplemented the resizeEvent
Qt Code:
void SLD::resizeEvent(QResizeEvent * /* event */){ mWidth = width(); showCB((mWidth-50)/2,0,100,50);}To copy to clipboard, switch view to plain text mode
Then he can center horizontally and vertically align to top. And set the size policy to minimumExpanding, so that the label takes all available space.
Of course you use a center widget.
So why not draw them on a pixmap and then just apply the pixmap to the label?because i need to draw MANY small images at their certain positions, and then show all of them as a whole structure.
If you only want to draw some images, you don't need a label.this is also the reason why i kept drawing that label, because there may be many same images.
Shawn (27th May 2007)
Because you always create the label in showCB.
By creating a new one, the old one will remain in the old position.
You should create your label only once, and position it afterwards, as needed.
But my question is, what will you do when you have more labels?
Shawn (27th May 2007)
Yes, of course that would be better, but I thought maybe he wants to interact somehow with those images.So why not draw them on a pixmap and then just apply the pixmap to the label?
Anyway, there are many solutions, neither is that hard, and this thread seems to be one of those that gets 2-3 pages long, so I'm bailing out.
Regards
Shawn (27th May 2007)
I consider a label as a "canvas" for showing a image.
so when I want to show a image, I creat a label for it.
maybe it's less efficient but I thought it would work.
just as you said, by creating a new label, the old one will remain in the old position
then there should 2 image, one at the new position and the other at the previous position, is that right?
in my program, the image just appear at the previous position, that's why I said"the image is still at the previous position"
waiting for your kindly reply!
But the resize events are posted very quickly. So, if you resize your window by 100 pixels, there is a good chance that the window receives 50 ( I'm just estimating here, could be less, could be more ) resize events.
Therefore you create 50 labels with the same image.
JPN suggested in an earlier post ( in this thread ) to draw the label delayed ( using a QTimer or whatever ). But this isn't to work in your case either because you're creating the labels in the resize event handler.
But why do you create them in when a resize event is received?I consider a label as a "canvas" for showing a image.
so when I want to show a image, I creat a label for it.
maybe it's less efficient but I thought it would work.
I don't know exactly what are you trying to do by this, but shouldn't you load them as a response to some user action ( button press, action trigger )?
Or do you want this to be some kind of background for your main window?
Please elaborate on this a little.
I understand you're trying to show some images, but why like this?
Shawn (27th May 2007)
this is the outcome of my program:
every element is a small image.
the position for each element is given by a XML file, which I should parse at the beginning.
So, my program should parsing a XML file and generate this layout automatically.
what I want to implement now is to keep the layout at the central(horizontal) when someone resize the window.
Last edited by Shawn; 27th May 2007 at 09:31.
In this case I approve Wysota's advice and I also strongly suggest you use a QGraphicsView.
Using the Graphics View Framework you will be able to achieve what I have seen in the screen shot much easier.
The Graphics View Framework is very well documented in Assistant and there are clarifying examples in the Qt Demos.
You should use this if you want your app to be fast and well behaved.
Once you understand the basics of this framework you will be able to do all those things very easily.
Of course, if you get stuck, you can ask here.
Regards
Shawn (27th May 2007)
and this one is the final goal
Yes, you can do this with QGraphicsView.and this one is the final goal
The main drawbacks of the method you're currently using are:
- If the document gets bigger you will have to add a QScrollArea manually;
- You have to manage image positions manually at resize and scroll.
- Zooming would be very hard to implement
QGraphicsView offers all of the above by default, with simple to use API, so you should think about using it.
You could event interact with those components in your document( move them around, change connections, etc ).
Regards
Shawn (27th May 2007)
QGraphicsView is certainly the way to go.
Shawn (27th May 2007)
Thank you very much for your suggestion!
I am gona to try it.
Why not use QGraphicsView?
Shawn (27th May 2007)
Bookmarks