Results 1 to 4 of 4

Thread: initial size of widgets in a QSplitter

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default initial size of widgets in a QSplitter

    Hi,
    recently someone on freenode asked how to set the initial height of three Text Edits inside a QSplitter with a vertical orientation. I tried to help him but failed miserably . It was possible to set the height if you used setMaximumHeight but he wanted the size to be adjustable by the user (otherwise using a QSplitter wouldn't have made sense anyway) which isa legitimate and reasonable request after all. The top and the bottom text edits shall take up twice the height of the current font. The text edit in between should get the rest.

    I tried this and it didn't work
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QSplitter* splitter = new QSplitter(Qt::Vertical);
    8. for (int i = 0; i < 3; ++i) {
    9. QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
    10. splitter->addWidget(te);
    11. if (i % 2 == 0)
    12. te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
    13. }
    14.  
    15. mw.setCentralWidget(splitter);
    16. mw.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Any Ideas?

    Thanks in advance
    Last edited by momesana; 13th December 2009 at 02:20.

  2. #2
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: initial size of widgets in a QSplitter

    Quote Originally Posted by momesana View Post
    how to set the initial height of three Text Edits inside a QSplitter with a vertical orientation.
    QSplitter::setSizes is a way to affect to initial sizes of each widget in the QSplitter. Next is a modified version of your code where the initial sizes are 100, 150 and 200 pixels.

    If QSplitter::setSizes fails then you might want to check what are the original sizes of each widget by using QSplitter::sizes and if the original sizes are 0 then you are setting sizes too early in the construction phase!

    Qt Code:
    1. #include <QtGui>
    2. #include <QList>
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QSplitter* splitter = new QSplitter(Qt::Vertical);
    9. for (int i = 0; i < 3; ++i) {
    10. QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
    11. splitter->addWidget(te);
    12. if (i % 2 == 0)
    13. te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
    14. }
    15.  
    16. // Set the initial sizes for QSplitter widgets
    17. QList<int> sizes;
    18. sizes << 100 << 150 << 200;
    19. splitter->setSizes(sizes);
    20.  
    21. mw.setCentralWidget(splitter);
    22. mw.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: initial size of widgets in a QSplitter

    Quote Originally Posted by tsp View Post
    QSplitter::setSizes is a way to affect to initial sizes of each widget in the QSplitter. Next is a modified version of your code where the initial sizes are 100, 150 and 200 pixels.

    If QSplitter::setSizes fails then you might want to check what are the original sizes of each widget by using QSplitter::sizes and if the original sizes are 0 then you are setting sizes too early in the construction phase!

    Qt Code:
    1. #include <QtGui>
    2. #include <QList>
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QSplitter* splitter = new QSplitter(Qt::Vertical);
    9. for (int i = 0; i < 3; ++i) {
    10. QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
    11. splitter->addWidget(te);
    12. if (i % 2 == 0)
    13. te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
    14. }
    15.  
    16. // Set the initial sizes for QSplitter widgets
    17. QList<int> sizes;
    18. sizes << 100 << 150 << 200;
    19. splitter->setSizes(sizes);
    20.  
    21. mw.setCentralWidget(splitter);
    22. mw.show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Needless to say, setSizes was the first thing I tried back then ;-). The stuff didn't work as you can see with this code:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QList<int> sizes;
    8. QSplitter* splitter = new QSplitter(Qt::Vertical);
    9. for (int i = 0; i < 3; ++i) {
    10. QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
    11. splitter->addWidget(te);
    12. sizes << ((i % 2 == 0) ? te->fontMetrics().height() * 2 : te->sizeHint().height());
    13. }
    14. // Set the initial sizes for QSplitter widgets
    15. splitter->setSizes(sizes);
    16.  
    17. mw.setCentralWidget(splitter);
    18. mw.show();
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Does anyone have any ideas?

    Thanx in advance

    P.s. @tsp: The QtGui module includes QtCore which in turns includes QtList. Thus there is no need for that headerfile to be included in the example you provided.

  4. #4
    Join Date
    Aug 2010
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: initial size of widgets in a QSplitter

    Did you solve it? If yes, please post a small example.

    Regards.

Similar Threads

  1. Replies: 4
    Last Post: 11th July 2010, 10:47
  2. Adjust the initial size of a QTextEdit in a layout
    By hecinho in forum Qt Programming
    Replies: 0
    Last Post: 26th November 2009, 16:58
  3. resizing widgets depending on a main widget size
    By luf in forum Qt Programming
    Replies: 6
    Last Post: 10th October 2009, 17:13
  4. Size proportions of widgets in QSplitter
    By Boron in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 19:25
  5. Replies: 1
    Last Post: 27th March 2008, 16:10

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.