This fixes bug #183.
// handled, leaving the parent popup_ time to handle the change.
if (popup_form_) {
QWidget *suicidal = new QWidget();
- suicidal->setLayout(popup_form_);
+ suicidal->setLayout(popup_->layout());
suicidal->deleteLater();
}
// Repopulate the popup
- popup_form_ = new QFormLayout(popup_);
- popup_->setLayout(popup_form_);
+ widgets::QWidthAdjustingScrollArea* scrollarea = new widgets::QWidthAdjustingScrollArea();
+ QWidget* scrollarea_content = new QWidget(scrollarea);
+
+ scrollarea->setWidget(scrollarea_content);
+ scrollarea->setWidgetResizable(true);
+ scrollarea->setContentsMargins(0, 0, 0, 0);
+ scrollarea->setFrameShape(QFrame::NoFrame);
+ scrollarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ scrollarea_content->setContentsMargins(0, 0, 0, 0);
+
+ popup_->setLayout(new QVBoxLayout());
+ popup_->layout()->addWidget(scrollarea);
+ popup_->layout()->setContentsMargins(0, 0, 0, 0);
+
+ popup_form_ = new QFormLayout(scrollarea_content);
+ popup_form_->setSizeConstraint(QLayout::SetMinAndMaxSize);
+
populate_popup_form(popup_, popup_form_);
}
#include <QApplication>
#include <QDesktopWidget>
#include <QLineEdit>
+#include <QScrollBar>
+#include <QStyle>
#include <QtGui>
#include "popup.hpp"
const unsigned int Popup::ArrowOverlap = 3;
const unsigned int Popup::MarginWidth = 6;
+
+QWidthAdjustingScrollArea::QWidthAdjustingScrollArea(QWidget* parent) :
+ QScrollArea(parent)
+{
+}
+
+void QWidthAdjustingScrollArea::setWidget(QWidget* w)
+{
+ QScrollArea::setWidget(w);
+ // It happens that QScrollArea already filters widget events,
+ // but that's an implementation detail that we shouldn't rely on.
+ w->installEventFilter(this);
+}
+
+bool QWidthAdjustingScrollArea::eventFilter(QObject* obj, QEvent* ev)
+{
+ if (obj == widget() && ev->type() == QEvent::Resize) {
+ if (widget()->height() > height())
+ setMinimumWidth(widget()->width() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent));
+ else
+ setMinimumWidth(widget()->width());
+ }
+
+ return QScrollArea::eventFilter(obj, ev);
+}
+
+
Popup::Popup(QWidget *parent) :
QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
point_(),
#ifndef PULSEVIEW_PV_WIDGETS_POPUP_HPP
#define PULSEVIEW_PV_WIDGETS_POPUP_HPP
+#include <QScrollArea>
#include <QWidget>
namespace pv {
namespace widgets {
+
+// A regular QScrollArea has a fixed size and provides scroll bars when the
+// content can't be shown in its entirety. However, we want no horizontal
+// scroll bar and want the scroll area to adjust its width to fit the content
+// instead.
+// Inspired by https://stackoverflow.com/questions/21253755/qscrollarea-with-dynamically-changing-contents?answertab=votes#tab-top
+class QWidthAdjustingScrollArea : public QScrollArea
+{
+ Q_OBJECT
+
+public:
+ QWidthAdjustingScrollArea(QWidget* parent = 0);
+ void setWidget(QWidget* w);
+ bool eventFilter(QObject* obj, QEvent* ev);
+};
+
+
class Popup : public QWidget
{
Q_OBJECT