Results 1 to 7 of 7

Thread: Dynamically resize spacing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: 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

  2. #2
    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, 00: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
  •  
Qt is a trademark of The Qt Company.