Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7459169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:24:44+00:00 2026-05-29T14:24:44+00:00

Since Qt doesnt allow templates in their slots, I tried to do the following

  • 0

Since Qt doesnt allow templates in their slots, I tried to do the following solution without success.

First a would like to create a list like this:

list commands = 
0, "MyDashboard", DashBoard0
1, "MomsDashboard", Dashboard1

Dashboard 0 and 1 are both derived from widget

Lets say you have a QListWidget where we add the strings in the list and then do the connection:

connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), 
       this, SLOT(addDashboard(QListWidgetItem*)));

void addDashboard(QListWidgetItem* item) {
    int index = listWidget->row(item);

    QWidget* widget = new typeof(command.atIndex(index).getType());
    widget->show();

}

How would I create that list and store types just like you do in C# Type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:24:46+00:00Added an answer on May 29, 2026 at 2:24 pm

    C++ does not allow you to create an object (using the new operator) where the type is only known at runtime. However, you can use a simplified form of the Factory Method pattern as a workaround.

    Here’s an example:

    // Type IDs that are associated with a widget type
    enum WidgetTypeId {
        dashboard1WidgetTypeId,
        dashboard2WidgetTypeId
    };
    
    // Factory method
    QWidget* createWidget(WidgetTypeId type) {
        switch (type)
        {
            case dashboard1WidgetTypeId:
                return new DashBoard0;
    
            case dashboard2WidgetTypeId:
                return new DashBoard1;
        }
    }
    
    void addDashboard(QListWidgetItem* item) {
        int index = listWidget->row(item);
    
        QWidget* widget = createWidget(command.atIndex(index).getWidgetTypeId());
        widget->show();
    
    }
    

    Not very pretty, I know. If your widgets are clonable, you can use a std::map instead of the ugly switch statement. This alternative approach would be an example of the Prototype Pattern. Here’s some sample code showing this approach:

    class WidgetFactory
    {
    public:
        QWidget* create(const std::string& name) {
            return prototypes_[name]->clone();
        }
    
        void addPrototype(const std::string& name, QWidget* prototype) {
            prototypes_[name] = prototype;
        }
    
    private:
        std::map<std::string, QWidget*> prototypes_;
    }
    
    
    WidgetFactory factory;
    factory.addPrototype("DashBoard0", new DashBoard0);
    factory.addPrototype("DashBoard1", new DashBoard1);
    
    
    void addDashboard(QListWidgetItem* item) {
        int index = listWidget->row(item);
    
        QWidget* widget = factory.create(command.atIndex(index).getWidgetTypeName());
        widget->show();
    
    }
    

    C++ is not a very dynamic language. It has limitied RTTI capabilities and practically none of the reflection features in C#. That’s why you have to resort to patterns like Factory Method and Abstract Factory.


    ADDENDUM

    It hasn’t dawned on me that Qt might provide runtime class information beyond what is normally available in C++ (I only used Qt for simple utility apps, so I don’t know all the bells & whistles available in that framework). With that in mind, I searched and found this mailing list discussion on how to instantiate Qt objects by class name. I don’t know if that solution works for plugin objects, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to overload the operator<< to allow it to work with shared_ptr
Since VBScript doesn't support lookbehinds, I'm looking for an alternative solution. I have the
Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge
I need to find a solution to allow a subclass to get its proper
I'd like to hide the taskbar entry to maximize effective space since the app
I want to use subdomains instead of subfolders. Unfortunately, since my host doesn't allow
So, I am trying to create a to_python converter that will allow me to
I plan to use non-renewing subscription on SaaS app since apple doesn't allow auto-renewable
I'm currently using a free host that doesn't allow https connections, and since my
Since Java doesn't allow passing methods as parameters, what trick do you use to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.