Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)
    {
    	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 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

    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.



  • 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