-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;
}
僅發佈代碼的相關部分。不要粘貼你的整個程序。 – sashoalm 2013-05-10 13:29:58
_you_認爲是錯誤的是什麼?在調試代碼時,您應該表現出一些努力,而不是僅僅把它扔給其他人。 – Michael 2013-05-10 13:35:06