Important: Please read the Qt Code of Conduct -

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 Derived class 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)
    	Derived *derived = new Derived(this);


    #include <QColor>
    #include <QImage>
    #include <QPoint>
    #include <QWidget>
    #include <QPainter>
    #include <QSize>
    class Derived : public QWidget
    	Derived(QWidget* parent = Q_NULLPTR);
    	void resizeEvent(QResizeEvent* event) override;
    	void resizeImage(QImage* image, const QSize& newSize);
    	QImage image;


    #include "Derived.h"
    Derived::Derived(QWidget* parent) : QWidget(parent)
    	setFixedSize(100, 100);
    void Derived::resizeImage(QImage* image, const QSize& newSize)
    	if (image->size() == newSize)
    	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));

  • @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 TestQt and your Derived-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.

  • Thanks for your reply. Exactly, I wanted to move the resizeEvent and some UI settings into that Derived class. Can you give me an example how to do that using signals and slots?

  • Lifetime Qt Champion


    Why do you want to delegate the resizeEvent to a different class ? Especially a child widget, this smell bad design and possible maintenance nightmare.

  • I asked the question for a friend. I told him that the developers intended it to be used in same class but he didn't listen to me. I guess he is just used to move the logic to different classes.

  • Lifetime Qt Champion

    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.

  • @sgaist Thank you! :)

Log in to reply