Moving UI settings to a new class
-
Hi,
I wanted to move all UI settings in a new class. That include custom style settings, resizeEvent, resizeImage, etc. The problem with the code below is that it isn't really linked to QMainWindow. The only way I can set the stuff through
Derivedclass is by I getting the data from parent, e.g.parent->setFixedSize(100, 100);. Even if I do that, the resizeEvent won't work because that's not the right way to do that. How could I do that? What's the right way?TestQt::TestQt(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); Derived *derived = new Derived(this); setCentralWidget(derived); }Derived.h
#include <QColor> #include <QImage> #include <QPoint> #include <QWidget> #include <QPainter> #include <QSize> class Derived : public QWidget { Q_OBJECT public: Derived(QWidget* parent = Q_NULLPTR); protected: void resizeEvent(QResizeEvent* event) override; private: void resizeImage(QImage* image, const QSize& newSize); QImage image; };Derived.cpp
#include "Derived.h" Derived::Derived(QWidget* parent) : QWidget(parent) { setAttribute(Qt::WA_StaticContents); setFixedSize(100, 100); } void Derived::resizeImage(QImage* image, const QSize& newSize) { if (image->size() == newSize) return; QImage newImage(newSize, QImage::Format_RGB32); newImage.fill(qRgb(0, 0, 0)); QPainter painter(&newImage); painter.drawImage(QPoint(0, 0), *image); *image = newImage; } void Derived::resizeEvent(QResizeEvent* event) { if (width() > image.width() || height() > image.height()) { int newWidth = qMax(width() + 128, image.width()); int newHeight = qMax(height() + 128, image.height()); resizeImage(&image, QSize(newWidth, newHeight)); update(); } QWidget::resizeEvent(event); } -
Hi,
I wanted to move all UI settings in a new class. That include custom style settings, resizeEvent, resizeImage, etc. The problem with the code below is that it isn't really linked to QMainWindow. The only way I can set the stuff through
Derivedclass is by I getting the data from parent, e.g.parent->setFixedSize(100, 100);. Even if I do that, the resizeEvent won't work because that's not the right way to do that. How could I do that? What's the right way?TestQt::TestQt(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); Derived *derived = new Derived(this); setCentralWidget(derived); }Derived.h
#include <QColor> #include <QImage> #include <QPoint> #include <QWidget> #include <QPainter> #include <QSize> class Derived : public QWidget { Q_OBJECT public: Derived(QWidget* parent = Q_NULLPTR); protected: void resizeEvent(QResizeEvent* event) override; private: void resizeImage(QImage* image, const QSize& newSize); QImage image; };Derived.cpp
#include "Derived.h" Derived::Derived(QWidget* parent) : QWidget(parent) { setAttribute(Qt::WA_StaticContents); setFixedSize(100, 100); } void Derived::resizeImage(QImage* image, const QSize& newSize) { if (image->size() == newSize) return; QImage newImage(newSize, QImage::Format_RGB32); newImage.fill(qRgb(0, 0, 0)); QPainter painter(&newImage); painter.drawImage(QPoint(0, 0), *image); *image = newImage; } void Derived::resizeEvent(QResizeEvent* event) { if (width() > image.width() || height() > image.height()) { int newWidth = qMax(width() + 128, image.width()); int newHeight = qMax(height() + 128, image.height()); resizeImage(&image, QSize(newWidth, newHeight)); update(); } QWidget::resizeEvent(event); }@electron said in Moving UI settings to a new class:
I wanted to move all UI settings in a new class. That include custom style settings, resizeEvent, resizeImage, etc.
What do you mean by that?
In general it's better, when childs dont know about their parent (If parent changes somehow, everything crashes, because you create a static connection between your
TestQtand yourDerived-class).If I understood it right, you want to set things like window size (TestQt) from your
Derived-class?You can easily use Signals & Slots for this. Connect these classes and pass the values.
-
@electron said in Moving UI settings to a new class:
I wanted to move all UI settings in a new class. That include custom style settings, resizeEvent, resizeImage, etc.
What do you mean by that?
In general it's better, when childs dont know about their parent (If parent changes somehow, everything crashes, because you create a static connection between your
TestQtand yourDerived-class).If I understood it right, you want to set things like window size (TestQt) from your
Derived-class?You can easily use Signals & Slots for this. Connect these classes and pass the values.
-
Hi,
Why do you want to delegate the resizeEvent to a different class ? Especially a child widget, this smell bad design and possible maintenance nightmare.
-
There are some stuff you can factor out, others you just can't. A child widget shall not manage a parent widget, it's wrong on several levels.
-
There are some stuff you can factor out, others you just can't. A child widget shall not manage a parent widget, it's wrong on several levels.