31 #include <QMouseEvent> 32 #include <QPaintEvent> 35 #include <QHeaderView> 43 : QWidget(parent), parent_(parent), first_column_size_ratio_(0.5
f), color_(128, 128, 128, 64)
45 setCursor(Qt::SplitHCursor);
46 parent_->header()->setStretchLastSection(
false);
47 parent_->viewport()->installEventFilter(
this);
53 if (event->type() == QEvent::Resize)
63 const int sbw =
parent_->verticalScrollBar()->isVisible() ? 0 :
parent_->verticalScrollBar()->width();
65 const int min =
header->minimumSectionSize();
66 const int available =
parent_->viewport()->contentsRect().width();
67 const auto& content =
parent_->contentsRect();
71 if (new_column_width <= 0)
73 else if (new_column_width >= available)
74 new_column_width = available;
76 new_column_width = qBound(min,
78 available - min - sbw);
80 if (new_column_width >
header->sectionSize(0))
82 header->resizeSection(1, available - new_column_width);
83 header->resizeSection(0, new_column_width);
87 header->resizeSection(0, new_column_width);
88 header->resizeSection(1, available - new_column_width);
92 int new_x = content.x() + new_column_width - w / 2;
93 if (new_x != x() || content.height() != height())
94 setGeometry(new_x, content.y(), w, content.height());
116 if (event->button() == Qt::LeftButton)
125 if (event->buttons() & Qt::LeftButton)
127 QPoint pos_rel_parent =
parent_->mapFromGlobal(event->globalPos());
135 int available_width =
parent_->contentsRect().width();
136 int default_width = 0.5f * available_width;
138 int col0 =
static_cast<QAbstractItemView*
>(
parent_)->sizeHintForColumn(0) - default_width;
139 int col1 =
static_cast<QAbstractItemView*
>(
parent_)->sizeHintForColumn(1) - default_width;
141 if (col0 <= 0 && col1 <= 0)
143 else if (col0 + col1 <= 0)
151 QPainter painter(
this);
153 painter.drawLine(width() / 2, 0, width() / 2, height());
void mouseDoubleClickEvent(QMouseEvent *event) override
float getRatio()
Get the ratio of the parent's left column to the parent widget width.
void paintEvent(QPaintEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void updateGeometry()
Update the parent's column widths and this splitter's geometry based on first_column_size_ratio_.
void setDesiredWidth(int width)
Set desired width of first column - subject to clamping.
double min(double a, double b)
bool eventFilter(QObject *event_target, QEvent *event) override
Catch resize events sent to parent to update splitter's geometry. Always returns false.
float first_column_size_ratio_
void setRatio(float ratio)
Set the ratio of the parent's left column to the parent widget width.
SplitterHandle(QTreeView *parent=nullptr)