两个方向的QSplitter
QSplitter in two directions
我想制作一个包含四个可使用 QSplitter 调整大小的小部件的应用程序。在此应用程序中,我希望在调整拆分器大小时调整所有四个小部件的大小。我通过让水平分离器包含两个垂直分离器来实现这一点。然而,通过这种方式,垂直拆分只涉及两个小部件,而不是全部四个。有没有办法做到这一点 "matrix" 分裂?
您是否尝试过将一个的 splitterMoved(int,int)
信号连接到另一个的 moveSplitter(int,int)
插槽?
QObject::connect(ui->upperSplitter, SIGNAL(splitterMoved(int,int), ui->lowerSplitter, SLOT(moveSplitter(int,int));
QObject::connect(ui->lowerSplitter, SIGNAL(splitterMoved(int,int), ui->upperSplitter, SLOT(moveSplitter(int,int));
http://doc.qt.io/qt-5/qsplitter.html#splitterMoved
http://doc.qt.io/qt-5/qsplitter.html#moveSplitter
或者您可能需要查看 QSplitterHandle class。
http://doc.qt.io/qt-5/qsplitterhandle.html
希望对您有所帮助。
另一个答案的另一种可能性是手动布局,在四个小部件的交叉处有一个奇特的单一大小调整手柄。
应该使用鼠标事件和 setGeometry 调用通过几行代码来完成。
像这样(工作示例):
(只需添加一个paint事件即可在中心画一个手柄)
该死的.. 很明显这是按钮标签的复制粘贴错误; ) 我更正了固定代码 ...
FourWaySplitter::FourWaySplitter(QWidget *parent) :
QWidget(parent),
ui(new Ui::FourWaySplitter), m_margin(5)
{
ui->setupUi(this);
m_ul = new QPushButton("Upper Left", this);
m_ur = new QPushButton("Upper Right", this);
m_ll = new QPushButton("Lower Left", this);
m_lr = new QPushButton("Lower Right", this);
setFixedWidth(500);
setFixedHeight(400);
// of course, the following needs to be updated in a sensible manner
// when 'this' is not of fixed size in the 'resizeEvent(QResizeEvent*)' handler
m_handleCenter = rect().center();
m_ul->setGeometry(QRect(QPoint(m_margin,m_margin), m_handleCenter - QPoint(m_margin, m_margin)));
m_ur->setGeometry(QRect(QPoint(width()/2 + m_margin, m_margin), QPoint(width() - m_margin, height()/2 - m_margin)));
m_ll->setGeometry(QRect(QPoint(m_margin, height()/2 + m_margin), QPoint(width()/2 - m_margin, height() - m_margin)));
m_lr->setGeometry(QRect(QPoint(width()/2 + m_margin, height()/2 + m_margin), QPoint(width() - m_margin, height() - m_margin)));
}
void FourWaySplitter::mouseMoveEvent(QMouseEvent * e)
{
if(m_mouseMove) {
QRect newGeo = m_ul->geometry();
newGeo.setBottomRight(e->pos() + QPoint(-m_margin, -m_margin));
m_ul->setGeometry(newGeo);
newGeo = m_ur->geometry();
newGeo.setBottomLeft(e->pos() + QPoint(+m_margin, -m_margin));
m_ur->setGeometry(newGeo);
newGeo = m_ll->geometry();
newGeo.setTopRight(e->pos() + QPoint(-m_margin, + m_margin));
m_ll->setGeometry(newGeo);
newGeo = m_lr->geometry();
newGeo.setTopLeft(e->pos() + QPoint(+m_margin, + m_margin));
m_lr->setGeometry(newGeo);
}
}
void FourWaySplitter::mousePressEvent(QMouseEvent * e)
{
if((e->pos() - m_handleCenter).manhattanLength() < 10) {
m_mouseMove = true;
}
}
void FourWaySplitter::mouseReleaseEvent(QMouseEvent * e)
{
m_handleCenter = rect().center();
m_mouseMove = false;
}
FourWaySplitter::~FourWaySplitter()
{
delete ui;
}
我想制作一个包含四个可使用 QSplitter 调整大小的小部件的应用程序。在此应用程序中,我希望在调整拆分器大小时调整所有四个小部件的大小。我通过让水平分离器包含两个垂直分离器来实现这一点。然而,通过这种方式,垂直拆分只涉及两个小部件,而不是全部四个。有没有办法做到这一点 "matrix" 分裂?
您是否尝试过将一个的 splitterMoved(int,int)
信号连接到另一个的 moveSplitter(int,int)
插槽?
QObject::connect(ui->upperSplitter, SIGNAL(splitterMoved(int,int), ui->lowerSplitter, SLOT(moveSplitter(int,int));
QObject::connect(ui->lowerSplitter, SIGNAL(splitterMoved(int,int), ui->upperSplitter, SLOT(moveSplitter(int,int));
http://doc.qt.io/qt-5/qsplitter.html#splitterMoved
http://doc.qt.io/qt-5/qsplitter.html#moveSplitter
或者您可能需要查看 QSplitterHandle class。
http://doc.qt.io/qt-5/qsplitterhandle.html
希望对您有所帮助。
另一个答案的另一种可能性是手动布局,在四个小部件的交叉处有一个奇特的单一大小调整手柄。
应该使用鼠标事件和 setGeometry 调用通过几行代码来完成。
像这样(工作示例):
(只需添加一个paint事件即可在中心画一个手柄)
该死的.. 很明显这是按钮标签的复制粘贴错误; ) 我更正了固定代码 ...
FourWaySplitter::FourWaySplitter(QWidget *parent) :
QWidget(parent),
ui(new Ui::FourWaySplitter), m_margin(5)
{
ui->setupUi(this);
m_ul = new QPushButton("Upper Left", this);
m_ur = new QPushButton("Upper Right", this);
m_ll = new QPushButton("Lower Left", this);
m_lr = new QPushButton("Lower Right", this);
setFixedWidth(500);
setFixedHeight(400);
// of course, the following needs to be updated in a sensible manner
// when 'this' is not of fixed size in the 'resizeEvent(QResizeEvent*)' handler
m_handleCenter = rect().center();
m_ul->setGeometry(QRect(QPoint(m_margin,m_margin), m_handleCenter - QPoint(m_margin, m_margin)));
m_ur->setGeometry(QRect(QPoint(width()/2 + m_margin, m_margin), QPoint(width() - m_margin, height()/2 - m_margin)));
m_ll->setGeometry(QRect(QPoint(m_margin, height()/2 + m_margin), QPoint(width()/2 - m_margin, height() - m_margin)));
m_lr->setGeometry(QRect(QPoint(width()/2 + m_margin, height()/2 + m_margin), QPoint(width() - m_margin, height() - m_margin)));
}
void FourWaySplitter::mouseMoveEvent(QMouseEvent * e)
{
if(m_mouseMove) {
QRect newGeo = m_ul->geometry();
newGeo.setBottomRight(e->pos() + QPoint(-m_margin, -m_margin));
m_ul->setGeometry(newGeo);
newGeo = m_ur->geometry();
newGeo.setBottomLeft(e->pos() + QPoint(+m_margin, -m_margin));
m_ur->setGeometry(newGeo);
newGeo = m_ll->geometry();
newGeo.setTopRight(e->pos() + QPoint(-m_margin, + m_margin));
m_ll->setGeometry(newGeo);
newGeo = m_lr->geometry();
newGeo.setTopLeft(e->pos() + QPoint(+m_margin, + m_margin));
m_lr->setGeometry(newGeo);
}
}
void FourWaySplitter::mousePressEvent(QMouseEvent * e)
{
if((e->pos() - m_handleCenter).manhattanLength() < 10) {
m_mouseMove = true;
}
}
void FourWaySplitter::mouseReleaseEvent(QMouseEvent * e)
{
m_handleCenter = rect().center();
m_mouseMove = false;
}
FourWaySplitter::~FourWaySplitter()
{
delete ui;
}