Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Reimplement of resizeEvent

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Reimplement of resizeEvent

    what I want: show image at the central of the QMainwindow(centralWidget)
    but after resizing, the image is still at the previous position

    my code is like this:

    Qt Code:
    1. SLD::SLD(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. }
    6.  
    7. void SLD::showCB(int x, int y, int w, int h)
    8. {
    9. label=new QLabel(ui.centralWidget);
    10. label->setGeometry(x,y,w,h);
    11. label->setPixmap(QPixmap("Resources/CB.bmp"));
    12. }
    13.  
    14. void SLD::resizeEvent(QResizeEvent * /* event */)
    15. {
    16. mWidth = width();
    17. showCB((mWidth-50)/2,0,100,50);
    18. }
    To copy to clipboard, switch view to plain text mode 
    please help me.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Reimplement of resizeEvent

    The easiest possible way is to let the label scale its contents:
    Qt Code:
    1. SLD::SLD(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. QLabel* label=new QLabel(this);
    6. label->setPixmap(QPixmap("Resources/CB.bmp"));
    7. label->setScaledContents(true); // let the label scale its contents
    8. setCentralWidget(label); // label will get automatically resized as a central widget
    9. }
    To copy to clipboard, switch view to plain text mode 

    If you need more intelligent scaling, like keeping aspect ratio or something, you might want to scale it by hand. This would be done by storing the image as a member variable, scaling it for example in resizeEvent() and setting the scaled image on the same label. I'd suggest doing it a bit delayed so that each and every resizeEvent() in a row doesn't scale and set a new image.

    Notice that you were creating new QLabel widgets again and again during every resize event! Also, Qt has a concept of layouts to handle geometries. You barely set geometries by hand.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Shawn (27th May 2007)

  4. #3
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    thanks for ur quick reply!

    what's wrong with my code? it is supposed to work.
    If I want to show the image"CB.bmp" exatcly at ((mWidth-50)/2,0,100,50) when the window is resized,what should I do?
    Last edited by Shawn; 27th May 2007 at 07:19.

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    Qt Code:
    1. #include "centrallabel.h"
    2. #include <QLabel>
    3. #include <QHBoxLayout>
    4.  
    5. centrallabel::centrallabel(QWidget *parent, Qt::WFlags flags)
    6. : QMainWindow(parent, flags)
    7. {
    8. ui.setupUi(this);
    9.  
    10. label = new QLabel( this->centralWidget() );
    11. label->setPixmap( QPixmap("c:\\pix_png.png") );
    12. label->setFixedSize( label->sizeHint() );
    13. }
    14.  
    15. void centrallabel::resizeEvent(QResizeEvent* )
    16. {
    17. label->move( width()/2 - label->size().width()/2, 0 );
    18. }
    19.  
    20. centrallabel::~centrallabel()
    21. {
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    This one works. The label will be kept centered at all times.

    As jpn said, the problem with your code was that you were always creating that label.
    The label should be created only once.
    Also, you shouldn't be so strict about sizes. In your case you should care only about position.

    You also could implement a small, custom layout that centers the widget(s) in the parent's area.
    But this works too.

    Regards
    Attached Images Attached Images

  6. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

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

    Default Re: Reimplement of resizeEvent

    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.

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

    Shawn (27th May 2007)

  9. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    apply a pixmap to it and set the alignment in both directions to center
    I thought so first, but it seems that he just wants it centered horizontally. The y coord is always 0.

  10. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

  11. #7
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    Quote Originally Posted by wysota View Post
    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.
    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.

  12. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    Quote Originally Posted by Shawn View Post
    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.
    sorry that i didn't make it clear at the beginning.
    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

  13. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

  14. #9
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    dear marcel:
    can you help finding out why my code doesn't work?
    I have already reimplemented the resizeEvent
    Qt Code:
    1. void SLD::resizeEvent(QResizeEvent * /* event */){ mWidth = width(); showCB((mWidth-50)/2,0,100,50);}
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Reimplement of resizeEvent

    Quote Originally Posted by marcel View Post
    I thought so first, but it seems that he just wants it centered horizontally. The y coord is always 0.
    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.

    Quote Originally Posted by Shawn View Post
    Yes, i don't use central widget
    Of course you use a center widget.

    because i need to draw MANY small images at their certain positions, and then show all of them as a whole structure.
    So why not draw them on a pixmap and then just apply the pixmap to the label?

    this is also the reason why i kept drawing that label, because there may be many same images.
    If you only want to draw some images, you don't need a label.

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

    Shawn (27th May 2007)

  17. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    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?

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

    Shawn (27th May 2007)

  19. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    So why not draw them on a pixmap and then just apply the pixmap to the label?
    Yes, of course that would be better, but I thought maybe he wants to interact somehow with those images.

    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

  20. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

  21. #13
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    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!

  22. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    Quote Originally Posted by Shawn View Post
    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.


    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.
    But why do you create them in when a resize event is received?
    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?

  23. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

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

    Default Re: Reimplement of resizeEvent

    Why not use QGraphicsView?

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

    Shawn (27th May 2007)

  26. #16
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    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.
    Attached Images Attached Images
    Last edited by Shawn; 27th May 2007 at 09:31.

  27. #17
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    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

  28. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

  29. #18
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reimplement of resizeEvent

    and this one is the final goal
    Attached Images Attached Images

  30. #19
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement of resizeEvent

    and this one is the final goal
    Yes, you can do this with QGraphicsView.

    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

  31. The following user says thank you to marcel for this useful post:

    Shawn (27th May 2007)

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

    Default Re: Reimplement of resizeEvent

    QGraphicsView is certainly the way to go.

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

    Shawn (27th May 2007)

Similar Threads

  1. resizeEvent help pls
    By munna in forum Newbie
    Replies: 10
    Last Post: 9th July 2010, 08:38
  2. resizeEvent from parent to child?
    By ChasW in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2007, 18:21
  3. Replies: 3
    Last Post: 27th November 2006, 09:56
  4. Reimplement QSplashScreen::drawContents
    By ucomesdag in forum Qt Programming
    Replies: 12
    Last Post: 24th November 2006, 09:39
  5. Question about resizeEvent
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 28th February 2006, 16:13

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.