Results 1 to 7 of 7

Thread: Problems with layout

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with layout

    Hi I would layout some widget inside my class derived from QFrame.
    This is the interface:

    Qt Code:
    1. class PanelInfo : public QFrame
    2. {
    3. Q_OBJECT
    4. public:
    5. PanelInfo(QWidget* parent);
    6. ~PanelInfo();
    7.  
    8. public slots:
    9. void panValueChanged(int);
    10. void volValueChanged(int);
    11.  
    12. protected:
    13. private:
    14. QLabel* m_durationLabel;
    15. QLabel* m_channelsLabel;
    16. QLabel* m_freqLabel;
    17. QLabel* m_bitsLabel;
    18.  
    19. QSlider* m_panSlider;
    20. QSlider* m_volSlider;
    21.  
    22. QDoubleSpinBox* m_freqControl;
    23. };
    To copy to clipboard, switch view to plain text mode 

    In the ctor I would layout the widget but the result is not the expected:
    Qt Code:
    1. PanelInfo::PanelInfo(QWidget* parent /* = 0 */ )
    2. : QFrame(parent)
    3. {
    4. setFrameStyle( QFrame::Box | QFrame::Raised );
    5.  
    6. setMinimumSize( QSize(120, 240) );
    7. setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    8.  
    9. /* labels */
    10. m_durationLabel = new QLabel("DURATION");
    11. m_channelsLabel = new QLabel("CHANNELS");
    12. m_freqLabel = new QLabel("FREQ");
    13. m_bitsLabel = new QLabel("BITS");
    14.  
    15. QVBoxLayout* ll = new QVBoxLayout();
    16. ll->setContentsMargins(0, 0, 0, 0);
    17. ll->setSpacing(0); //<---doesn't work in my case
    18. ll->addWidget( m_durationLabel );
    19. ll->addWidget( m_channelsLabel );
    20. ll->addWidget( m_freqLabel );
    21. ll->addWidget( m_bitsLabel );
    22.  
    23. m_panSlider = new QSlider(Qt::Horizontal);
    24. m_panSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    25. m_panSlider->setMinimum( -100 );
    26. m_panSlider->setMaximum( +100 );
    27. m_panSlider->setValue( 0 );
    28.  
    29. m_volSlider = new QSlider(Qt::Horizontal);
    30. m_volSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    31. m_volSlider->setMinimum( 0 );
    32. m_volSlider->setMaximum( +100 );
    33. m_volSlider->setValue( 100 );
    34.  
    35. /* spinbox */
    36. m_freqControl = new QDoubleSpinBox();
    37. m_freqControl->setMinimum( 0.0 );
    38. m_freqControl->setMaximum( 3.0 );
    39. m_freqControl->setValue( 1.0 );
    40. m_freqControl->setSingleStep( 0.1 );
    41. m_freqControl->setWrapping(1);
    42.  
    43. QVBoxLayout* vl = new QVBoxLayout();
    44. vl->addLayout(ll);
    45.  
    46. vl->addWidget(m_panSlider);
    47. vl->addWidget(m_volSlider);
    48.  
    49. vl->addWidget(m_freqControl);
    50. setLayout(vl);
    51.  
    52. connect(m_panSlider, SIGNAL( valueChanged(int) ), this, SLOT( panValueChanged(int) ) );
    53. connect(m_volSlider, SIGNAL( valueChanged(int) ), this, SLOT( volValueChanged(int) ) );
    54. connect(m_freqControl, SIGNAL( valueChanged(double) ),this, SLOT( freqChanged( double ) ) );
    55. }
    To copy to clipboard, switch view to plain text mode 

    The result is that I have some not desired spaces between the labels also setting setSpacing to 0px. Layout are not completely clear to me.
    In the attached image I drawn some red lines indicating the spaces I would delete.

    Best
    Attached Images Attached Images
    Franco Amato

  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: Problems with layout

    add a spacer before you insert the sliders in the second layout: QBoxLayout::addSpacerItem(). You also can play with the size policy of the widgets. But using a spacer item is the best way for you, I guess.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with layout

    there are no spaces between labels in your code and image. But those labels got stretched to fill whole area in a layout.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with layout

    Quote Originally Posted by faldżip View Post
    there are no spaces between labels in your code and image. But those labels got stretched to fill whole area in a layout.
    But I set setSpacing(0), what must I do?
    Maybe it's better If I avoid layout
    Franco Amato

  5. #5
    Join Date
    Oct 2008
    Location
    Boston
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with layout

    Did you try Lykurg's suggestion: QBoxLayout::addSpacerItem() ?

    If that didn't work for you, try adding a simple, empty QBoxLayout::addStretch() between adding your upper layout and slider widgets. This should work and will also keep you from hardcoding a "magic number" height and width for your spacer.

  6. #6
    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: Problems with layout

    Quote Originally Posted by franco.amato View Post
    But I set setSpacing(0), what must I do?
    Maybe it's better If I avoid layout
    Maybe you want to read about layouts in general and the widgets size policies in the documentation. Your problem is so basic, and your above quoted question shows, that you have a huge lack of understanding the different between "spacing between widgets" and "the spacing (=padding) inside a wiget regarding its size policy".

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with layout

    Quote Originally Posted by pezmaster31 View Post
    Did you try Lykurg's suggestion: QBoxLayout::addSpacerItem() ?

    If that didn't work for you, try adding a simple, empty QBoxLayout::addStretch() between adding your upper layout and slider widgets. This should work and will also keep you from hardcoding a "magic number" height and width for your spacer.
    Yes but QBoxLayout::addSpacerItem() take an argument. Don't know what to pass.
    QBoxLayout::addStretch(1) worked. I noticed that if I add addStretch(1 or 5 or 10 ) result doesn't change. Don't understand why
    I'm learning better layout as I'm sure they are basics of Qt.
    Franco Amato

Similar Threads

  1. QWidget layout problems
    By MarkoSan in forum Newbie
    Replies: 3
    Last Post: 12th January 2010, 10:23
  2. Problems with QMainWindow and layout
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 22:49
  3. Replies: 0
    Last Post: 25th May 2009, 10:00
  4. Replies: 7
    Last Post: 15th June 2007, 16:13
  5. Resizing problems when applying a layout
    By JimBrown in forum Newbie
    Replies: 1
    Last Post: 21st February 2007, 22:54

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.