Results 1 to 9 of 9

Thread: Layout problem

  1. #1
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Layout problem

    Screenshot:
    QQ截图未命名.jpg

    The m_layout has been inited like this:
    Qt Code:
    1. m_layout = new QVBoxLayout();
    2. m_layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    3. m_layout->setAlignment(Qt::AlignLeft);
    4. m_layout->setContentsMargins(3,0,0,0);
    To copy to clipboard, switch view to plain text mode 

    What my code does:
    1. Parse the data from the server.
    2. Remove the last widget if it has reach maximum count. (By calling QLayout::removeWidget(QWidget*)).
    3. Create a new widget, or reuse the removed one. Update it with the data. Resize it to proper size. Both minimumSizeHint() and sizeHint() will return this proper size.
    4. Insert the widget back to the layout.

    How can I do these:
    1. The m_parentWidget should always resize to fit the children. When I add new children. It expands, when I remove one, it shrinks.
    2. The children widgets should always resize to its proper size. As you can see from the image, the children doesn't resize properly.
    3. The code I describe above would run several times at once. How can I make sure that the layout calculate/move/resize my widgets only once after I finished removing/inserting the widgets?
    It's not the goodbye that hurts,but the flashback that follow.

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

    Default Re: Layout problem

    1. I would do a custom calc function which calculates the dimension of m_parentWidget. Then set this size. (Or see if setSizeConstraint(QLayout::SetFixedSize) does the tick for you.)
    2. For the children, react on any size event of the parent and call a function like customResize(int newWidth);
    3. Just call the parent resize function at the end of all your actions. And a vertical resize of the parent is not so time consuming. So it won't matter so much.

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

    Default Re: Layout problem

    According to me proper implementations of sizeHint() together with setting a layout constraint should do the trick without the need for any custom calculations.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Layout problem

    Quote Originally Posted by Lykurg View Post
    1. I would do a custom calc function which calculates the dimension of m_parentWidget. Then set this size. (Or see if setSizeConstraint(QLayout::SetFixedSize) does the tick for you.)
    2. For the children, react on any size event of the parent and call a function like customResize(int newWidth);
    3. Just call the parent resize function at the end of all your actions. And a vertical resize of the parent is not so time consuming. So it won't matter so much.
    If I do this, then the using layout is meaningless.

    Quote Originally Posted by wysota View Post
    According to me proper implementations of sizeHint() together with setting a layout constraint should do the trick without the need for any custom calculations.
    That's what I thought, I have already implement the sizeHint(), it returns the proper size. But the problem is that the layout seems to ignore it. So, some of my widget has been cut.
    It's not the goodbye that hurts,but the flashback that follow.

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

    Default Re: Layout problem

    Quote Originally Posted by MorrisLiang View Post
    If I do this, then the using layout is meaningless.
    Yes, but it still arranges the items for you.
    How does your sizeHint function look like (also of the child widgets)?

  6. #6
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Layout problem

    Qt Code:
    1. // Header File
    2. #ifndef MINIBLOGENTRYWIDGET_H
    3. #define MINIBLOGENTRYWIDGET_H
    4.  
    5. #include <QWidget>
    6. class QFrame;
    7. namespace Douban
    8. {
    9. class MiniBlogEntryWidget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. MiniBlogEntryWidget(MiniBlogEntry*);
    14. ~MiniBlogEntryWidget(){}
    15.  
    16. QSize sizeHint() const {return m_sizeHint;}
    17. QSize minimumSizeHint() const {return m_sizeHint;}
    18. void updateContent();
    19. void updateRelatedImg();
    20. private:
    21. MiniBlogEntry* m_entry;
    22.  
    23. QFrame* m_bgFrame;
    24. QTextBrowser* m_contentText;
    25. QTextDocument* m_contentDoc;
    26. QSize m_sizeHint;
    27. };
    28. }
    29. #endif // MINIBLOGENTRYWIDGET_H
    30.  
    31.  
    32. // Source File
    33. MiniBlogEntryWidget::MiniBlogEntryWidget(MiniBlogEntry* e):
    34. QWidget(0),
    35. m_entry(e)
    36. {
    37. setObjectName("MiniBlogEntry");
    38. m_bgFrame = new QFrame(this);
    39. m_bgFrame->setObjectName("bgFrame");
    40.  
    41. m_contentText = new QTextBrowser(this);
    42. m_contentText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    43. m_contentText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    44. connect(m_contentText,SIGNAL(anchorClicked(QUrl)),this,SLOT(onLinkClicked(QUrl)));
    45.  
    46. ensurePolished();
    47.  
    48. setMaximumWidth(minimumWidth());
    49.  
    50. m_contentDoc = new QTextDocument();
    51. m_contentText->setDocument(m_contentDoc);
    52. m_contentDoc->setTextWidth(m_contentText->width());
    53.  
    54. m_sizeHint.setWidth(minimumWidth());
    55. }
    56.  
    57. void MiniBlogEntryWidget::updateContent()
    58. {
    59. show();
    60.  
    61. //... //omit some other unimportant code.
    62.  
    63. // Set the related image and the content text
    64. QString html = Entry_HTML_TEMPLATE_IMAGE;
    65. m_contentDoc->addResource(QTextDocument::ImageResource,
    66. QUrl("relatedImage.jpg"),
    67. QVariant(m_entry->relatedImg()));
    68.  
    69. m_contentDoc->setHtml(html.arg(m_entry->content()));
    70. m_contentText->resize(m_contentText->width(),
    71. m_contentDoc->size().height());
    72.  
    73. int h = m_contentText->y() + m_contentText->height();
    74. if(h < minimumHeight())
    75. h = minimumHeight();
    76. m_sizeHint.setHeight(h);
    77.  
    78. m_bgFrame->resize(m_sizeHint);
    79. resize(m_sizeHint);
    80. }
    81.  
    82. void MiniBlogEntryWidget::updateRelatedImg()
    83. {
    84. QString html = Entry_HTML_TEMPLATE_IMAGE;
    85. m_contentDoc->addResource(QTextDocument::ImageResource,
    86. QUrl("relatedImage.jpg"),
    87. m_entry->relatedImg());
    88.  
    89. m_contentDoc->setHtml(html->arg(m_entry->content()));
    90. m_contentText->resize(m_contentText->width(),
    91. m_contentDoc->size().height());
    92.  
    93. int h = m_contentText->y() + m_contentText->height();
    94. if(h < minimumHeight())
    95. h = minimumHeight();
    96.  
    97. if(m_sizeHint.height() < h)
    98. {
    99. m_sizeHint.setHeight(h);
    100.  
    101. m_bgFrame->resize(m_sizeHint);
    102. resize(m_sizeHint);
    103. updateGeometry();
    104. }
    105. }
    To copy to clipboard, switch view to plain text mode 

    As I mentioned in the first post, I parse the data from the server, then update the data by calling "updateContent()". I resized the widget in "updateContent()" function. Then insert the widget into the layout.

    Sometimes, it will be a while before the image is downloaded. After the image is downloaded (at this time, the widget has been added into the layout) , I calc the size again.
    If the height of the widget is smaller than it needs, resize the widget.
    It's not the goodbye that hurts,but the flashback that follow.

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

    Default Re: Layout problem

    If you change sizeHint, you have to inform Qt about it by calling QWidget::updateGeometry().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Layout problem

    Quote Originally Posted by wysota View Post
    If you change sizeHint, you have to inform Qt about it by calling QWidget::updateGeometry().
    I try, it doesn't work. I call updateContent() to change the sizehint before I insert the widget to the layout
    It's not the goodbye that hurts,but the flashback that follow.

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

    Default Re: Layout problem

    Quote Originally Posted by MorrisLiang View Post
    I try, it doesn't work. I call updateContent() to change the sizehint before I insert the widget to the layout
    Did I say anything about "updateContent()"? If you ever call resize(sizeHint()) then it almost always means you are doing something wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem with layout!
    By sang in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2010, 21:01
  2. Problem with layout
    By franco.amato in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2009, 19:32
  3. QT Layout problem
    By bod in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2008, 13:29
  4. Layout problem
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2006, 23:05
  5. Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 11:08

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.