Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving UI settings to a new class
Qt 6.11 is out! See what's new in the release blog

Moving UI settings to a new class

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Electron
    wrote on last edited by
    #1

    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);
    }
    
    Pl45m4P 1 Reply Last reply
    0
    • E Electron

      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);
      }
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      E 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @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.

        E Offline
        E Offline
        Electron
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • E Offline
            E Offline
            Electron
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              E 1 Reply Last reply
              2
              • SGaistS SGaist

                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.

                E Offline
                E Offline
                Electron
                wrote on last edited by
                #7

                @sgaist Thank you! :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved