Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, August 2007
Problems with QSplitter::setSizes() and QSplitter::sizes() in Qt 3.3.5


Message 1 in thread

Hi,

has anybody an idea why QSplitter::setSizes() and QSplitter::sizes()
does not behave as expected in this example? I call setSizes() with the
values (178, 235, 178). When I call getSizes() afterwards I get the
values (209, 276, 105).

I attached the source code and the printf output below.

I use Qt version 3.3.5.

Thanks a lot for your help.
Regards

Sabine

***********************************************************************

int handleWidth = splitAccessScaling->handleWidth();
int maxWidth = splitAccessScaling->width();
float lower = docking->accessLowerThreshold();
float upper = docking->accessUpperThreshold();

QValueList<int> l;

int size0 = (int) ((lower*maxWidth)-(0.5*handleWidth));
int size1 = (int) (((upper-lower)*maxWidth)-handleWidth);
int size2 = (int) (((1.0-upper)*maxWidth)-(0.5*handleWidth));

l.append(size0);
l.append(size1);
l.append(size2);

printf("***  size list          : size0 = %d, size1 = %d, size2 = %d\n",
        l[0], l[1], l[2]);

QValueList<int> sz = splitAccessScaling->sizes();

printf("***  sizes before       : size0 = %d, size1 = %d, size2 = %d\n",
        sz[0], sz[1], sz[2]);

splitAccessScaling->setSizes(l);
splitAccessScaling->repaint();

sz = splitAccessScaling->sizes();

printf("***  sizes after        : size0 = %d, size1 = %d, size2 =
        %d\n\n", sz[0], sz[1], sz[2]);

************************************************************************

***  size list          : size0 = 178, size1 = 235, size2 = 178
***  sizes before       : size0 = 59, size1 = 442, size2 = 89
***  sizes after        : size0 = 209, size1 = 276, size2 = 105

--
 [ signature omitted ] 

Message 2 in thread

Gutentag Sabine,

I looked at the example and I would have some remarks/questions that may 
help your problem.

1) You set 3 values in your value list. Although the documentation is not 
very explanatory on this, should it not be 2 values? I do not think that the 
handle of the splitter counts as one of the widgets. You can actually check 
this by doing

printf("%d\n ", sz.count() );

which, when I ran a modified version of your example, gave me 2. If you need 
to set the width of the handle, you may want to use  setHandleWidth ( int ).


2) Perhaps the exact values of size0 = 178, size1 = 235 are not set. However 
it looks like
the proportion between size1 and size 2 you specified is practically 
respected.
The fraction 178/235 is pretty much 209/276. Is that not what you would 
like?

3) I always use refresh() or update() to give the widgets a little kick in 
the bum instead of repaint().

I hope this helps.


Christopher

_________________________________________________________________
Windows Live Hotmail is the next generation of MSN Hotmail.  It?s fast, 
simple, and safer than ever and best of all ? it?s still free. Try it today! 
www.newhotmail.ca?icid=WLHMENCA146

--
 [ signature omitted ] 

Message 3 in thread

Hallo Christopher,

thanks for your reply. I just verified / checked your remarks.
Here is what I found out:

> 
> I looked at the example and I would have some remarks/questions that may
> help your problem.
> 
> 1) You set 3 values in your value list. Although the documentation is
> not very explanatory on this, should it not be 2 values? I do not think
> that the handle of the splitter counts as one of the widgets. You can
> actually check this by doing
> 
> printf("%d\n ", sz.count() );
> 
> which, when I ran a modified version of your example, gave me 2. If you
> need to set the width of the handle, you may want to use  setHandleWidth
> ( int ).

I use a splitter to simulate a color gradient by combining 3 QLabels.
The left and right label have a constant color, while the label in the
middle is the gradient. By the splitter handles we scale this gradient.

Therefore, sz.count() is 3.

> 2) Perhaps the exact values of size0 = 178, size1 = 235 are not set.
> However it looks like
> the proportion between size1 and size 2 you specified is practically
> respected.
> The fraction 178/235 is pretty much 209/276. Is that not what you would
> like?

