2014-01-22 40 views
0

我想創建一個簡單的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 
+0

爲什麼你不使用qmake來構建項目? –

+0

我正在使用Visual Studio進行Qt項目。我怎麼用它?我是Qt的新手。 – citi

+0

如果您是Qt新手,那麼您應該先閱讀一些教程。有Visual Studio的Qt集成模塊。你應該可以使用它。 –

回答

1

您需要啓用您在Qt項目設置中使用的模塊。更多信息,你可以在Qt文檔發現:Qt Visual Studio Add-in

你也不應使用包括像

#include <QtWidgets/QMainWindow>

你應該始終包括但路徑唯一的類文件中像

#include <QMainWindow>

所有模塊都一樣,所以在爲項目啓用模塊後跳過QtNetwork等。

+0

沒有QtNetwork /我在Visual Studio中收到一個致命錯誤: 致命錯誤C1083:無法打開包含文件:'QTcpSocket':沒有這樣的文件或目錄 – citi

+0

因爲你沒有啓用qt網絡模塊。 –