簡體   English   中英

將類添加到基本Qt GUI應用程序時出現Qt鏈接器錯誤

[英]Qt linker error when adding class to basic Qt GUI application

我已經掙扎了一段時間,無法解決這個問題。 我在基本的Qt GUI應用程序中添加了一個類,但出現鏈接器錯誤。 我看不出是什么原因引起的,希望獲得反饋。

鏈接器錯誤:

main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main

debug\Test.exe:-1: error: LNK1120: 2 unresolved externals

這是項目文件:

#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    serial.cpp

HEADERS  += mainwindow.h \
    serial.h

FORMS    += mainwindow.ui

serial.h文件:

#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>

class Serial : public QObject
{
    Q_OBJECT
public:
    explicit Serial(QObject *parent = 0);
    void start(QSerialPort::BaudRate baudRate);
    bool Serial::closePort();
    void Serial::readData();

signals:

public slots:

};

#endif // SERIAL_H

serial.cpp文件:

#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>

Serial::Serial(QObject *parent) :
    QObject(parent)
{
    serial = new QSerialPort(this);
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

bool Serial::start(QSerialPort::BaudRate baudRate)
{
    if(serial.open(QIODevice::ReadWrite))
    {
        //TODO: Connection OK is green in GUI
        QDebug << "Port opened successfully" << std::endl;
        return true;
    }
    else
    {
        //Open failed.
        QDebug << "Port failed to open" << std::endl;
        //TODO: Show error window in GUI
        return false;
    }

    if(
        serial.setBaudRate(baudRate) &&
        serial.setDataBits(QSerialPort::Data8) &&
        serial.setParity(QSerialPort::NoParity) &&
        serial.setStopBits(QSerialPort::OneStop) &&
        serial.setFlowControl(QSerialPort::NoFlowControl)
      )
    {
        //Connection ok.
        QDebug << "Parameters set OK" << std::endl;
    }
    else
    {
        QDebug << "Parameters set FAILED" << std::endl;
        Serial::closePort();
    }
}

bool Serial::closePort()
{
    serial.close();
    //TODO: Connection OK is red in GUI
}

void Serial::readData()
{
    this->readDataArray = this->serial.readAll();
}

如前所述這里 ,因為QtSerialPort是你需要明確地將它添加到您的項目模塊的加載:

QT += serialport

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM