Results 1 to 5 of 5

Thread: Automatically resizing custom QWidget based on runtime specifications

  1. #1
    Join Date
    Aug 2013
    Location
    Texas, U.S.A.
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question Automatically resizing custom QWidget based on runtime specifications

    I have a custom QWidget that displays some number of QSliders, depending on the labels passed into its constructor:

    Qt Code:
    1. NodeEditBox(QWidget* _parent, vector<string> _sliderLabels);
    To copy to clipboard, switch view to plain text mode 

    So, for example, the box would look like this if 4 labels "X", "Y", "Z", and "theta" were passed in:

    ExpandedNode.png

    I want to automatically resize the NodeEditBox depending on the number of sliders it will have. One way that works is just doing this right in the beginning of the constructor body:

    Qt Code:
    1. resize(450, 75*(_sliderLabels.size()));
    To copy to clipboard, switch view to plain text mode 

    ...But it doesn't always scale quite right and can look sloppy (e.g., what works nicely for 3 sliders doesn't work for one.) Is there a way I can be more clean about it and just require that the NodeEditBox always displays all of its widgets?
    Last edited by nicole.cpp; 26th August 2013 at 23:47.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Automatically resizing custom QWidget based on runtime specifications

    override resizeEvent of the window and use setGeometry(x,y, fixedWidth, sliderSize()) .
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Automatically resizing custom QWidget based on runtime specifications

    Is there a way I can be more clean about it and just require that the NodeEditBox always displays all of its widgets?
    If you are using a layout then this will be default behaviour, by default layouts will adjust such that all the widgets (1 slider/3 slliders) fit into the available space.

    1. Show us the actual screen shot of the widget where 1 slider is looks sloppy.
    2. By "sloppy" do you mean that widgets are not snugly fitted?
    3. Where is this custom widget loaded? Onto to scroll area? or onto another layout? or onto another widget directly (without layout)?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Automatically resizing custom QWidget based on runtime specifications

    This may help. On the vertical layout the sliders are in:
    Qt Code:
    1. layout->setSizeConstraint(QLayout::SetFixedSize);
    To copy to clipboard, switch view to plain text mode 
    and the widget's size will track the sizeHint() which is, in turn, derived from the size hints of the sliders. This will also lock the horizontal size but you can force a workable size with setMinimumSize() on the sliders.
    Qt Code:
    1. class Widget: public QWidget {
    2. Q_OBJECT
    3. public:
    4. explicit Widget(QWidget *p, const QStringList &labels): QWidget(p) {
    5. QVBoxLayout *layout = new QVBoxLayout(this);
    6. layout->setSizeConstraint(QLayout::SetFixedSize);
    7. foreach(const QString &label, labels) {
    8. QSlider *s = new QSlider(Qt::Horizontal, this);
    9. s->setMinimumWidth(200);
    10. layout->addWidget(s);
    11. }
    12. setLayout(layout);
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2013
    Location
    Texas, U.S.A.
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Automatically resizing custom QWidget based on runtime specifications

    Here is what happens in a typical case with my current code: it works for 2 or 3, but obscures one (I'll worry about the giant set button later...ha ha). When I think about it, it makes sense; factoring in the top area makes all the difference:
    one.jpgtwo.pngthree.png

    As for the constructor, it is like this. The m_nodeLabel is set by a different function called after construction:

    Qt Code:
    1. 18 NodeEditBox::NodeEditBox(QWidget* _parent, vector<string> _sliderLabels){
    2. 19
    3. 20 resize(450, 90*(_sliderLabels.size()));
    4. 21 setWindowTitle("Modify Node");
    5. 22
    6. 23 m_layout = new QVBoxLayout();
    7. 24
    8. 25 m_nodeLabel = new QLabel(this);
    9. 26 m_nodeLabel->setGeometry(QRect(10, 10, 67, 21));
    10. 27 m_layout->addWidget(m_nodeLabel);
    11. 28
    12. 29 for(size_t i = 0; i < _sliderLabels.size(); i++){
    13. 30 NodeEditSlider* s = new NodeEditSlider(this, _sliderLabels[i]);
    14. 31 m_sliders.push_back(s);
    15. 32 m_layout->addWidget(s);
    16. 33 }
    17. 34
    18. 35 m_setButton = new QPushButton(this);
    19. 36 m_setButton->setText("Set");
    20. 37 m_layout->addWidget(m_setButton);
    21. 38
    22. 39 this->setLayout(m_layout);
    23. 40 }
    To copy to clipboard, switch view to plain text mode 

    Here is the NodeEditSlider widget constructor. I made it on the QtDesigner and then pasted the code into my program and modified it as needed (removing retranslate UI business, etc...) :

    Qt Code:
    1. 3 NodeEditSlider::NodeEditSlider(QWidget* _parent, string _label){
    2. 4
    3. 5 m_slider = new QSlider(this);
    4. 6 m_slider->setGeometry(QRect(50, 10, 371, 20));
    5. 7 m_slider->setOrientation(Qt::Horizontal);
    6. 8
    7. 9 m_label = new QLabel(this);
    8. 10 m_label->setGeometry(QRect(10, 10, 41, 21));
    9. 11
    10. 12 QString label = QString::fromStdString(_label);
    11. 13 m_label->setText(label);
    12. 14
    13. 15 }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 48 minutes:
    Last edited by nicole.cpp; 27th August 2013 at 16:37.

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2012, 01:10
  2. Problem with automatically resizing label text
    By glennr in forum Qt Programming
    Replies: 0
    Last Post: 4th January 2012, 22:11
  3. Replies: 2
    Last Post: 27th July 2011, 15:49
  4. QWidget stylesheets not right in runtime
    By Denarius in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2009, 11:21
  5. Resizing any kind of widget runtime
    By vajindarladdad in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2008, 16:02

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.