我想創建一個簡單的tcp通信項目,但我遇到了一些問題,我不知道如何解決這個問題。當我試圖找到解決方案時,所有人都告訴在.pro文件中添加此代碼(QT + = network),但在ui項目中,我沒有任何pro文件,因此我不知道找到解決方案。qt與ui項目的簡單tcp通信
//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)" ([email protected]@[email protected]) referenced in function "public: virtual __thiscall commu::~commu(void)" ([email protected]@[email protected])
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QTcpSocket::QTcpSocket(class QObject *)" ([email protected]@[email protected]@@@Z) referenced in function "public: __thiscall commu::commu(class QWidget *)" ([email protected]@[email protected]@@@Z)
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::~QHostAddress(void)" ([email protected]@[email protected]) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" ([email protected]@@[email protected]@[email protected])
1>commu.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(class QString const &)" ([email protected]@[email protected]@@@Z) referenced in function "public: void __thiscall commu::start(class QString,unsigned short)" ([email protected]@@[email protected]@[email protected])
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的新手。 – citi
如果您是Qt新手,那麼您應該先閱讀一些教程。有Visual Studio的Qt集成模塊。你應該可以使用它。 –