c++ - Trying to connect QTcpServer to GUI with signals -
i created connection between server , client, connection works fine in console, coudn't connect qtcpserver class gui signals , slots. here code :
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; }
you have typo in connect method: ihm_connetion
qobject::connect(&server,signal(**ihm_connetion**()) while emitted signal is:
emit ihm_connection() qobject:connect returns bool value indicates if signal-slot connection successful. checking value (for example, q_assert) practice , can save lot of time in case of typos this. .
Comments
Post a Comment