Qt 5.15.2使用connect报错 error C2665: “QObject::connect”: 没有重载函数可以转换所有参数类型
代码如下,我定义了一个int变量n_all,想实现在n_all变量数值改变的时候,执行一次函数channelchange(),百度了一堆,还是不知道问题出在哪里,怎么解决?
connect(n_all, SIGNAL(valueChanged(int)), this, SLOT(channelchange()));
Qt 5.15.2使用connect报错 error C2665: “QObject::connect”: 没有重载函数可以转换所有参数类型
代码如下,我定义了一个int变量n_all,想实现在n_all变量数值改变的时候,执行一次函数channelchange(),百度了一堆,还是不知道问题出在哪里,怎么解决?
connect(n_all, SIGNAL(valueChanged(int)), this, SLOT(channelchange()));
MyObject.h:
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QTimer>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = nullptr);
int getNAll() const;
public slots:
void updateNAll();
signals:
void nAllChanged(int n);
private:
int m_n_all;
QTimer *m_timer;
};
#endif // MYOBJECT_H
MyObject.cpp
#include "MyObject.h"
MyObject::MyObject(QObject *parent)
: QObject(parent),
m_n_all(0)
{
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &MyObject::updateNAll);
m_timer->start(1000);
}
int MyObject::getNAll() const
{
return m_n_all;
}
void MyObject::updateNAll()
{
m_n_all++;
emit nAllChanged(m_n_all);
}
现在,您可以在GUI控制器类中声明一个MyObject指针,并在其构造函数中连接nAllChanged(信号)到channelchange(槽):
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "MyObject.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
m_obj(new MyObject(this))
{
ui->setupUi(this);
connect(m_obj, &MyObject::nAllChanged, this, &MainWindow::channelchange);
}
void MainWindow::channelchange(int n)
{
// Do something with n
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "MyObject.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),
m_obj(new MyObject(this))
{
ui->setupUi(this);
connect(m_obj, &MyObject::nAllChanged, ui->nAllSpinBox, &QSpinBox::setValue);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_changeNAllButton_clicked()
{
m_obj->updateNAll();
}