QObject Connection“不能从''Class中转换参数3,该类别继承了Qobject'到qObject*

发布于 2025-02-12 18:02:39 字数 1794 浏览 2 评论 0原文

我正在使用MVC模式,并且正在尝试将视图类的信号与继承qObject的控制器类连接起来

class View : public QWidget
{
    Q_OBJECT

private:

   Controller* controller;

   QPushButton* startButton;

   void addControls(QVBoxLayout* mainLayout);

public:
   explicit View(QWidget *parent = nullptr);

   void setController(Controller* c);
};

#endif // VIEW_H

是方法

void View::addControls(QVBoxLayout *mainLayout)
{
    //I'm adding the button
}

View::View(QWidget *parent) : QWidget(parent)
{
//Layout
}

void View::setController(Controller *c){
    controller = c;

    connect(startButton, SIGNAL(clicked()), controller, SLOT(begin()));
//ERROR controller is a Controller* and it can't be converted it to const QObject*
}

,这是控制器类

class Controller : public QObject
{
    Q_OBJECT
private:
    QTimer* timer;

    View* view;
    Model* model;

public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller();

    void setModel(Model* m);
    void setView(View* v);

public slots:
    void begin() const;

};

#endif // CONTROLLER_H

方法

Controller::Controller(QObject *parent):
    QObject(parent), timer(new QTimer)
{
    connect(timer, SIGNAL(timeout()), this, SLOT(next()));
}

Controller::~Controller() { delete timer; }

void Controller::setModel(Model* m) { model = m; }

void Controller::setView(View* v) { view = v; }

void Controller::begin() const {
    timer->start(200);

}

,是良好的 每个组件。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    View w;
    Controller c;
    Model m;

    c.setModel(&m);
    c.setView(&w);
    w.setController(&c);
    w.show();
    return a.exec();
}

,这是我的主要位置。设置我尝试了我能想到的一切,无法使其起作用的

I'm using the MVC pattern and I'm trying to connect a signal from my view class with my controller class that inherits QObject

class View : public QWidget
{
    Q_OBJECT

private:

   Controller* controller;

   QPushButton* startButton;

   void addControls(QVBoxLayout* mainLayout);

public:
   explicit View(QWidget *parent = nullptr);

   void setController(Controller* c);
};

#endif // VIEW_H

This are the methods

void View::addControls(QVBoxLayout *mainLayout)
{
    //I'm adding the button
}

View::View(QWidget *parent) : QWidget(parent)
{
//Layout
}

void View::setController(Controller *c){
    controller = c;

    connect(startButton, SIGNAL(clicked()), controller, SLOT(begin()));
//ERROR controller is a Controller* and it can't be converted it to const QObject*
}

And this is the Controller class

class Controller : public QObject
{
    Q_OBJECT
private:
    QTimer* timer;

    View* view;
    Model* model;

public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller();

    void setModel(Model* m);
    void setView(View* v);

public slots:
    void begin() const;

};

#endif // CONTROLLER_H

And the methods

Controller::Controller(QObject *parent):
    QObject(parent), timer(new QTimer)
{
    connect(timer, SIGNAL(timeout()), this, SLOT(next()));
}

Controller::~Controller() { delete timer; }

void Controller::setModel(Model* m) { model = m; }

void Controller::setView(View* v) { view = v; }

void Controller::begin() const {
    timer->start(200);

}

For good measure, here is the main where I set each component

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    View w;
    Controller c;
    Model m;

    c.setModel(&m);
    c.setView(&w);
    w.setController(&c);
    w.show();
    return a.exec();
}

I've tried everything I could think of, can't make it work..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2025-02-19 18:02:39

停止使用可怕的基于文本的插槽连接接口。
使用带来不错的编译错误的现代案例:

QObject::connect(startButton, &QPushButton::clicked, controller, &Controller::begin);

Stop using the terrible text-based slot connection interface.
Use the modern one that gives nice compile errors:

QObject::connect(startButton, &QPushButton::clicked, controller, &Controller::begin);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文