0

Hello I don't know why it's do this error, I see one the web that might be come from the macro Q_OBJECT but on other class this problem don't appear.

I use the same code for ram and I call it just one time, the only difference here is that I call it 4 time for the four CPU.

Here is my 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));
}

and here is my 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
Benjamin Sx
  • 653
  • 1
  • 7
  • 18

1 Answers1

1

From signals and slots doc:

The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)

You have 2 problems when you are connecting:

connect(timer, SIGNAL(timeout()), this, SLOT(showCPU(_i)));
  1. You are connecting signal and slot with different signatures.
  2. While connecting you are giving a variable to slot instead of just puting the type of parameter.

So just changing your slot signature solves your problem:

//.h file
void showCPU();
...
// .cpp
connect(timer, SIGNAL(timeout()), this, SLOT(showCPU()));


EDIT

As O'Neil suggests, you can use C++11 lambda function as well:

qtCPUBar::qtCPUBar(int i)
{
    ...
    _i = i;
    connect(timer, QTimer::timeout, this, [this]() { showCPU(_i); });
    ...
}

Warning: it uses New Signal Slot Style

Bobur
  • 545
  • 1
  • 6
  • 21