本文介绍了qt与ui项目的简单tcp通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a simple Tcp Communication project but I get some problems and I dont know how to solve that problem. When I try to find solution all people tell add this code (QT += network)on .pro file but in ui projects I dont have any pro file so I dont know the find the solution.

//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

//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

推荐答案

您需要在 Qt 项目设置中启用您正在使用的模块.您可以在 Qt 文档中找到更多信息:Qt VisualStudio 插件

You need to enable modules you're using in Qt Project Settings. More info you can find in Qt docs: Qt Visual Studio Add-in

你也不应该像这样使用包含

You also shouldn't use includes like

#include

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

you should always include only class file without path like

#include

所有模块都一样,因此在为项目启用模块后跳过 QtNetwork 等.

Same goes for all modules, so skip QtNetwork etc after you enable modules for your project.

这篇关于qt与ui项目的简单tcp通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 19:59