-1

我在服务器和客户端之间创建了一个连接,该连接在控制台中工作正常,但我无法使用信号和插槽将我的 QTcpServer 类连接到 GUI。这是我的代码:

服务器TCP.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;
}
4

1 回答 1

1

您在连接方法中有错字:IHM_connetion

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

而发射的信号是:

 emit IHM_connection()

QObject:connect 返回一个布尔值,指示信号槽连接是否成功。检查此值(例如,使用 Q_ASSERT)是一种很好的做法,并且可以在出现此类拼写错误的情况下为您节省大量时间。.

于 2013-05-10T13:41:28.953 回答