Regarding to Abstract (or) factory pattern design with Qt.
-
wrote on 24 Sept 2021, 15:46 last edited by
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::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::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/exampleIs it recommended to follow abstract factory to create QWidget objects or what design pattern should I follow?
thanks -
I don't see any adavantag ein using wmaster here. Why do you think you need it?
-
wrote on 24 Sept 2021, 16:01 last edited by
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...
-
Lifetime Qt Championwrote on 24 Sept 2021, 16:48 last edited by Christian Ehrlicher
@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. -
@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.wrote on 24 Sept 2021, 17:07 last edited by U7Development@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..
-
@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).
-
wrote on 27 Sept 2021, 06:59 last edited by
If
wmaster
is a factory andwlogin
inherits it this also makeswlogin
a factory. That doesn't sound right thatwlogin
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 theQLabel
). 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.
5/7