【问题标题】:qt simple tcp communication with ui projectsqt简单的tcp通信与ui项目
【发布时间】:2014-02-12 05:02:46
【问题描述】:

我想创建一个简单的 Tcp 通信项目,但我遇到了一些问题,我不知道如何解决这个问题。当我尝试找到解决方案时,所有人都告诉在 .pro 文件中添加此代码(QT += 网络),但在 ui 项目中我没有任何 pro 文件,所以我不知道找到解决方案。

//commu.h

#ifndef COMMU_H
#define COMMU_H

    #include <QtWidgets/QMainWindow>
    #include "ui_commu.h"
    #include <QtNetwork/QTcpSocket>
    #include <QObject>
    #include <QString>

    class commu : public QMainWindow
    {
        Q_OBJECT

    public:
        commu(QWidget *parent = 0);
        ~commu();

         void start(QString address, quint16 port);

    private:
        Ui::commuClass ui;
        QTcpSocket client;
    public slots:
        void startTransfer();
    };

    #endif // COMMU_H

//commu.cpp

#include "commu.h"
#include <QtNetwork/QHostAddress>

commu::commu(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    connect(&client, SIGNAL(connected()),this,SLOT(startTransfer()));
}

commu::~commu()
{
    client.close();
}


void commu::start(QString address, quint16 port)
{
    QHostAddress addr(address);
    client.connectToHost(addr, port);
}

void commu::startTransfer()
{
    client.write("Hello, world", 13);
}

//main.cpp

#include "commu.h"
#include <QtWidgets/QApplication>
#include <QtCore>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    commu w;
    w.show();
    return a.exec();

    commu client;
    client.start("127.0.0.1", 8888);

}

我得到错误:

1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QTcpSocket::~QTcpSocket(void)" (__imp_??1QTcpSocket@@UAE@XZ) referenced in function "public: virtual __thiscall commu::~commu(void)" (??1commu@@UAE@XZ)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QTcpSocket::QTcpSocket(class QObject *)" (__imp_??0QTcpSocket@@QAE@PAVQObject@@@Z) referenced in function "public: __thiscall commu::commu(class QWidget *)" (??0commu@@QAE@PAVQWidget@@@Z)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::~QHostAddress(void)" (__imp_??1QHostAddress@@QAE@XZ) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" (?start@commu@@QAEXVQString@@G@Z)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(class QString const &)" (__imp_??0QHostAddress@@QAE@ABVQString@@@Z) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" (?start@commu@@QAEXVQString@@G@Z)
1>c:\users\sel\documents\visual studio 2010\Projects\commu\Win32\Debug\\commu.exe : fatal error LNK1120: 4 unresolved externals

【问题讨论】:

  • 为什么不使用 qmake 构建项目?
  • 我正在将 Visual Studio 用于 Qt 项目。我该如何使用它?我是 Qt 的新手。
  • 如果您是 Qt 新手,那么您应该先阅读更多教程。 Visual Studio 有 Qt 集成模块。您可能应该使用它。
  • 我可以毫无问题地创建一些带有Qt集成的项目,现在我需要解决TCP连接问题。所以我在问这个问题......

标签: qt tcp qtcpsocket qtnetwork


【解决方案1】:

您需要在 Qt 项目设置中启用您正在使用的模块。您可以在 Qt 文档中找到更多信息:Qt Visual Studio Add-in

你也不应该使用包含之类的

#include &lt;QtWidgets/QMainWindow&gt;

你应该总是只包含没有路径的类文件

#include &lt;QMainWindow&gt;

所有模块都一样,所以在为项目启用模块后跳过 QtNetwork 等。

【讨论】:

  • 没有 QtNetwork/ 我在 Visual Studio 中收到一个致命错误:致命错误 C1083:无法打开包含文件:'QTcpSocket':没有这样的文件或目录
猜你喜欢
  • 1970-01-01
  • 2011-12-16
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多