2013-05-10 2 views
-1

서버와 클라이언트간에 연결을 만들었지 만 콘솔에서 제대로 작동하지만 신호와 슬롯이있는 GUI에 QTcpServer 클래스를 연결할 수 없습니다. 여기 내 코드입니다 :QTcpServer를 신호로 GUI에 연결하려고 시도했습니다.

ServerTCP.cpp는

ServerTcp :: ServerTcp (QWidget *parent) 
{ 
    listen(QHostAddress::Any,4000); 
    QObject:: connect(this, SIGNAL(newConnection()), 
    this, SLOT(connectionRequest())); 
} 

void ServerTcp :: connectionRequest() 
{ 

    emit IHM_connection(); 
    clientConnection = nextPendingConnection(); 

    QObject:: connect(clientConnection, SIGNAL(readyRead()), 
    this, SLOT(read())); 
} 

void ServerTcp::read() 
{ 

    QString ligne; 
    while(clientConnection->canReadLine())  
    { 
     ligne = clientConnection->readLine(); 
     emit IHM_text(ligne);   
    } 
    QTextStream text(clientConnection);  


} 

ManinWindow.cpp

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    QObject::connect(&server,SIGNAL(IHM_connetion()),this,SLOT(connectionRequested())); 
    QObject::connect(&server,SIGNAL(read(QString)),this,SLOT(print_text(QString))); 
} 



void MainWindow::on_quitButton_clicked() 
{ 
    qApp->quit(); 
} 

void MainWindow::print_text(QString text){ 
    ui->log->append("message : " + text); 
} 

void MainWindow::connectionRequested(){ 
    ui->log->append("connection OK!"); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
+0

코드의 관련 부분 만 게시하십시오. 전체 프로그램을 붙여 넣지 마십시오. – sashoalm

+0

_you_는 무엇이라고 생각하십니까? 다른 사람들에게 코드를 던지기보다는 코드를 디버깅 할 때 약간의 노력을 보여 주어야합니다. – Michael

답변

1

당신은 연결 방법에 오타가 있습니다 방출하면서 IHM_connetion

QObject::connect(&server,SIGNAL(**IHM_connetion**()) 

을 신호 :

emit IHM_connection() 

QObject : connect는 신호 슬롯 연결이 성공했는지를 나타내는 bool 값을 반환합니다. 이 값을 확인하는 것은 (예를 들어, Q_ASSERT를 사용하는 것) 좋은 연습이며 이와 같은 오타가 발생할 경우 많은 시간을 절약 할 수 있습니다. .