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..
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.