PDA

View Full Version : problem with QSplitter



tangtao_xp
23rd June 2013, 12:32
Hi, all

In my situation, I have several QFrames in the QSplitter (QFrame can be added or deleted dynamically by user). When resizing the QFrame by mouse on the QSplitter, in default, for example (only considering the horizontal orientation):

------- in default -------
before resizing

parent window width: 200
QFrame A width: 100
QFrame B width: 100

after resize:

parent window width: 200
QFrame A width: 150
QFrame B width: 50
------------------------

and I want when resize the QFrame, the parent window will grow or shrink with its child QFrame.

-------- I want ---------
before resizing

parent window width: 200
QFrame A width: 100
QFrame B width: 100

after resize:

parent window width: 250 (grow with its child)
QFrame A width: 150
QFrame B width: 100 (keep its size)
-------------------------

So, any ideas?


Thank you in advance!


Tang Tao

ChrisW67
24th June 2013, 04:23
What is stopping you from resizing the parent window?

tangtao_xp
24th June 2013, 11:12
What is stopping you from resizing the parent window?

sorry for my unclear explanation.

Actually, "resizing" mentioned above means that dragging the splitter handle and changing its children's width by mouse.


In defalut action of Qsplitter, dragging the splitter handle, one child widget will grow, the other neighbor child widget will shrink, and parent window will keep its width unchanged.

I want, when dragging the handle, one child widget will grow, but the neighbor widget will keep its width, parent window will increase width.

9181



Thank you for your reply.


Tang Tao

ChrisW67
24th June 2013, 22:41
QSplitter divides the available space (in the container it is in) between the widgets either side of it. It's not capable of changing the size of the outer container. If the user wants a larger window nothing stops them resizing the parent window; this is the standard and expected GUI behaviour.

If you want to code this unusual behaviour yourself then connect to the splitterMoved() signal and programmatically resize the container. Use the splitter stretch factors to ensure all the extra space is placed in the growing widget.

tangtao_xp
25th June 2013, 02:46
Thanks, ChrisW67

I'll try it. I will post the new problem if I meet trouble with this idea.


Tang Tao

ChrisW67
25th June 2013, 03:08
I will not be surprised if resizing the window causes the splitterMoved() signal to fire again in a nasty loop.

tangtao_xp
25th June 2013, 12:59
I will not be surprised if resizing the window causes the splitterMoved() signal to fire again in a nasty loop.

Hi, ChrisW67

I am glad to hear your far-seeing advice. Why does it cause a nasty loop?

Meanwhile, I'm working on how to get the deltaSize (how far the mouse dragging, or check the image below), which is used for resizing the parent window.

9185


Tang Tao

tangtao_xp
26th June 2013, 16:51
Hi, all

I still have problem with the QSplitter. As described below, I try to get the deltaSize, pls refer to the code below:



// in Widget.cpp
Widget::Widget(QWidget *parent)
{
layout = new QHBoxLayout(this);
mySplitter = new MySplitter(this);

frame1 = new QFrame();
frame2 = new QFrame();

mySplitter->addWidget(frame1);
mySplitter->addWidget(frame2);

layout->addWidget(mySplitter);
this->setLayout(layout);

this->resize(200, 200);

// always returns (0, 0), I don't know why.
qDebug() << mySplitter->sizes();
}

// in MySplitter.cpp
MySplitter::MySplitter(Qt::Orientation ori, QWidget *parent):
QSplitter(ori, parent)
{
if (this->parentWidget() == NULL)
return;

this->setOpaqueResize(false);
this->setChildrenCollapsible(false);

qDebug() << mySplitter->sizes();
originalSizes = this->sizes();

connect(this, SIGNAL(splitterMoved(int,int)), this, SLOT(keepSizeMove(int,int)));
}

void MySplitter::keepSizeMove(int pos, int index)
{
if (this->parentWidget() == NULL)
return;

QList<int> newSizes = this->sizes();
int originalSize = originalSizes.at(index - 1);
int newSize = newSize.at(index - 1);
int deltaSize = newSize - originalSize;

int width = this->parentWidget()->width();
int height = this->parentWidget()->height();

... ...

if (this->orientation() == Qt::Horizontal)
{
this->parentWidget()->resize(width + deltaSize, height);
}
else
{
this->parentWidget()->resize(width, height + deltaSize);
}

this->parentWidget()->repaint();
}




my problem is : "originalSizes" always get (0, 0).

I try to use "setSizes()" and "originalSizes" will get value but a wrong value.(setSizes() fails)



// in Widget.cpp
Widget::Widget(QWidget *parent)
{
... ...

this->resize(200, 200);

QList<int>sizes;
sizes << 100 << 100;
mySplitter->setSizes(sizes);

// returns (20, 21), setSizes() fails!
qDebug() << mySplitter->sizes();
}


I googled and found that none had solved yet. (refer to http://www.qtcentre.org/archive/index.php/t-26520.html)

Need help!


Thank you in advance!


Tang Tao

ChrisW67
26th June 2013, 23:27
You are constructing your MySplitter instance at line 5 of your first listing. At this point in time it has no widgets to split but you are assuming it does. Until you have added widgets to the splitter and the widget containing the splitter is shown there are no sizes.

tangtao_xp
27th June 2013, 03:16
You are constructing your MySplitter instance at line 5 of your first listing. At this point in time it has no widgets to split but you are assuming it does. Until you have added widgets to the splitter and the widget containing the splitter is shown there are no sizes.

Thank you, ChrisW67! It works! I should call sizes() until the parent widget is shown.

Now, I think I could go on with my work.



Tang Tao