#include <vector>
#include <QtGui/QPixmap>
#include <QtWidgets/QAction>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QWidget>
#include <QtWidgets/QFrame>
class InnerWidget
: public QFrame {
static void update_img_label
(const QPixmap
& img,
QLabel* label
);
static void init_img_label
(QPixmap
& img,
QLabel*& label
);
static void zoom_img_label
(double factor, QPixmap
& img,
QLabel* label
);
public:
InnerWidget();
void zoom(double factor);
};
// --------------------------------------------------
Q_OBJECT
std::vector<InnerWidget*> m_entries;
void update_max_width();
private slots:
void zoom_inner();
public:
MainWindow();
};
#include <vector>
#include <QtGui/QPixmap>
#include <QtWidgets/QAction>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QWidget>
#include <QtWidgets/QFrame>
class InnerWidget : public QFrame {
QPixmap m_img_1;
QPixmap m_img_2;
QLabel* m_label_1;
QLabel* m_label_2;
static QPixmap create_black_img();
static void update_img_label(const QPixmap& img, QLabel* label);
static void init_img_label(QPixmap& img, QLabel*& label);
static void zoom_img_label(double factor, QPixmap& img, QLabel* label);
public:
InnerWidget();
void zoom(double factor);
};
// --------------------------------------------------
class MainWindow : public QMainWindow {
Q_OBJECT
QMenu* m_edit_menu;
QAction* m_zoom_action;
QScrollArea* m_central;
QWidget* m_main;
std::vector<InnerWidget*> m_entries;
void update_max_width();
private slots:
void zoom_inner();
public:
MainWindow();
};
To copy to clipboard, switch view to plain text mode
#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);
}
#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() {
setFrameShape(QFrame::Panel);
auto* layout = new QHBoxLayout{};
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);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
adjustSize();
}
QPixmap InnerWidget::create_black_img() {
QPixmap img(400, 400);
QPainter p(&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();
label = new QLabel{};
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" } },
m_central{ new QScrollArea },
m_main{ new QWidget } {
connect(m_zoom_action, &QAction::triggered, this, &MainWindow::zoom_inner);
m_edit_menu->addAction(m_zoom_action);
auto* layout = new QVBoxLayout{};
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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks