Results 1 to 7 of 7

Thread: Dynamically resize spacing

  1. #1
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Dynamically resize spacing

    I have a QHBoxLayout that contains a fixed size spacer, a widget and another fixed size spacer. I connected the resizing signal of another widget to a slot where I would like to update the width of the first spacer, but I cannot find a way to do it. How can I update the width of a spacer that is already in the layout?

    I tried creating my own QSpacerItems andd add them to the layout using addItem(), so later on I can call their changeSize(), but it does not have any effect.

    Any ideas?

    Thanks

  2. #2
    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: Dynamically resize spacing

    From Assistant:
    Note that if changeSize() is called after the spacer item has been added to a layout, it is necessary to invalidate the layout in order for the spacer item's new size to take effect.

  3. #3
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Dynamically resize spacing

    I think it is a refresh problem. The spacers do not resize when I call changeSize, but if then I change the main window size or the size of the splitter where my widgets are, it instantly updates and the spacers are resized.
    I tried calling repaint() and even updateGeometry() after the call to changeSize(), but it doesn't make a difference. How can I force this update? What does the resizeEvent do that repaint() and updateGeometry() don't?

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

    Default Re: Dynamically resize spacing

    Try calling invalidate() on the layout.
    Qt Code:
    1. widget->layout()->invalidate();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Dynamically resize spacing

    Thanks, tried that but still does not work
    Need a way to trigger an update similar to the one that is done during resizeEvent()

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

    Default Re: Dynamically resize spacing

    I gave it a quick try and it seems to work at least for a top level widget:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Window : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. Window()
    8. {
    9. int spacing = 20;
    10.  
    11. QSpinBox* spinbox = new QSpinBox(this);
    12. spinbox->setStyleSheet("background: red");
    13. spinbox->setValue(spacing);
    14.  
    15. QLabel* label = new QLabel(this);
    16. label->setStyleSheet("background: blue");
    17. label->setNum(spacing);
    18.  
    19. QVBoxLayout* layout = new QVBoxLayout(this);
    20. spacer = new QSpacerItem(spacing, spacing);
    21. layout->addWidget(spinbox);
    22. layout->addItem(spacer);
    23. layout->addWidget(label);
    24.  
    25. connect(spinbox, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
    26. connect(spinbox, SIGNAL(valueChanged(int)), this, SLOT(setSpacing(int)));
    27. }
    28.  
    29. private slots:
    30. void setSpacing(int spacing)
    31. {
    32. spacer->changeSize(spacing, spacing);
    33. layout()->invalidate();
    34. }
    35.  
    36. private:
    37. QSpacerItem* spacer;
    38. };
    39.  
    40. int main(int argc, char* argv[])
    41. {
    42. QApplication a(argc, argv);
    43. Window w;
    44. w.show();
    45. return a.exec();
    46. }
    47.  
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I suppose the spacing is not homogeneous over the layout? Otherwise you could simply use QBoxLayout::setSpacing().
    J-P Nurmi

  7. #7
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Dynamically resize spacing

    It is a quite complex layout setup, with nested layouts (I'm sorry but I cannot post the code). There is a Q3ListView bellow and I want to set the spacer width to match that of one column of the list view. I think the resizing is done internally, but it never gets refreshed to the GUI. If I manually resize the window in the GUI, then magically everything gets to it's required size.

    I finally solved it by calling hide() and then show(), forcing a full refresh. Not a very elegant solution but it works.

Similar Threads

  1. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32

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.