Results 1 to 8 of 8

Thread: proportional widget resize

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default proportional widget resize

    Hi,
    I have a simple question regardin widget resizes. I want my widget that has a certain aspect ratio to shrink/grow without affecting that ratio. How do I do that? I thought about heightForWidth but I am not sure and it doesn't really work.

    Thanks in advance
    momesana

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: proportional widget resize

    heightForWidth is what you are looking for. What happens if you try it?
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: proportional widget resize

    well,
    nothing happens!
    I probably have to activate set the hasWidthForHeight flag somewhere. All I know is that it doesn't work right now :-(.

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: proportional widget resize

    Yes you have to set hasHeightForWidth in the sizePolicy of your widget.
    It's nice to be important but it's more important to be nice.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: proportional widget resize

    I did but it still doesn't work. Here is the code

    Qt Code:
    1. #include <QApplication>
    2. #include <QStylePainter>
    3. #include <QStyleOption>
    4. #include <QMouseEvent>
    5. #include <QDebug>
    6. #include <cmath>
    7. #include <QWidget>
    8. #include <QList>
    9. #include <QRect>
    10.  
    11. class RgbBitMaskWidget : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. RgbBitMaskWidget(QWidget* parent = 0);
    16. ~RgbBitMaskWidget();
    17. uchar red() const;
    18. uchar green() const;
    19. uchar blue() const;
    20. virtual int heightForWidth(int) const;
    21. virtual QSize sizeHint() const;
    22. protected:
    23. virtual void resizeEvent(QResizeEvent*);
    24. virtual void paintEvent(QPaintEvent*);
    25. virtual void mousePressEvent(QMouseEvent* );
    26. signals:
    27. void rgbBitMaskChanged(int, int, int) const;
    28. private:
    29. uchar mBits[3];
    30. QList<QList<QRect> > mGeometryRects;
    31.  
    32. void recalculateRectGeometries();
    33. };
    34.  
    35. RgbBitMaskWidget::RgbBitMaskWidget(QWidget* parent)
    36. : QWidget(parent)
    37. {
    38. // initialize array
    39. for (int i = 0; i < 3; ++i)
    40. mBits[i] = 255;
    41.  
    42. // initialize list of arrays
    43. for (int i = 0; i < 3; ++i) {
    44. QList<QRect> l;
    45. for (int j = 0; j < 8; ++j)
    46. l << QRect();
    47. mGeometryRects << l;
    48. }
    49.  
    50. // set sizePolicy (important for heightForWidth)
    51. QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    52. sizePolicy.setHeightForWidth(true);
    53. setSizePolicy(sizePolicy);
    54.  
    55. const int minWidth = 150;
    56. setMinimumSize(minWidth, minWidth / (8 / 3.0));
    57. }
    58.  
    59. RgbBitMaskWidget::~RgbBitMaskWidget()
    60. {
    61. }
    62.  
    63. void RgbBitMaskWidget::recalculateRectGeometries()
    64. {
    65. const int MARGIN = 2;
    66. QRect r = contentsRect().adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
    67.  
    68. int x = 0;
    69. int y = 0;
    70. int w = r.width() / 8;
    71. int h = r.height() / 3;
    72.  
    73. for (int i = 0; i < mGeometryRects.size(); ++i) {
    74. QList<QRect> rectList = mGeometryRects[i];
    75. x = 0;
    76. for (int j = 0; j < rectList.size(); ++j) {
    77. mGeometryRects[i][j] = QRect(x, y, w, h);
    78. x += w;
    79. }
    80. y += h;
    81. }
    82. }
    83.  
    84. void RgbBitMaskWidget::resizeEvent(QResizeEvent* event)
    85. {
    86. Q_UNUSED(event);
    87. recalculateRectGeometries();
    88. }
    89.  
    90. void RgbBitMaskWidget::mousePressEvent(QMouseEvent* event)
    91. {
    92. QPoint pos = event->pos();
    93. for (int i = 0; i < 3; ++i) {
    94. for (int j = 0; j < 8; ++j) {
    95. if (mGeometryRects[i][j].contains(pos)) {
    96. uchar flag = std::pow(2, 7 - j);
    97. if (flag == (mBits[i] & flag))
    98. mBits[i] = mBits[i] & (~flag);
    99. else
    100. mBits[i] = mBits[i] | flag ;
    101. }
    102. }
    103. }
    104. QWidget::mousePressEvent(event);
    105. update();
    106. }
    107.  
    108. void RgbBitMaskWidget::paintEvent(QPaintEvent* event)
    109. {
    110. Q_UNUSED(event);
    111.  
    112. opt.init(this);
    113. QStylePainter painter(this);
    114. style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    115.  
    116. QList<QColor> colors;
    117. colors << QColor(Qt::red) << QColor(Qt::green) << QColor(Qt::blue);
    118. // paint rects
    119. QPen pen(Qt::white);
    120. pen.setWidth(3);
    121. painter.setPen(pen);
    122. for(int i = 0; i < 3; ++i) {
    123. QList<QRect> l = mGeometryRects[i];
    124. QColor c = colors[i];
    125. for (int j = 0; j < 8; ++j) {
    126. uchar flags = std::pow(2, 7 - j);
    127. if ((mBits[i] & flags) == flags)
    128. painter.setBrush(c);
    129. else
    130. painter.setBrush(c.lighter(185));
    131.  
    132. painter.drawRect(mGeometryRects[i][j]);
    133. }
    134. }
    135. }
    136.  
    137. int RgbBitMaskWidget::heightForWidth(int) const
    138. {
    139. double ratio = 8 / 3;
    140. return static_cast<int>(height() / ratio);
    141. }
    142.  
    143. QSize RgbBitMaskWidget::sizeHint() const
    144. {
    145. int w = width();
    146. return QSize(w, heightForWidth(w));
    147. }
    148.  
    149. int main(int argc, char *argv[])
    150. {
    151. QApplication a(argc, argv);
    152. RgbBitMaskWidget rgb;
    153. rgb.show();
    154. return a.exec();
    155. }
    156.  
    157. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: proportional widget resize

    I think you should not reimplement sizeHint then. But anyway maybe heightForWidth is not supported for toplevel-widgets. Reason: How should the widget be resized when the user clicks "maximize" and the display has an aspect ratio != your heightForWidth?
    It's nice to be important but it's more important to be nice.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: proportional widget resize

    I just added it to a QWidget that itself is the centralWidget of a QMainWindow. But it still loses the aspect ratio when it is resized. Btw, the widget itself is supposed to be placed into a QDockWidget later. So how can I make it work so that it keeps its aspect ratio and only grows proportionally? Any Ideas?

    Thanks in advance
    momesana

    Qt Code:
    1. #include <QtGui>
    2. #include <cmath>
    3.  
    4. class RgbBitMaskWidget : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. RgbBitMaskWidget(QWidget* parent = 0);
    9. ~RgbBitMaskWidget();
    10. uchar red() const;
    11. uchar green() const;
    12. uchar blue() const;
    13. virtual int heightForWidth(int) const;
    14. virtual QSize sizeHint() const;
    15. protected:
    16. virtual void resizeEvent(QResizeEvent*);
    17. virtual void paintEvent(QPaintEvent*);
    18. virtual void mousePressEvent(QMouseEvent* );
    19. signals:
    20. void rgbBitMaskChanged(int, int, int) const;
    21. private:
    22. uchar mBits[3];
    23. QList<QList<QRect> > mGeometryRects;
    24.  
    25. void recalculateRectGeometries();
    26. };
    27.  
    28. RgbBitMaskWidget::RgbBitMaskWidget(QWidget* parent)
    29. : QWidget(parent)
    30. {
    31. // initialize array
    32. for (int i = 0; i < 3; ++i)
    33. mBits[i] = 255;
    34.  
    35. // initialize list of arrays
    36. for (int i = 0; i < 3; ++i) {
    37. QList<QRect> l;
    38. for (int j = 0; j < 8; ++j)
    39. l << QRect();
    40. mGeometryRects << l;
    41. }
    42.  
    43. // set sizePolicy (important for heightForWidth)
    44. QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    45. sizePolicy.setHeightForWidth(true);
    46. setSizePolicy(sizePolicy);
    47.  
    48. const int minWidth = 150;
    49. setMinimumSize(minWidth, minWidth / (8 / 3.0));
    50. }
    51.  
    52. RgbBitMaskWidget::~RgbBitMaskWidget()
    53. {
    54. }
    55.  
    56. void RgbBitMaskWidget::recalculateRectGeometries()
    57. {
    58. const int MARGIN = 2;
    59. QRect r = contentsRect().adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
    60.  
    61. int x = 0;
    62. int y = 0;
    63. int w = static_cast<int>(r.width() / 8.0);
    64. int h = static_cast<int>(r.height() / 3.0);
    65.  
    66. for (int i = 0; i < mGeometryRects.size(); ++i) {
    67. QList<QRect> rectList = mGeometryRects[i];
    68. x = 0;
    69. for (int j = 0; j < rectList.size(); ++j) {
    70. mGeometryRects[i][j] = QRect(x, y, w, h);
    71. x += w;
    72. }
    73. y += h;
    74. }
    75. }
    76.  
    77. void RgbBitMaskWidget::resizeEvent(QResizeEvent* event)
    78. {
    79. Q_UNUSED(event);
    80. recalculateRectGeometries();
    81. }
    82.  
    83. void RgbBitMaskWidget::mousePressEvent(QMouseEvent* event)
    84. {
    85. QPoint pos = event->pos();
    86. for (int i = 0; i < 3; ++i) {
    87. for (int j = 0; j < 8; ++j) {
    88. if (mGeometryRects[i][j].contains(pos)) {
    89. uchar flag = std::pow(2, 7 - j);
    90. if (flag == (mBits[i] & flag))
    91. mBits[i] = mBits[i] & (~flag);
    92. else
    93. mBits[i] = mBits[i] | flag ;
    94. }
    95. }
    96. }
    97. QWidget::mousePressEvent(event);
    98. update();
    99. }
    100.  
    101. void RgbBitMaskWidget::paintEvent(QPaintEvent* event)
    102. {
    103. Q_UNUSED(event);
    104.  
    105. opt.init(this);
    106. QStylePainter painter(this);
    107. style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    108.  
    109. QList<QColor> colors;
    110. colors << QColor(Qt::red) << QColor(Qt::green) << QColor(Qt::blue);
    111. // paint rects
    112. QPen pen(Qt::white);
    113. pen.setWidth(3);
    114. painter.setPen(pen);
    115. for(int i = 0; i < 3; ++i) {
    116. QList<QRect> l = mGeometryRects[i];
    117. QColor c = colors[i];
    118. for (int j = 0; j < 8; ++j) {
    119. uchar flags = std::pow(2, 7 - j);
    120. if ((mBits[i] & flags) == flags)
    121. painter.setBrush(c);
    122. else
    123. painter.setBrush(c.lighter(185));
    124.  
    125. painter.drawRect(mGeometryRects[i][j]);
    126. }
    127. }
    128. }
    129.  
    130. int RgbBitMaskWidget::heightForWidth(int width) const
    131. {
    132. return static_cast<int>(width / (8 / 3.0));
    133. }
    134.  
    135. QSize RgbBitMaskWidget::sizeHint() const
    136. {
    137. int w = width();
    138. return QSize(w, heightForWidth(w));
    139. }
    140.  
    141. int main(int argc, char *argv[])
    142. {
    143. QApplication a(argc, argv);
    144. // central widget
    145. QWidget* cw = new QWidget;
    146. cw->setLayout(lo);
    147.  
    148. RgbBitMaskWidget* rgb = new RgbBitMaskWidget();
    149. cw->layout()->addWidget(rgb);
    150.  
    151. mw->setCentralWidget(cw);
    152. mw->show();
    153. return a.exec();
    154. }
    155.  
    156. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by momesana; 25th October 2009 at 18:19.

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

    Default Re: proportional widget resize

    Sorry to pick up the old thread, but have you found a solution?
    I failed to solve it within spacers, layouts and heightForWidth, setHeightForWidth etc. functions.
    Basically the question is how to force a widget to keep proportions under resizing its parent?

Similar Threads

  1. Resize widget force layout resizing
    By ^NyAw^ in forum Qt Programming
    Replies: 17
    Last Post: 11th February 2009, 11:27
  2. Replies: 5
    Last Post: 25th May 2008, 08:22
  3. Auto resize Widget to maximum avail. space
    By pospiech in forum Qt Programming
    Replies: 16
    Last Post: 15th April 2008, 14:47
  4. Resize a Widget
    By sonu in forum Qt Programming
    Replies: 9
    Last Post: 29th October 2007, 07:06
  5. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00

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.