PDA

View Full Version : initial size of widgets in a QSplitter



momesana
13th December 2009, 01:10
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 :D. 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



#include <QtGui>

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QSplitter* splitter = new QSplitter(Qt::Vertical);
for (int i = 0; i < 3; ++i) {
QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
splitter->addWidget(te);
if (i % 2 == 0)
te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
}

QMainWindow mw;
mw.setCentralWidget(splitter);
mw.show();
return app.exec();
}

Any Ideas?

Thanks in advance

tsp
13th December 2009, 07:50
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!


#include <QtGui>
#include <QList>

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QSplitter* splitter = new QSplitter(Qt::Vertical);
for (int i = 0; i < 3; ++i) {
QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
splitter->addWidget(te);
if (i % 2 == 0)
te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
}

// Set the initial sizes for QSplitter widgets
QList<int> sizes;
sizes << 100 << 150 << 200;
splitter->setSizes(sizes);

QMainWindow mw;
mw.setCentralWidget(splitter);
mw.show();
return app.exec();
}

momesana
15th December 2009, 01:09
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!


#include <QtGui>
#include <QList>

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QSplitter* splitter = new QSplitter(Qt::Vertical);
for (int i = 0; i < 3; ++i) {
QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
splitter->addWidget(te);
if (i % 2 == 0)
te->resize(te->sizeHint().width(), te->fontMetrics().height() * 2);
}

// Set the initial sizes for QSplitter widgets
QList<int> sizes;
sizes << 100 << 150 << 200;
splitter->setSizes(sizes);

QMainWindow mw;
mw.setCentralWidget(splitter);
mw.show();
return app.exec();
}

Needless to say, setSizes was the first thing I tried back then ;-). The stuff didn't work as you can see with this code:


#include <QtGui>

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QList<int> sizes;
QSplitter* splitter = new QSplitter(Qt::Vertical);
for (int i = 0; i < 3; ++i) {
QTextEdit* te = new QTextEdit("Text Edit nr. " + QString::number(i));
splitter->addWidget(te);
sizes << ((i % 2 == 0) ? te->fontMetrics().height() * 2 : te->sizeHint().height());
}
// Set the initial sizes for QSplitter widgets
splitter->setSizes(sizes);

QMainWindow mw;
mw.setCentralWidget(splitter);
mw.show();
return app.exec();
}

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.

olzzen
12th July 2012, 15:41
Did you solve it? If yes, please post a small example.

Regards.