You are right. The ratio of the first two sizes seems to be what I want.
The problem is, that the 3rd size is completely ignored.

> 3) I always use refresh() or update() to give the widgets a little kick
> in the bum instead of repaint().

Unfortunately, this makes no difference at all.

So, maybe this is a bug in Qt? As they say in the API that the values
supplied for setSizes() should be the height/width that the widgets
should be resized to.

So anyway, thanks a lot for your comments.

Best
 Sabine

--
 [ signature omitted ] 

Message 4 in thread

Hi Sabine,

Sorry, I did not realize that you may have added 3 widgets to the splitter. 
I tryed a few things with the following example in a main program. And it 
seemed to work fine if I set the values before show(). The proportions are 
close to what your example wanted. Is there is a minimum size related to 
text you put in your label or the size of the font. There only seems to be a 
problem if there is some sort of minimum is set for the qlabel.



QSplitter *s1 = new QSplitter( QSplitter::Horizontal, 0 , "main" );

I tried this:

QLabel *lb =  new QLabel(  "Hello ", s1 );
QLabel *lv = new QLabel(  "Hello " , s1);
QLabel *ed = new QLabel( "Hello", s1  );


and I tried this

QLabel *lb =  new QLabel(  "Hello ", s1 );
QLabel *lv = new QLabel(  "Hello " , s1);
QLabel *ed = new QLabel( "Donaudampfschiffahrtsgesellschaftskapitän", s1  );

The proportions in the first were respected, in the second they were not.

complete example

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

    QSplitter *s1 = new QSplitter( QSplitter::Horizontal, 0 , "main" );

    a.setMainWidget( s1 );
    s1->setCaption("Qt Example - Splitters");

    QLabel *lb =  new QLabel(  " Hello ", s1 );
    QLabel *lv = new QLabel(  " Hello " , s1);
    QLabel *ed = new QLabel( " Donaudampfschiffahrtsgesellschaftskapitän", 
s1 );





    QValueList<int> l;



     int size0 = s1->height() / 3;

     int size1 = s1->height() / 5;

    int size2 = s1->height() / 3;



    l.append(size0);
    l.append(size1);
    l.append(size2);




    s1->setSizes(l);
    sz = s1->sizes();



    s1->show();
    int result = a.exec();


    delete s1;
    return result;
}

_________________________________________________________________
Get Cultured With Arts & Culture Festivals On Live Maps 
http://local.live.com/?mkt=en-ca&v=2&cid=A6D6BDB4586E357F!2010&encType=1&style=h&FORM=SERNEP

--
 [ signature omitted ] 

Message 5 in thread

Hi Christopher,

thank you again for your ideas...
> 
> Sorry, I did not realize that you may have added 3 widgets to the
> splitter. I tryed a few things with the following example in a main
> program. And it seemed to work fine if I set the values before show().
> The proportions are close to what your example wanted. Is there is a
> minimum size related to text you put in your label or the size of the
> font. There only seems to be a problem if there is some sort of minimum
> is set for the qlabel.
> 

I removed any minimum sizes for the labels, but it still does not work.

For the first call of my function within the init() function of the
corresponding ui.h file it works correctly.
That is comparable to your call before show() I think.

But we have to call this function later on to dynamically change
the proportions of the 3 labels. Then it does not work anymore...

For today I give up. Have a nice evening!

Best
 Sabine

--
 [ signature omitted ] 

Message 6 in thread

Oh Sabine,

Another point, don't forget (If you have not already done so), to go into 
your Qt-Designer and set the Size Policy of your QLabels to 
QSizePolicy::Ignored for width and height.

I hope this helps.

Christopher

_________________________________________________________________
Get Cultured With Arts & Culture Festivals On Live Maps 
http://local.live.com/?mkt=en-ca&v=2&cid=A6D6BDB4586E357F!2010&encType=1&style=h&FORM=SERNEP

--
 [ signature omitted ] 

Message 7 in thread

Hi Christopher,

Thank you again for your effort to help me.

I have tried all combinations you said and I could think of (no minimum
size, size policy 'Ignored', stretch factor 0, stretch factor1)... and
ist still does not work. I have not the faintest idea what is wrong.

