#include <cmath>
#include <QtGui/QPainter>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QScrollBar>
#include <NestedWidgetResizeIssue_MWE.hh>
InnerWidget::InnerWidget() {
init_img_label(m_img_1, m_label_1);
init_img_label(m_img_2, m_label_2);
layout->addWidget(m_label_1);
layout->addWidget(m_label_2);
setLayout(layout);
adjustSize();
}
QPixmap InnerWidget
::create_black_img() { img.
fill(QColor::fromRgb(0,
0,
0));
return img;
}
void InnerWidget
::update_img_label(const QPixmap
& img,
QLabel* label
) { label->setPixmap(img);
label->setFixedSize(img.size());
}
void InnerWidget
::init_img_label(QPixmap
& img,
QLabel*& label
) { img = create_black_img();
update_img_label(img, label);
}
void InnerWidget
::zoom_img_label(double factor, QPixmap
& img,
QLabel* label
) { img = img.scaled(img.size() * factor);
update_img_label(img, label);
}
void InnerWidget::zoom(double factor) {
zoom_img_label(factor, m_img_1, m_label_1);
zoom_img_label(factor, m_img_2, m_label_2);
}
// --------------------------------------------------
MainWindow::MainWindow() :
m_edit_menu{ menuBar()->addMenu("&Edit") },
m_zoom_action
{ new QAction{ "&Zoom" } },
connect(m_zoom_action, &QAction::triggered, this, &MainWindow::zoom_inner);
m_edit_menu->addAction(m_zoom_action);
layout->setContentsMargins(0, 0, 0, 0);
for (int i = 0; i < 2; ++i) {
auto* entry = new InnerWidget;
m_entries.push_back(entry);
layout->addWidget(entry);
}
m_main->setLayout(layout);
m_central->setWidget(m_main);
setCentralWidget(m_central);
show();
update_max_width();
resize(maximumWidth(), 500);
}
void MainWindow::zoom_inner() {
bool confirm = false;
auto factor
= QInputDialog::getDouble(this,
"Zoom images",
"Factor",
1.0,
0.1,
2.0,
2,
&confirm, Qt
::WindowFlags{},
0.05);
if (!confirm) return;
for (auto* e : m_entries) {
e->zoom(factor);
e->adjustSize();
}
update_max_width();
resize(maximumWidth(), size().height());
}
void MainWindow::update_max_width() {
int max_entry_width = 0;
for (const auto* e : m_entries)
max_entry_width = std::max(max_entry_width, e->width());
setMaximumWidth(max_entry_width + m_central->verticalScrollBar()->width() + 3);
}