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. widget container with custom type
Forum Updated to NodeBB v4.3 + New Features

widget container with custom type

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 560 Views 1 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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:

    1. 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?)
    2. 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?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Template approach is good.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • U 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?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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