QSplitter in two directions

强颜欢笑 提交于 2019-12-12 02:24:18

问题


I want to make an app that includes four widgets that are resizable using QSplitter. In this app I would like that all four widgets are resized when I resize the splitter. I realised this by having a horizontal splitter contain two vertical splitters. This way however the vertical splitting only concerns two widgets and not all four. Is there a way to to this "matrix" splitting?


回答1:


Have you tried connecting the splitterMoved(int,int) signal of one to the moveSplitter(int,int) slot of the other?

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

Or you may have to look at the QSplitterHandle class.

http://doc.qt.io/qt-5/qsplitterhandle.html

Hope that helps.




回答2:


Another possibility to the other answer would be a manual layout with a fancy single resizing handle in the crossing of the four widgets.

Should be done with a couple lines of code using mouse events and setGeometry calls.

Like this (working example) :

(simply add a paint event to draw a handle in the center as you like)

Damn it .. obviously the was a copy'n'paste error with the button labels ; ) I corrected fixed the code ...

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;
}


来源:https://stackoverflow.com/questions/34202805/qsplitter-in-two-directions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!