QSplitter を使用してサイズ変更可能な 4 つのウィジェットを含むアプリを作成したいと考えています。このアプリでは、スプリッターのサイズを変更するときに、4 つのウィジェットすべてのサイズを変更したいと考えています。これは、水平スプリッターに 2 つの垂直スプリッターを含めることで実現しました。ただし、この方法では、垂直方向の分割は 2 つのウィジェットのみに関係し、4 つすべてには関係しません。この「マトリックス」分割への方法はありますか?
1218 次
2 に答える
1
他の答えに対する別の可能性は、4 つのウィジェットが交差するところに派手な単一のサイズ変更ハンドルを備えた手動レイアウトです。
マウス イベントと setGeometry 呼び出しを使用して、数行のコードで実行する必要があります。
このように(実際の例):
(ペイントイベントを追加して、好きなように中央にハンドルを描画するだけです)
くそー..明らかに、ボタンラベルのコピーアンドペーストエラーでした。)コードを修正しました...
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;
}
于 2015-12-10T14:21:13.617 に答える
1
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 クラスを確認する必要がある場合もあります。
http://doc.qt.io/qt-5/qsplitterhandle.html
それが役立つことを願っています。
于 2015-12-10T13:55:30.670 に答える
