Trying to provide console input to GUI window lineedit, But getting fail.
-
Hello I am trying with a multiple client server console/GUI application in which server is receiving data from clients and providing it to GUI lineedit. But unfortnately in is not reflecting the data in lineedit, anyone please help to sort it out.
here I am providing with code details.---
main.cpp
@
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "myserver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MyServer server;
server.startServer();MainWindow w;
w.show();
return a.exec();
}
@ -
mainwindow.cpp
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"myserver.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
qDebug()<<"i am in main window ";
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::acceptData(QString text)
{
qDebug()<<"in acceptData()---"<<text;
nitin = text;
emit NeedtoUpdate(text);
ui->lineEdit->setText(text);
}
@- mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include"myserver.h"class MyServer;
namespace Ui {
class MainWindow;}
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QString nitin;
friend class MyServer;
int socketDescriptor;signals:
void NeedtoUpdate(QString);public:
Ui::MainWindow *ui;public:
void inlinefunction(QString t1){newtext=t1;}void acceptData(QString);
QString newtext;
};
#endif // MAINWINDOW_H
@- myserver.cpp
@
#include "myserver.h"
#include"mythread.h"
#include <QHostAddress>
#include<mainwindow.h>
#include"ui_mainwindow.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{}
void MyServer::startServer()
{
int port = 1234;if(!this->listen(QHostAddress::Any, port)) { qDebug() << "Could not start server"; } else { qDebug() << "Listening to port " << port << "..."; }
}
// This function is called by QTcpServer when a new connection is available.
void MyServer::incomingConnection(int socketDescriptor)
{
// We have a new connection
qDebug() << socketDescriptor << " Connecting...";// Every new connection will be run in a newly created thread MyThread *thread = new MyThread(socketDescriptor,this); // connect signal/slot // once a thread is not needed, it will be beleted later connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread,SIGNAL(somedata(QString)),this,SLOT(somefunction(QString))); thread->start();
}
void MyServer:: somefunction(QString Datarecv)
{
qDebug()<<"recieved data" <<Datarecv ;
MainWindow *main_access=new MainWindow;
main_access->acceptData(Datarecv.toAscii());
main_access->inlinefunction(Datarecv.toAscii());
}
@- myserver.h
@
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include "mainwindow.h"namespace Ui {
class MyServer;
}
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();private:
Ui::MyServer *ui;
signals:
void tomainwindow(QString);
public slots:
void somefunction(QString);public slots:
void incomingConnection(int socketDescriptor);
// void AppendToBrowser(QString);
};#endif // MYSERVER_H
@- mythread.cpp
@
#include "mythread.h"
#include "myserver.h"
#include"ui_mainwindow.h"
#include<mainwindow.h>
MyThread::MyThread(int ID,QObject *parent) :
QThread(parent)
{
this->socketDescriptor= ID;
}
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";socket = new QTcpSocket(); // set the ID if(!socket->setSocketDescriptor(this->socketDescriptor)) { // something's wrong, we just emit a signal emit error(socket->error()); return; } // connect socket and signal connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); // We'll have multiple clients, we want to know which is which qDebug() << socketDescriptor << " Client connected"; // make this thread a loop, // thread will stay alive so that signal/slot to function properly // not dropped out in the middle when thread dies exec();
}
void MyThread::readyRead()
{Data = socket->readAll();
// qDebug()<<socket->readAll();
// qDebug() << socketDescriptor << " Data in: " << Data;// socket->write(Data);
emit somedata(Data);
}void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";socket->deleteLater(); exit(0);
}
@- mythread.h
@
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int ID,QObject *parent = 0);
void run();public:
QString Data;signals: void error(QTcpSocket::SocketError socketerror); public slots: void readyRead(); void disconnected();
signals:
void somedata(QString);//private:
private:
QTcpSocket *socket;int socketDescriptor;
};
#endif // MYTHREAD_H
@andreyc edit: Added @ around code.
-
-
Hi and welcome to devnet,
There are several strange things in your code:
you have an UI based QTcpServer, that doesn't make sense
You have both your server and GUI in the same application which is pretty strange
There's nothing console related
Before going further with threading (which is not needed with Qt's QTcpServer/Socket) I would recommend that you read the Fortune Client/Server examples that shows a great part of what you want to achieve.