Results 1 to 8 of 8

Thread: resizeEvent - resizing unproportionally

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default resizeEvent - resizing unproportionally

    Hello!

    I've wrote my widget (which is something like plot). When I put it in layout it works fine. Widget is square (height and width is the same). When layout isn't square (for example width is longer than height) widget stays squared (height=width). And when layout is bigger then widget it stays in size that it was drawn (only when layout is smaller then widget it goes smaller). Can I change it somehow? Or is it some kind of limitation? My resizeEvent code:

    Qt Code:
    1. void WykresSlupkowy::resizeEvent(QResizeEvent *event)
    2. {
    3. QWidget::resizeEvent(event);
    4. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Feb 2010
    Posts
    68
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Default Re: resizeEvent - resizing unproportionally

    You didn't tell us how do you want it exactly to behave. So far your reimplemented resizeEvent() seems to do nothing but calling a standard QWidget resizeEvent, it might not be there as well and the effect would be the same.

    BTW: Cześć .

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: resizeEvent - resizing unproportionally

    So I want my widget to be same size as layout (for example QHBoxLayout). If i set it for example:

    Qt Code:
    1. horizontalLayoutWidget->setGeometry(QRect(QPoint(100,100),QPoint(300,300)));
    To copy to clipboard, switch view to plain text mode 

    I want my widget to resize to that dimensions. And one more thing, I've got in my code some lines that I don't understand (get it from some program):

    Qt Code:
    1. int side = qMin(width(), height());
    2.  
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5. painter.scale(side / 256.0, side / 256.0);
    To copy to clipboard, switch view to plain text mode 

    I think it has something with resize. Maybe I should change something here?

    thanks in advance
    best regards
    Tomasz

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 20 Times in 20 Posts

    Default Re: resizeEvent - resizing unproportionally

    I think you're mixing things up here. It sounds like you want to resize your widget, but resizeEvent() is what get's called when the widget is resized, not what you call to do the resizing (since that would cause infinite recursion). Does the following basically sum up the logic of what you want to do?

    Qt Code:
    1. QRect r = yourLayout.geometry();
    2. yourWidget.resize(r.width(), r.height());
    To copy to clipboard, switch view to plain text mode 

    With regards to the painter code that you mention, it says the following in QWidget's resizeEvent() documentation: "The widget will be erased and receive a paint event immediately after processing the resize event. No drawing need be (or should be) done inside this handler."

    I apologize if this isn't helpful or, God forbid, leads you further astray. As a newbie perhaps I should just keep my mouth shut but maybe someone will correct me and I'll learn too. :P

    edit: By the way, why ARE you trying to resize a widget based on the layout? Usually a widget is resized by the user directly (as in dragging a window's borders around) or by the layout manager in response to such direct action, or as more widgets are added to the layout. The fact that layouts don't have size/resize/resizeEvent methods is not accidental. I think that most of the time you're not supposed to be concerned with a layout's size...
    Last edited by Urthas; 13th August 2010 at 23:54.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: resizeEvent - resizing unproportionally

    Quote Originally Posted by Tomasz View Post
    I've got in my code some lines that I don't understand (get it from some program):

    Qt Code:
    1. int side = qMin(width(), height());
    2.  
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5. painter.scale(side / 256.0, side / 256.0);
    To copy to clipboard, switch view to plain text mode 

    I think it has something with resize. Maybe I should change something here?
    Yes, because that is one possible reason why your widget always show as a square. Before you copy without thinking, and then waste your time by dealing with (nonsense) layout - widget relation and try there dirty hacks, you should spend some time to understand the copied code.
    By seeing the whole paint event your problem could be clearer for us.

  6. #6
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: resizeEvent - resizing unproportionally

    Quote Originally Posted by Urthas View Post
    Does the following basically sum up the logic of what you want to do?
    Qt Code:
    1. QRect r = yourLayout.geometry();
    2. yourWidget.resize(r.width(), r.height());
    To copy to clipboard, switch view to plain text mode 
    Ok. @Lykurg is right - I should use code that I dont't understand. I've read class reference about it. I think it's not what I'm looking for. I've tried code above but it doesn't work. My widget looks like this:

    Qt Code:
    1. WykreSlupkowy::WykresSlupkowy(*QWidget parent) : QWidget(parent)
    2. {
    3. }
    4.  
    5. void WykresSlupkowy::paintEvent(QPaintEvent *event)
    6. {
    7. paintMyWidget();
    8. QWidget::paintEvent(event);
    9. }
    10.  
    11. void WykresSlupkowy::resizeEvent(QResizeEvent *event)
    12. {
    13. QWidget::reziseEvent(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

    So what should I do to get size I want?

    thanks in advance
    best regards
    Tomasz

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: resizeEvent - resizing unproportionally

    The code does not help much. We need to see how you make your drawing. But before: Your widget stays a square or your drawing?

    to determine you can use:
    Qt Code:
    1. void WykresSlupkowy::paintEvent(QPaintEvent *event)
    2. {
    3. paintMyWidget();
    4. QWidget::paintEvent(event);
    5. QPainter p(this);
    6. p.setPen(QPen(Qt::red, 1));
    7. p.drawRect(rect().adjusted(0, 0, -1, -1));
    8. }
    To copy to clipboard, switch view to plain text mode 
    So is the red border always a square?

  8. #8
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: resizeEvent - resizing unproportionally

    Without scaling thing my widget stays the size I drew it. Code posted by @Lykurg draws rectangle of my layout size like I want. Code of my widget looks like this:

    Qt Code:
    1. #include "slupkowy.h"
    2.  
    3. WykresSlupkowy::WykresSlupkowy(QWidget *parent) : QWidget(parent)
    4. {
    5.  
    6. }
    7.  
    8. void WykresSlupkowy::paintEvent(QPaintEvent *event)
    9. {
    10. narysujWykres();
    11. QWidget::paintEvent(event);
    12. QPainter p(this);
    13. p.setPen(QPen(Qt::red, 1));
    14. p.drawRect(rect().adjusted(0, 0, -1, -1));
    15. }
    16.  
    17. void WykresSlupkowy::resizeEvent(QResizeEvent *event)
    18. {
    19. QWidget::resizeEvent(event);
    20. }
    21.  
    22. void WykresSlupkowy::ustawNazwePliku(QString nazwa)
    23. {
    24. nazwaPlikuDanych = nazwa;
    25. }
    26.  
    27. void WykresSlupkowy::odczytajDane()
    28. {
    29. QFile plikDanych(nazwaPlikuDanych);
    30. QString line;
    31. bool ok;
    32. int nrDanej = 0;
    33.  
    34. if (plikDanych.open(QFile::ReadOnly))
    35. {
    36. QTextStream in(&plikDanych);
    37. QTextStream out(stdout);
    38. while ( !in.atEnd() )
    39. {
    40. line=in.readLine();
    41. out << line << " ";
    42. dane[nrDanej]=line.toInt(&ok,10);
    43. nrDanej++;
    44. }
    45.  
    46. }
    47. }
    48.  
    49. void WykresSlupkowy::rysujOsie()
    50. {
    51. int side = qMin(width(), height());
    52. int wykresX, wykresY;
    53.  
    54. QPainter painter(this);
    55. painter.setRenderHint(QPainter::Antialiasing);
    56. //painter.scale(256.0, side / 256.0);
    57.  
    58. painter.setPen(QColor("#000000"));
    59. painter.setBrush(QColor("#000000"));
    60.  
    61. //os X
    62. painter.drawLine(QPoint(14,151),QPoint(234,151));
    63. wykresX=20;
    64. for(int i=0; i<24; i++)
    65. {
    66. painter.drawLine(QPoint(wykresX+(i*9),149),QPoint(wykresX+(i*9),153));
    67. }
    68.  
    69. //os Y
    70. painter.drawLine(QPoint(14,151),QPoint(14,0));
    71. wykresY=151;
    72. for(int i=1; i<15; i++)
    73. {
    74. painter.drawLine(QPoint(12,wykresY-(i*10)),QPoint(16,wykresY-(i*10)));
    75. }
    76.  
    77. //groty strzalek
    78. QPoint strzalka[3] = {
    79. QPoint(14,0),
    80. QPoint(12,5),
    81. QPoint(16,5)
    82. };
    83.  
    84. painter.drawPolygon(strzalka, 3);
    85.  
    86. strzalka[0]=QPoint(234,151);
    87. strzalka[1]=QPoint(229,149);
    88. strzalka[2]=QPoint(229,153);
    89.  
    90. painter.drawPolygon(strzalka, 3);
    91.  
    92. //liczby na osiach
    93. QFont serifFont( "Times", 4);
    94. painter.setFont(serifFont);
    95.  
    96. for(int i=1; i<15; i++)
    97. {
    98. painter.drawText(0,154-(i*10),QString::number(i*10));
    99. }
    100.  
    101. for(int i=1; i<25; i++)
    102. {
    103. painter.drawText(18+((i-1)*9),165,QString::number(i));
    104. }
    105. }
    106.  
    107. void WykresSlupkowy::narysujWykres()
    108. {
    109. int side = qMin(width(), height());
    110.  
    111. QTextStream out(stdout);
    112. out << side ;
    113. for(int i=0; i<24; i++)
    114. {
    115. dane[i]=i+30+(i*2);
    116. }
    117.  
    118. QPainter painter(this);
    119. painter.setRenderHint(QPainter::Antialiasing);
    120. //painter.scale(side / 256.0, side / 256.0);
    121. painter.setPen(QColor("#ffffff"));
    122. painter.setBrush(QColor("#ff0000"));
    123. painter.setBrush(Qt::SolidPattern);
    124.  
    125. int wykresX=15;;
    126.  
    127. for(int i=0; i<24; i++)
    128. {
    129. painter.setBrush(QColor(255,0,0));
    130. painter.drawRect(QRect(QPoint(wykresX,150),QPoint(wykresX+8,150-dane[i])));
    131. wykresX+=9;
    132. }
    133.  
    134. rysujOsie();
    135. }
    To copy to clipboard, switch view to plain text mode 

    It paints simple plot. Data is from text file. Only WykresSlupkowy::narysujWykres() and WykresSlupkowy::rysujOsie() paints something. Any suggestions how should I write my widget to resize it how i want?

    thanks in advance
    best regards
    Tomasz

Similar Threads

  1. resizeEvent help pls
    By munna in forum Newbie
    Replies: 10
    Last Post: 9th July 2010, 09:38
  2. Replies: 3
    Last Post: 2nd March 2010, 21:58
  3. What cannot be done in resizeEvent(..)?
    By nifei in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2008, 03:48
  4. Reimplement of resizeEvent
    By Shawn in forum Qt Programming
    Replies: 20
    Last Post: 27th May 2007, 11:04
  5. Question about resizeEvent
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 28th February 2006, 17: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
  •  
Qt is a trademark of The Qt Company.