2014-01-22 3 views
0

간단한 Tcp 통신 프로젝트를 만들고 싶지만 몇 가지 문제가 있으며 그 문제를 해결하는 방법을 모른다. 솔루션을 찾으려고 할 때 모든 사람들이 .pro 파일에이 코드 (QT + = network)를 추가하라고 지시하지만 ui 프로젝트에서는 프로 파일이 없으므로 해결책을 찾을 수 없습니다.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

Qt 프로젝트에 Visual Studio를 사용하고 있습니다. 어떻게 사용할 수 있습니까? 나는 Qt에서 새로운 사람이다. – citi

+0

Qt를 처음 접한다면 좀 더 많은 튜토리얼을 읽어야합니다. Visual Studio 용 Qt 통합 모듈이 있습니다. 아마 그것을 사용해야합니다. –

답변

1

Qt 프로젝트 설정에서 사용중인 모듈을 활성화해야합니다. 당신은 Qt는 문서에서 찾을 수있는 추가 정보 : 당신은 항상

#include <QMainWindow>

같은 경로가없는 경우에만 클래스 파일을 포함해야한다

#include <QtWidgets/QMainWindow>

처럼 Qt Visual Studio Add-in

또한 사용해서는 안 포함 프로젝트의 모듈을 활성화 한 후에는 QtNetwork 등을 건너 뛰십시오.

+0

/Visual Studio에서 치명적인 오류가 발생합니다 : 치명적인 오류 C1083 : 포함 파일을 열 수 없습니다 : 'QTcpSocket': 해당 파일이나 디렉토리가 없습니다. – citi

+0

Qt 네트워크를 활성화하지 않았으므로 기준 치수. –