Qt:无法对MyClass

时间:2016-06-29 19:55:37

标签: c++ qt signals-slots qthread

我试图通过这个信号传递我的Class:

connect(this, SIGNAL(SIG_connectSerial(SerialSetting::Settings)), serial, SLOT(openConnection(SerialSetting::Settings)),Qt::QueuedConnection);

我要传递的课程是那个课程:

#ifndef SERIALSETTING_H
#define SERIALSETTING_H

#include <QWidget>
#include <QtSerialPort/QSerialPort>



namespace Ui {
class SerialSetting;
}   

class QIntValidator;

class SerialSetting : public QWidget
{
    Q_OBJECT

public:
    struct  Settings {
            QString portName;
            qint32 baudRate;
    };

    Settings settings();



public:
    explicit SerialSetting(QWidget *parent = 0);
    ~SerialSetting();

private slots:
    void apply();

    void on_btnApply_clicked();

private:
    void fillPortsParameters();
    void fillPortsInfo();
    void updateSettings();


private:
    Ui::SerialSetting *ui;
    Settings currentSettings;
    QIntValidator *intValidator;
};

#endif // SERIALSETTING_H

#include "serialsetting.h"
#include "ui_serialsetting.h"

#include <QtSerialPort/QSerialPortInfo>
#include <QIntValidator>
#include <QLineEdit>

QT_USE_NAMESPACE

static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");

SerialSetting::SerialSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialSetting)
{
    ui->setupUi(this);

    intValidator = new QIntValidator(0, 4000000, this);

    //ui->cboBaudRate->setInsertPolicy(QComboBox::NoInsert);

    fillPortsParameters(); //call function to fill comboboxes

    connect(ui->btnApply, SIGNAL(clicked()),this, SLOT(apply()));
}

SerialSetting::~SerialSetting()
{
    delete ui;
}

void SerialSetting::fillPortsParameters()
{

//fill cboComport with all available comports
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
    ui->cboComport->addItem(info.portName());
    }

}

void SerialSetting::apply()
{
    SerialSetting::currentSettings.portName = ui->cboComport->currentText();
    hide();
}

SerialSetting::Settings SerialSetting::settings()
{
    return SerialSetting::currentSettings;
}

void SerialSetting::on_btnApply_clicked()
{

}

编译器抛出此异常: QObject :: connect:无法对“SerialSetting :: Settings”类型的参数进行排队 (确保使用qRegisterMetaType()注册'SerialSetting :: Settings'。) 我尝试qRegisterMetaType<SerialSetting>();,但结果出现以下错误:

static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
 #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)

在class-header的末尾添加Makro Q_DECLARE_METATYPE(Ui::SerialSetting)会引发另一个错误:

invalid application of 'sizeof' to incomplete type 'Ui::SerialSetting'
         isLarge = (sizeof(T)>sizeof(void*)),

1 个答案:

答案 0 :(得分:2)

您无法在前向声明的类(Q_DECLARE_METATYPE)上调用Ui::SerialSetting。此外,您需要声明信号用作参数的类型,在本例中为SerialSetting::Settings

替换

Q_DECLARE_METATYPE(Ui::SerialSetting)

Q_DECLARE_METATYPE(SerialSetting::Settings)

你应该没事。