你好,我不知道为什么会出现这个错误,我看到一个可能来自宏Q_OBJECT的web,但是在其他类中这个问题不会出现。
我对ram使用相同的代码,我只对它进行了一次调用,这里唯一的区别是,对于四个CPU,我称之为4次。
这是我的CPP
qtCPUBar::qtCPUBar(int i)
{
_i = i;
auto *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showCPU(_i)));
timer->start(1000);
showCPU(_i);
resize(150, 60);
}
qtCPU::qtCPU()
{
CPUStats info;
info.updateInfo();
if (info.getCoreAmount() >= 4) {
auto *CPU1 = new qtCPUBar(0);
auto *CPU2 = new qtCPUBar(1);
auto *CPU3 = new qtCPUBar(3);
auto *CPU4 = new qtCPUBar(4);
auto *vbox6 = new QVBoxLayout();
vbox6->addWidget(CPU1);
vbox6->addWidget(CPU2);
vbox6->addWidget(CPU3);
vbox6->addWidget(CPU4);
this->setLayout(vbox6);
}
}
void qtCPUBar::showCPU(int i)
{
CPUStats cpu;
cpu.updateInfo();
auto tot = cpu._CPUInfo._Frequence;
auto la = (cpu.getInfo(i, CPUStats::CORE_USAGE)).back();
auto res = 100 * la / tot;
this->setValue(static_cast<int>(res));
}这是我的HPP
#ifndef CPP_RUSH3_CPU_HPP
#define CPP_RUSH3_CPU_HPP
#include <QProgressBar>
#include <QtWidgets>
class qtCPUBar : public QProgressBar{
Q_OBJECT
public:
virtual ~qtCPUBar() = default;
qtCPUBar(int i);
private slots:
void showCPU(int i);
private:
int _i = 0;
};
struct qtCPU : public QWidget{
public:
virtual ~qtCPU() = default;
qtCPU();
};
#endif发布于 2018-01-21 06:19:08
从信号和插槽文档:
信号的签名必须与接收时隙的签名相匹配。(事实上,时隙的签名可能比接收的信号短,因为它可以忽略额外的参数。)
连接时有两个问题:
connect(timer, SIGNAL(timeout()), this, SLOT(showCPU(_i)));因此,只要更改时隙签名就可以解决您的问题:
//.h file
void showCPU();
...
// .cpp
connect(timer, SIGNAL(timeout()), this, SLOT(showCPU()));
编辑
正如O‘’Neil所建议的,您也可以使用C++11 lambda函数:
qtCPUBar::qtCPUBar(int i)
{
...
_i = i;
connect(timer, QTimer::timeout, this, [this]() { showCPU(_i); });
...
}警告:它使用新型信号槽型
https://stackoverflow.com/questions/48363918
复制相似问题