I modified you example slightly to have horizontal QLabels with colors.
It works as well, also with Qt 3.3.5. So, in principle it should work
with my program as well. Maybe some layout managers have influence on this?

Sabine

Christopher Probst schrieb:
> Oh Sabine,
> 
> Another point, don't forget (If you have not already done so), to go
> into your Qt-Designer and set the Size Policy of your QLabels to
> QSizePolicy::Ignored for width and height.
> 
> I hope this helps.
> 
> Christopher
> 
> _________________________________________________________________
> Get Cultured With Arts & Culture Festivals On Live Maps
> http://local.live.com/?mkt=en-ca&v=2&cid=A6D6BDB4586E357F!2010&encType=1&style=h&FORM=SERNEP
> 
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ] 

Message 8 in thread

Hi Sabine,

I would be really interested to know what the mystery is. If you find it, 
please let me (us) know. Good luck!
If you are allowed to, can you send me your modified version of the example? 
Did you hook it up to the slider move?

Christopher

_________________________________________________________________
Show Your Messenger Buddies How You Really Feel 
http://www.freemessengeremoticons.ca/?icid=EMENCA122

--
 [ signature omitted ] 

Message 9 in thread

Hi Christopher,

I have not found the problem, but I attach you my modified version of
the example. This worked without problems...

My real application is quite complex. I even cannot edit the designer
file using the designer anymore. So, we have to edit it in a text
editor...

The connection of the handle move events (i.e. moveEvent() and
resizeEvent() of the label in the middle) to the corresponding slots
works without problems.

The other way round, it does not work (setting the position of these
handles by setSizes() in the 'cancel' slot for the dialog).

My workaround will be to create a new instance of the dialog each time I
show it....

Best
 Sabine



Christopher Probst schrieb:
> Hi Sabine,
> 
> I would be really interested to know what the mystery is. If you find
> it, please let me (us) know. Good luck!
> If you are allowed to, can you send me your modified version of the
> example? Did you hook it up to the slider move?
> 
> Christopher

/****************************************************************************
/****************************************************************************
** $Id: qt/splitter.cpp   3.3.7   edited Aug 31 2005 $
**
** Copyright (C) 1992-2005 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qapplication.h>
#include <qlabel.h>
#include <qsplitter.h>
#include <qmultilineedit.h>
#include <iostream>
using namespace std;
#include <qpainter.h>
#include <qvaluelist.h>
#include <qlayout.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qlabel.h>
#include <qtextedit.h>
#include <qdatetime.h>
#include <qcolor.h>
#include <qpixmap.h>
#include <qpainter.h>


class Test : public QWidget {
public:
    Test(QWidget* parent=0, const char* name=0, int f=0);


	void mousePressEvent ( QMouseEvent * e );
private:
	QSplitter* aSplitter;
};



Test::Test(QWidget* parent, const char* name, int f) :
    QWidget(parent, name, f)
{
  float dr, dg, db;
  float fr, fg, fb;
  QColor c_lower(Qt::red);
  QColor c_upper(Qt::blue);
  QColor color(c_lower);
  QPixmap pixAsTransition(200, 1);
  QPixmap pix;
  QPainter painter(&pixAsTransition);
  QLabel* label;

  fr = c_lower.red();
  fg = c_lower.green();
  fb = c_lower.blue();
  
  dr = (float) (c_upper.red()-c_lower.red()) / 200;
  dg = (float) (c_upper.green()-c_lower.green()) / 200;
  db = (float) (c_upper.blue()-c_lower.blue()) / 200;

  /* Colorbar for transition from full to zero scoring */
  for (int i = 0; i < 200; i++)
  {
    painter.setPen(color);
    painter.drawPoint(i, 0);
    color.setRgb((int) (fr += dr), (int) (fg += dg), (int) (fb += db));
  }
  
  QGridLayout* Form2Layout = new QGridLayout( this, 1, 1 ); 

  aSplitter = new QSplitter ( QSplitter::Horizontal, this );
  Form2Layout->addWidget( aSplitter , 0, 0 );

  QLabel *lb = new QLabel( aSplitter );
  lb->setPaletteBackgroundColor(c_lower);
 
  QLabel *lv = new QLabel( aSplitter );
  lv->setPixmap(pixAsTransition);
  lv->setScaledContents(true);
 
 
  QLabel *ed = new QLabel( aSplitter ); 
  ed->setPaletteBackgroundColor(Qt::blue);
  
  QValueList<int> l;
  l.append(200);
  l.append(200);
  l.append(200);
  aSplitter->setSizes(l);
}

