首页 > 解决方案 > Qt C++:信号和插槽参数不兼容

问题描述

我不知道这段代码有什么问题,我用来连接信号和排序的语法看起来是正确的,但由于某种原因,我一直收到这个错误

error: static assertion failed: Signal and slot arguments are not compatible.
 #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                             ^

这是头文件的代码

include <QString>
#include <QStringList>
#include <QTextStream>

class filereader : public QObject
{
    Q_OBJECT
public:
    Q_PROPERTY(QString filename READ getFileName WRITE setFileName NOTIFY fileNameChanged) 
    Q_PROPERTY(QString output READ getOutput) 
    Q_PROPERTY(QString error READ getErrorString)

    explicit filereader(QObject *parent = 0);

    QString getFileName();
    QString getOutput();
    QString getErrorString();

    void setFileName(const QString &fileName);

    Q_INVOKABLE void readFile(); // this allows us to use this is as a function so we could do something like onClicked{ <text_id>.text = <file_reader_id>.readFile() }
signals:
    void fileNameChanged();
    void outputChanged(QString &Output);
    void gotError(QString &&err);
    void errorSignal();
protected slots:
    void handleOut(QString &output);
    void handleError(QString &err);
private:
    QString fileName;
    QString output;
    QString error;
};

#endif // FILEREADER_H

这是源文件的代码

#include "filereader.h"

filereader::filereader(QObject *parent):    
    QObject(parent)
{
    connect(this,&filereader::gotError,this,&filereader::handleError);
    connect(this,&filereader::outputChanged,this,&filereader::handleOut);
}

QString filereader::getFileName()
{
    return this->fileName;
}

QString filereader::getOutput()
{
    return this->output;
}

QString filereader::getErrorString()
{
    return this->error;
}

void filereader::setFileName(const QString &fileName)
{
    if(this->fileName == fileName)
        return;
    this->fileName = fileName;
    emit fileNameChanged();
}

void filereader::readFile()
{
    QFile file(this->fileName);
    if (file.open(QIODevice::ReadOnly))
    {
       QTextStream in(&file);
       while (!in.atEnd())
       {
          QString line = in.readAll();
          outputChanged(line);
          qDebug() << line;
       }
       file.close();
       file.flush(); // this flushes the file stream
    }else{
        gotError("Could not open the file!");
    }
}

void filereader::handleOut(QString &output)
{
    if(this->output == output)
        return;
    this->output = output;
}

void filereader::handleError(QString &err)
{
    if(this->error == err)
        return;
    this->error = err;
    emit errorSignal();
}

我是 Qt 和 Qml 的新手,所以请帮助我理解这个问题。谢谢 :)

标签: c++qtqmlsignals-slots

解决方案


推荐阅读