widget container with custom type
-
wrote on 16 Jan 2019, 03:38 last edited by
i have a widget and a widget container. the container creates a lot of those widgets in the constructor and sets some attributes and connects some signals and slots.
class Widget : public QWidget {}; class Container : public QWidget {};
i need another container widget like the above one but with different widgets inside.
what i think i can do:
- make a template function that takes as template argument the widget type, and call that function from each container widget with appropriate type (can i override template functions?)
- copy the code of first container widget's ctor into the ctor of the second one and there create another type of widgets
i like the 1st one more. any more suggestions/advice?
-
Template approach is good.
-
wrote on 16 Jan 2019, 04:45 last edited by user4592357
@dheerendra
yeah but i can't override a template function...
also, how should i create the different signal/slot connections? should i pass them as a list to the function? -
@dheerendra
yeah but i can't override a template function...
also, how should i create the different signal/slot connections? should i pass them as a list to the function?@user4592357 Something like
// Base would be QWidget in your case class Base {}; class W1 : public Base {}; class W2: public Base {}; template<typename W> Base* createWidget() { return new W(); } class Container { public: Container(std::function<Base*()> factory) { // Create an instance of the widget Base* widget = factory(); } } Container container1(createWidget<W1>); Container container2(createWidget<W2>);
Regarding connections: as long as these widgets have same signals/slots (from base class from example) I don't see any issues. But if they have different signals/slots I don't see how you can do this in a generic way as your container needs to know what these different widgets have.
1/4