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. Regarding to Abstract (or) factory pattern design with Qt.
QtWS25 Last Chance

Regarding to Abstract (or) factory pattern design with Qt.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 605 Views
  • 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.
  • U Offline
    U Offline
    U7Development
    wrote on 24 Sept 2021, 15:46 last edited by
    #1

    Hello..!

    my current interface for creating widgets in my program is this following:

    This is my factory superclass

    class wmaster : public QDialog{
         Q_OBJECT
    
    private:
         std::vector<QWidget*> vQWidgets;
    
    protected:
         QLabel* setup_QLabel(QString _txt, QString _stylesheet);
         QLineEdit* setup_QLineEdit(QString _ph);
         QPushButton* setup_QPushbutton(QString _txt, QString _stylesheet);
         
         //etc... 
    
    public:
         explicit wmaster(QWidget* parent = nullptr);
         ~wmaster();
    };
    

    //wmaster.cpp

    wmaster::wmaster(QWidget* parent) : QDialog(parent){ }
    wmaster::~wmaster(){
         for (auto& o : vQWidgets) delete o; o = nullptr;
    }
    
    QLabel* wmaster::setup_QLabel(QString _txt, QString _stylesheet){
         QLabel* o = new QLabel(this);
         o->setText(_txt);
         o->setStyleSheet(_styleSheet);
        vQWidgets.push_back(o);
        return o;
    }
    //same algorithm for QPushButton and QLineEdit
    

    This is the login window which inherits from wmaster factory:

    class wlogin : public wmaster{
    private:
         QLabel* lblName {nullptr};
         QLineEdit* ledName {nullptr};
         QPushButton* pbtLogin {nullptr};
    
    public:
         explicit wlogin(QWidget* parent = nullptr);
    };
    

    //wlogin.cpp

    wlogin::wlogin(QWidget* parent) : wmaster(parent) {
          //initialization
          lblName = setup_QLabel("Username", C_STYLESHEET_LABEL);  //C_STYLESHEET_LABEL constant as QString
          ledName = setup_QLineEdit("Please input here your username...");
          pbtLogin = setup_QPushButton("Click here to login", C_STYLESHEET_BTN);
          
    }
    

    This tricky factory works fine for me, but I know this is not the best approach to inherit factory...
    I was reading an article that explains abstract factory, but I don't see how can I apply to QWidget classes.. since the article explains that I need to create an abstract object for each of my instantiated objects... and I guess I don't need to create an abstract class for QLabel for example.
    https://refactoring.guru/es/design-patterns/abstract-factory/cpp/example

    Is it recommended to follow abstract factory to create QWidget objects or what design pattern should I follow?
    thanks

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 24 Sept 2021, 15:51 last edited by
      #2

      I don't see any adavantag ein using wmaster here. Why do you think you need it?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • U Offline
        U Offline
        U7Development
        wrote on 24 Sept 2021, 16:01 last edited by
        #3

        Since my program has more windows, all of them initialize their member QWidgets from their respective constructor and all of them make the call to wmaster factory with the setup_ method... this helps me to write less on constructors.

        Inherits from wmaster is trivial, just curious about if it is the sane way or there is another way I can follow...

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 24 Sept 2021, 16:48 last edited by Christian Ehrlicher
          #4

          @U7Development said in Regarding to Abstract (or) factory pattern design with Qt.:

           QLabel* o = new QLabel(this);
           o->setText(_txt);
           o->setStyleSheet(_styleSheet);
          

          So you want to move those three (basically only one, the stylesheet can be set on the parent and all is fine) lines into a separate function?
          Where do you set up the layouting? I would guess a ui file would be much more maintainable here.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          U 1 Reply Last reply 24 Sept 2021, 17:07
          2
          • C Christian Ehrlicher
            24 Sept 2021, 16:48

            @U7Development said in Regarding to Abstract (or) factory pattern design with Qt.:

             QLabel* o = new QLabel(this);
             o->setText(_txt);
             o->setStyleSheet(_styleSheet);
            

            So you want to move those three (basically only one, the stylesheet can be set on the parent and all is fine) lines into a separate function?
            Where do you set up the layouting? I would guess a ui file would be much more maintainable here.

            U Offline
            U Offline
            U7Development
            wrote on 24 Sept 2021, 17:07 last edited by U7Development
            #5

            @Christian-Ehrlicher
            thanks for your answers..
            I recently found that in the Scott Meyer's book : Effective C++ recommends to use factory as a component more than a superclass.

            ui is fine, but I switched to handcoding (I am a bit capricious) because I like to implement manually my user interface. I build my whole program as this way and I'm about to finish it.. but this is the time for a refactory.

            Regarding to layouts.. im not using them, all sizes are fixed and positions are preconfigured at compile time..

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Sept 2021, 18:00 last edited by
              #6

              @U7Development said in Regarding to Abstract (or) factory pattern design with Qt.:

              Regarding to layouts.. im not using them, all sizes are fixed and positions are preconfigured at compile time..

              ouch

              Effective C++ recommends to use factory as a component more than a superclass.

              I still don't see a reason why you need a base class for this one line - you don't gain anything apart you can say you're using a specific design pattern (and this is more or less a reason not to use it).

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SimonSchroeder
                wrote on 27 Sept 2021, 06:59 last edited by
                #7

                If wmaster is a factory and wlogin inherits it this also makes wlogin a factory. That doesn't sound right that wlogin should be a factory.

                Honestly, I am not an expert on many of the design patterns, so you try to read up on the things I mention. However, to me a factory class usually only has static method: You don't need a factory object and you can just call the methods to create new objects (like the QLabel). This also means that you are right that a factory should be a component and not a superclass. BTW, to make a factory a class also comes from pure OOP (like is required from Java). But, you don't actually need an object here and just want to call methods that do something. Since C++ is multiparadigm, to me personally a better approach would be to use free standing functions instead of methods for the factory. These can then be put into a namespace to clean things up a little.

                1 Reply Last reply
                0

                7/7

                27 Sept 2021, 06:59

                • Login

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