PDA

View Full Version : QSpitter - prevent resizing



jonks
27th July 2009, 02:42
Hi

How do I lock the position of a QSplitter so that it cannot be moved?

Thanks

nish
27th July 2009, 03:12
splitters are especially designed to be moved.. if you want fixed position that why not use just a simple layout?

anyways... try to catch the splitterMoved() signal in a slot and then set back to its original position by calling moveSplitter().

jonks
27th July 2009, 05:04
splitters are especially designed to be moved.. if you want fixed position that why not use just a simple layout?

I want the user to be able to move the splitter, but also be able to lock it's position too.
The widgets on either side are designed to be resizable to suit the needs of the user.


anyways... try to catch the splitterMoved() signal in a slot and then set back to its original position by calling moveSplitter().

Nah - doesn't work unfortunately.

numbat
28th July 2009, 20:36
One way is to eat the mouse move event of the splitter handle. There are probably more elegant methods though. Here's a little implementation I made:


/* Header. */
#include <QSplitterHandle>
class Splitter : public QSplitter
{
public:
Splitter();
void setAllowMove(bool allow);
bool allowMove();

protected:
QSplitterHandle *createHandle();

private:
bool m_allowMove;
};

class SplitterHandle : public QSplitterHandle
{
public:
SplitterHandle(Qt::Orientation orientation, QSplitter* parent);

protected:
void mouseMoveEvent ( QMouseEvent * event );
};


#include <QListView>
#include <QTreeView>
#include <QSplitter>
#include <QDirModel>
#include <QDebug>

/* Demonstration. */
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
Splitter *splitter = new Splitter;

QListView *lv1 = new QListView;
QListView *lv2 = new QListView;

splitter->addWidget(lv1);
splitter->addWidget(lv2);

splitter->setAllowMove(false); /* Use as required. */

setCentralWidget(splitter);
}



/* Splitter implementation. */
Splitter::Splitter() : QSplitter(), m_allowMove(true) {}

bool Splitter::allowMove()
{
return m_allowMove;
}

void Splitter::setAllowMove(bool allow)
{
m_allowMove = allow;
}

/* SplitterHandle implementation. */
SplitterHandle::SplitterHandle(Qt::Orientation orientation, QSplitter* parent) :
QSplitterHandle(orientation, parent)
{}

QSplitterHandle* Splitter::createHandle()
{
return new SplitterHandle(orientation(), this);
}

void SplitterHandle::mouseMoveEvent(QMouseEvent * event)
{
if (static_cast<Splitter*>(splitter())->allowMove())
QSplitterHandle::mouseMoveEvent(event);
else
return;
}