Results 1 to 5 of 5

Thread: Positioning elements in QScrollArea

  1. #1
    Join Date
    Jan 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question Positioning elements in QScrollArea

    First of all, hello everyone!

    I'm not a native speaker, so really sorry for my english. I'll do my best to be as clear as only possible.

    I've started my journey with Qt few days ago, so I'm not very experienced with it. I'm trying to make simple application with QScrollArea and QTextEdit fields.
    I want to add QPushButtons to QScrollArea dynamically - on button press. Everything works fine, but positioning elements in QScrollArea is ruined when there is less than few buttons added (everything explained in screenshot in attachments). I've tried to setContentsMargins(0, 0, 0, 0) and setAligment(Qt::AlignTop), but it doesn't work as I thought it will be.

    Here is my code where I'm adding QScrollArea:
    Qt Code:
    1. numberOfGridBoxRows = 0;
    2.  
    3. gridGroupBox = new QGroupBox(tr("Próbki"));
    4. layout = new QGridLayout;
    5.  
    6. samplesWidget = new QWidget();
    7. samplesButtonsLayout = new QGridLayout();
    8. samplesButtonsLayout->setContentsMargins(0, 0, 0, 0);
    9. samplesWidget->setLayout(samplesButtonsLayout);
    10.  
    11. samplesScroller = new QScrollArea();
    12. samplesScroller->setWidgetResizable(true);
    13. samplesScroller->setContentsMargins(0, 0, 0, 0);
    14. samplesScroller->setAlignment(Qt::AlignTop);
    15. samplesScroller->setWidget(samplesWidget);
    16.  
    17. smallEditor = new QTextEdit;
    18. smallEditor->setPlainText(tr(""));
    19. smallEditor->setMinimumWidth(300);
    20. smallEditor->setMinimumHeight(300);
    21.  
    22. layout->addWidget(samplesScroller, 0, 0, 1, 1);
    23. layout->addWidget(smallEditor, 0, 1, 1, 1);
    24.  
    25. layout->setColumnStretch(0, 100);
    26. layout->setColumnMinimumWidth(0, 150);
    27. layout->setColumnStretch(1, 60);
    28.  
    29. gridGroupBox->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 
    And here is code where I'm adding QPushButtons to QScrollArea widget (part of another function):
    Qt Code:
    1. // I've changed parameters in button constructor here, because normally as a button name is value provided by user
    2. QPushButton *showSampleButton = new QPushButton(tr("button"), this);
    3. samplesScroller->ensureWidgetVisible(samplesWidget, 0, 0);
    4. samplesButtonsLayout->addWidget(showSampleButton, numberOfGridBoxRows++, 0, 1, 1, Qt::AlignTop);
    To copy to clipboard, switch view to plain text mode 

    I'll be very thankfull for help.
    Best regards,
    Ironus
    question.jpg

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Positioning elements in QScrollArea

    Add a vertical spacer below the buttons. Start with the spacer only and insert buttons before the spacer. You reach the same effect as if you added buttons to the end.

  3. The following user says thank you to Radek for this useful post:

    Ironus (29th January 2015)

  4. #3
    Join Date
    Jan 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Positioning elements in QScrollArea

    I've changed QGridLayout to QVBoxLayout and added spacer as you suggested.
    Works like a charm! Thank you for quick answer!

    Is there any possibility to change spacing between buttons? I've set contents margin to (0, 0, 0, 0), but it doesn't change anything.

    Best regards,
    Ironus
    question.png

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

    Default Re: Positioning elements in QScrollArea

    Quote Originally Posted by Ironus View Post
    Is there any possibility to change spacing between buttons?
    Yes. Adjust the spacing property for the layout.
    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.


  6. The following user says thank you to wysota for this useful post:

    Ironus (29th January 2015)

  7. #5
    Join Date
    Jan 2015
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Positioning elements in QScrollArea

    Och, ok, silly me. I checked documentation only for QVBoxLayout and there was only setContentsMargin(0,0,0,0);
    I haven't thought about checking QLayout documenation.
    Now setSpacing works!
    Thank you for help!

Similar Threads

  1. Qt 5.2 (beta) - new Positioning API
    By bma in forum Qt Programming
    Replies: 0
    Last Post: 16th November 2013, 21:33
  2. Positioning of QFormLayout
    By Julieng031 in forum Qt Programming
    Replies: 4
    Last Post: 29th August 2013, 10:26
  3. Qt StyleSheet positioning
    By AlexCoder in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2012, 11:23
  4. Replies: 2
    Last Post: 10th March 2008, 20:16
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

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.