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
{
mySplitter = new MySplitter(this);
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
):{
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();
}
// 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();
}
To copy to clipboard, switch view to plain text mode
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
{
... ...
this->resize(200, 200);
QList<int>sizes;
sizes << 100 << 100;
mySplitter->setSizes(sizes);
// returns (20, 21), setSizes() fails!
qDebug() << mySplitter->sizes();
}
// 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();
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks