qt factory for contro1

海前 王 - Sep 2 - - Dev Community
#include <QApplication>
#include <QDebug>
#include <QMap>
#include <QString>
#include <QWidget>

// 基类
class MyBaseWidget : public QWidget {
    Q_OBJECT
public:
    explicit MyBaseWidget(QWidget *parent = nullptr) : QWidget(parent) {}
    virtual void doSomething() = 0;
};

// 派生类1
class MyDerivedWidget1 : public MyBaseWidget {
    Q_OBJECT
public:
    explicit MyDerivedWidget1(QWidget *parent = nullptr) : MyBaseWidget(parent) {}
    void doSomething() override {
        qDebug() << "MyDerivedWidget1 doing something!";
    }
};

// 派生类2
class MyDerivedWidget2 : public MyBaseWidget {
    Q_OBJECT
public:
    explicit MyDerivedWidget2(QWidget *parent = nullptr) : MyBaseWidget(parent) {}
    void doSomething() override {
        qDebug() << "MyDerivedWidget2 doing something!";
    }
};

// 工厂类
class MyWidgetFactory {
public:
    // 注册工厂函数
    static void registerClass(const QString& className, MyBaseWidget* (*creator)(QWidget *)) {
        getRegistry()[className] = creator;
    }

    // 创建控件
    static MyBaseWidget* createWidget(const QString& className, QWidget *parent = nullptr) {
        auto it = getRegistry().find(className);
        if (it != getRegistry().end()) {
            MyBaseWidget* (*creator)(QWidget *) = it.value();
            qDebug() << "Creating widget for class:" << className;
            return creator(parent);
        }
        qDebug() << "Class not found:" << className;
        return nullptr;
    }

private:
    // 获取注册表
    static QMap<QString, MyBaseWidget* (*)(QWidget *)>& getRegistry() {
        static QMap<QString, MyBaseWidget* (*)(QWidget *)> registry;
        return registry;
    }
};

// 定义工厂函数
MyBaseWidget* createMyDerivedWidget1(QWidget *parent) { return new MyDerivedWidget1(parent); }
MyBaseWidget* createMyDerivedWidget2(QWidget *parent) { return new MyDerivedWidget2(parent); }

// 注册类
static bool registered1 = []() {
    MyWidgetFactory::registerClass("MyDerivedWidget1", createMyDerivedWidget1);
    return true;
}();

static bool registered2 = []() {
    qDebug() << "Registering MyDerivedWidget2";
    MyWidgetFactory::registerClass("MyDerivedWidget2", createMyDerivedWidget2);
    return true;
}();
#include
// 主函数
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);  // 使用 QApplication

    // 创建并显示控件
    QString className1 = "MyDerivedWidget1";
    QString className2 = "MyDerivedWidget2";

    MyBaseWidget* widget1 = MyWidgetFactory::createWidget(className1);
    MyBaseWidget* widget2 = MyWidgetFactory::createWidget(className2);


    if (widget1) {
        widget1->doSomething();
        widget1->show();  // 显示控件
    }

    if (widget2) {
        widget2->doSomething();
        widget2->show();  // 显示控件
    }

    return app.exec();
}

#include "main.moc"
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player