PDA

View Full Version : QMessageBox won't appear.



Vladimir_
23rd September 2014, 14:41
Hello,

I am building a client/server application , I made a response in the server with

socket->write("blabla");

i used telnet as a client and actually the response recieved.

But the problem is that I want to make a notification as a QMessageBox . But nothing appears.

server:


void MyClient::TaskResult(int number)
{
QByteArray Buffer;
Buffer.append("Task Result = ");
Buffer.append(QString::number(number));

socket->write(Buffer);
QMessageBox::information(this, "Bytes written", "Bytes written successfully to the client");
}


the
socket->write(buffer); works, but the messagebox does not work.

Can you tell me the reason why ?

NOTE: i'm not aiming to use the messagebox in the real project, but i made this to debug and make sure about the steps done.
There's another issue which will BE ACTUALLY used in the project, its changing some labels/widgets in the MainWindow from another class at incoming connection.
But thats not my question now.

Thanks,
Vladimir.

Infinity
23rd September 2014, 20:31
It might be a good idea to use an actual debugger to check whether a method is called or not. Based on the provided information it is hard to tell why the message box doesn't appear.

d_stranz
24th September 2014, 00:57
Where are you trying to post this message box? In the main (GUI) thread, or some other thread where you are running your server?

You can't make Qt GUI calls outside of the main thread. If you are doing this from within the main thread, then I have no idea why it doesn't work. Nine lines of code from the middle of a file aren't enough information.

Vladimir_
24th September 2014, 01:06
Here's the whole file :

myclient.h


#ifndef MYCLIENT_H
#define MYCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QThreadPool>
#include <QtGui>
#include "mytask.h"
#include "myserver.h"

class MyClient : public QObject
{
Q_OBJECT
public:
explicit MyClient(QObject *parent = 0);
void setsocket(int Descriptor);
int test = 0;
QWeakPointer<QMessageBox> messageBox;


QTcpSocket *socket;

signals:
bool isConnected();

public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();

private:
MyClient *client;

};

#endif // MYCLIENT_H




myclient.cpp


#include "myclient.h"

MyClient::MyClient(QObject *parent) :
QObject(parent)
{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}

void MyClient::setsocket(int Descriptor)
{

socket = new QTcpSocket;

connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));


socket->setSocketDescriptor(Descriptor);

//QMessageBox::about(this,"new client","a new client is connected "+QString::number(socket->socketDescriptor()));
//connect(socket, SIGNAL(connected()), this, SLOT(setNum()));
//QMessageBox::information(0,"test", QString::number(test));



}

void MyClient::isConnected()
{
if(socket->connected())
{
return true;
}
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
}

void MyClient::connected()
{
messageBox = new QMessageBox(QMessageBox::Information, "Client", "Recu", QMessageBox::Ok, this);
qDebug() << "client connected event";
messageBox.information(parent, "connected", "new");

}

void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}

void MyClient::readyRead()
{
qDebug() << socket->readAll();
QMessageBox::information(0, "connected", "new");

// time consumer
MyTask *mytask = new MyTask();
mytask->setAutoDelete(true);
connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
QThreadPool::globalInstance()->start(mytask);
}

void MyClient::TaskResult(int number)
{
QByteArray Buffer;
Buffer.append("Task Result = ");
Buffer.append(QString::number(number));

socket->write(Buffer);
QMessageBox::information(this, "Bytes written", "Bytes written successfully to the client");
}

void MyClient::setNum()
{
this->test = 12;
}



EDIT: i used the message box to tell me that something happen, This is in the server not the client. after i see that a connection came i will use the messagebox to show the incoming data from the client, to make sure it comes.

Then i will make a notification appear in the main window to tell me that a client connected, that notification may be by a status bar ..etc

d_stranz
24th September 2014, 02:41
I don't know how you are getting this code to compile in the first place. Any QWidget needs another QWidget (or NULL) as its parent. Your MyClient class is derived from QObject, not QWidget, so passing a pointer to "this" into the QMessageBox constructor shouldn't compile because QObject cannot be cast to QWidget.

So assuming that somehow you are getting this to compile, it is possible that because you aren't passing a valid QWidget as the parent of the message box, it fails to construct properly and therefore doesn't appear.

But you still haven't answered the question of where the code you posted lives. Do you create a MyClient instance in the main GUI thread, or are you creating it in another thread? If it is in another thread, then the message box is also being created in that thread, and GUI code will not execute there.

Vladimir_
24th September 2014, 04:20
Thanks,
Now the messsagebox is working..

But how can I work with the UI of the mainWindow from the MyClient file ??

MyClient uses ThreadPool and it opens tasks in each incoming connection.

And each connection has nothing to do with the main GUI...

The BIG deal here is to make a ui->blabla in the main gui when an incoming connection comes ..!

Thanx

anda_skoa
24th September 2014, 05:56
If you want to log incoming data for testng purposes, why don't you just simply use logging facilities such as qDebug/qCDebug?

Cheers,
_

Vladimir_
24th September 2014, 06:23
Please read my replied in this topic rather than reading only the main post.

The previous comment has the issue.

Thanks.

d_stranz
25th September 2014, 01:03
But how can I work with the UI of the mainWindow from the MyClient file ??

You can't execute GUI code from any thread except the main one. If you need to send information from a worker thread to the main thread, then use signals and slots. If the sender and receiver of a signal / slot connection live in different threads, the sender's signal will automatically be queued to be processed as an event by the receiver's event queue. This is thread safe.

You can use any parameters in your signal and slot, as long as Qt's metatype system can serialize them into temporary objects while queuing the signal. Most of Qt's built-in data types (like QByteArray or QString) are supported.

So, if you need to have something in your worker thread cause a change to some GUI feature in the main thread, connect a signal from the worker thread instance to a slot in the main thread, and when the worker thread needs to notify the GUI, send the signal. The slot uses whatever data is transferred to update the GUI.