2013-05-10 52 views
-1

我創建了服務器和客戶端之間的連接,連接在控制檯中工作正常,但我不能將QTcpServer類連接到帶有信號和插槽的GUI。這裏是我的代碼:嘗試連接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 2013-05-10 13:29:58

+0

_you_認爲是錯誤的是什麼?在調試代碼時,您應該表現出一些努力,而不是僅僅把它扔給其他人。 – Michael 2013-05-10 13:35:06

回答

1

您在連接方法的錯字:IHM_connetion

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

而發出信號是:

emit IHM_connection() 

QObject:connect返回一個布爾值,表示信號插槽連接是否成功。檢查此值(例如,使用Q_ASSERT)是一種很好的做法,可以爲您節省大量時間,以防出現類似這樣的錯字。 。