void Test::mousePressEvent ( QMouseEvent * e ) 
{
	
	static int firstTry = 0;

	QValueList<int> l;
	int size0, size1, size2;
	if ( firstTry  == 0)
    	{	
		qDebug ( "I get here 1" );
		size0 = width() / 7 ;
    		size1 = width() / 2;
		size2 = width() / 7;
	
	}
	else
	{
		qDebug ( "I get here 2" );
		size0 = width() / 7 ;
    		size1 = width() / 7;
		size2 = width() / 7;
	}
	
	firstTry= ( firstTry + 1)% 2;
	
	l.append( size0 );
	l.append( size1 );
	l.append( size2 );

        aSplitter->setSizes( l );
	aSplitter->refresh();
	
}

int main( int argc, char ** argv )
{
    QApplication a( argc, argv );
    QWidget* aWidget= new Test();


    aWidget->show();
    return a.exec();

    
}


Message 10 in thread

Hi Sabine,

Well it worked in the init(), so you are almost there...Tomorrow is another 
day!
I am not exactly sure on what event you call the reproportioning of the 
widgets of the splitter. I am sending you an example where I have connected 
dynamically the setSizes to a MousePressEvent. Press on the with the mouse 
on the widget, and the splitter is reproportionned. I am using Qt 3.3.7.
Maybe this helps, that said I am sorry I could not be of more help.


Christopher

_________________________________________________________________
Put Your Face In Your Space with Windows Live Spaces 
http://spaces.live.com/?mkt=en-ca
/****************************************************************************
/****************************************************************************
** $Id: qt/splitter.cpp   3.3.7   edited Aug 31 2005 $
**
** Copyright (C) 1992-2005 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qapplication.h>
#include <qlabel.h>
#include <qsplitter.h>
#include <qmultilineedit.h>
#include <iostream>
using namespace std;
#include <qpainter.h>
#include <qvaluelist.h>
#include <qlayout.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qlabel.h>
#include <qtextedit.h>
#include <qdatetime.h>


class Test : public QWidget {
public:
    Test(QWidget* parent=0, const char* name=0, int f=0);


	void mousePressEvent ( QMouseEvent * e );
private:
	QSplitter* aSplitter;
};



Test::Test(QWidget* parent, const char* name, int f) :
    QWidget(parent, name, f)
{

	QGridLayout* Form2Layout = new QGridLayout( this, 1, 1 ); 

	aSplitter = new QSplitter ( QSplitter::Vertical, this );
	Form2Layout->addWidget( aSplitter , 0, 0 );


	QLabel *lb = new QLabel(  "Hello", aSplitter );
	QLabel *lv = new QLabel(  "Hello", aSplitter );
	QLabel *ed = new QLabel(  "Hello", aSplitter );
	 
}

void Test::mousePressEvent ( QMouseEvent * e ) 
{
	
	static int firstTry = 0;

	QValueList<int> l;
	int size0, size1, size2;
	if ( firstTry  == 0)
    	{	
		qDebug ( "I get here 1" );
		size0 = height() / 7 ;
    		size1 = height() / 2;
		size2 = height() / 7;
	
	}
	else
	{
		qDebug ( "I get here 2" );
		size0 = height() / 7 ;
    		size1 = height() / 7;
		size2 = height() / 7;
	}
	
	firstTry= ( firstTry + 1)% 2;
	
	l.append( size0 );
	l.append( size1 );
	l.append( size2 );

        aSplitter->setSizes( l );
	aSplitter->refresh();
	
}

int main( int argc, char ** argv )
{
    QApplication a( argc, argv );
    QWidget* aWidget= new Test();


    aWidget->show();
    return a.exec();

    
}