Results 1 to 9 of 9

Thread: QMessageBox won't appear.

  1. #1
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation QMessageBox won't appear.

    Hello,

    I am building a client/server application , I made a response in the server with
    Qt Code:
    1. socket->write("blabla");
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void MyClient::TaskResult(int number)
    2. {
    3. QByteArray Buffer;
    4. Buffer.append("Task Result = ");
    5. Buffer.append(QString::number(number));
    6.  
    7. socket->write(Buffer);
    8. QMessageBox::information(this, "Bytes written", "Bytes written successfully to the client");
    9. }
    To copy to clipboard, switch view to plain text mode 

    the
    Qt Code:
    1. socket->write(buffer);
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QMessageBox won't appear.

    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.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    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.

  4. #4
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    Here's the whole file :

    myclient.h
    Qt Code:
    1. #ifndef MYCLIENT_H
    2. #define MYCLIENT_H
    3.  
    4. #include <QObject>
    5. #include <QTcpSocket>
    6. #include <QDebug>
    7. #include <QThreadPool>
    8. #include <QtGui>
    9. #include "mytask.h"
    10. #include "myserver.h"
    11.  
    12. class MyClient : public QObject
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit MyClient(QObject *parent = 0);
    17. void setsocket(int Descriptor);
    18. int test = 0;
    19. QWeakPointer<QMessageBox> messageBox;
    20.  
    21.  
    22. QTcpSocket *socket;
    23.  
    24. signals:
    25. bool isConnected();
    26.  
    27. public slots:
    28. void connected();
    29. void disconncted();
    30. void readyRead();
    31. void TaskResult(int number);
    32. void setNum();
    33.  
    34. private:
    35. MyClient *client;
    36.  
    37. };
    38.  
    39. #endif // MYCLIENT_H
    To copy to clipboard, switch view to plain text mode 


    myclient.cpp
    Qt Code:
    1. #include "myclient.h"
    2.  
    3. MyClient::MyClient(QObject *parent) :
    4. QObject(parent)
    5. {
    6. QThreadPool::globalInstance()->setMaxThreadCount(5);
    7. }
    8.  
    9. void MyClient::setsocket(int Descriptor)
    10. {
    11.  
    12. socket = new QTcpSocket;
    13.  
    14. connect(socket,SIGNAL(connected()), this, SLOT(connected()));
    15. connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
    16. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    17.  
    18.  
    19. socket->setSocketDescriptor(Descriptor);
    20.  
    21. //QMessageBox::about(this,"new client","a new client is connected "+QString::number(socket->socketDescriptor()));
    22. //connect(socket, SIGNAL(connected()), this, SLOT(setNum()));
    23. //QMessageBox::information(0,"test", QString::number(test));
    24.  
    25.  
    26.  
    27. }
    28.  
    29. void MyClient::isConnected()
    30. {
    31. if(socket->connected())
    32. {
    33. return true;
    34. }
    35. connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    36. }
    37.  
    38. void MyClient::connected()
    39. {
    40. messageBox = new QMessageBox(QMessageBox::Information, "Client", "Recu", QMessageBox::Ok, this);
    41. qDebug() << "client connected event";
    42. messageBox.information(parent, "connected", "new");
    43.  
    44. }
    45.  
    46. void MyClient::disconncted()
    47. {
    48. qDebug() << "client disconnected!";
    49. // test -= 1;
    50. }
    51.  
    52. void MyClient::readyRead()
    53. {
    54. qDebug() << socket->readAll();
    55. QMessageBox::information(0, "connected", "new");
    56.  
    57. // time consumer
    58. MyTask *mytask = new MyTask();
    59. mytask->setAutoDelete(true);
    60. connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
    61. QThreadPool::globalInstance()->start(mytask);
    62. }
    63.  
    64. void MyClient::TaskResult(int number)
    65. {
    66. QByteArray Buffer;
    67. Buffer.append("Task Result = ");
    68. Buffer.append(QString::number(number));
    69.  
    70. socket->write(Buffer);
    71. QMessageBox::information(this, "Bytes written", "Bytes written successfully to the client");
    72. }
    73.  
    74. void MyClient::setNum()
    75. {
    76. this->test = 12;
    77. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by Vladimir_; 24th September 2014 at 01:22.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    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.

  6. #6
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox won't appear.

    If you want to log incoming data for testng purposes, why don't you just simply use logging facilities such as qDebug/qCDebug?

    Cheers,
    _

  8. #8
    Join Date
    Sep 2014
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    Please read my replied in this topic rather than reading only the main post.

    The previous comment has the issue.

    Thanks.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMessageBox won't appear.

    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.

  10. The following user says thank you to d_stranz for this useful post:

    Vladimir_ (25th September 2014)

Similar Threads

  1. Size of QMessageBox
    By elizabeth.h1 in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 16:00
  2. QMessageBox
    By sonuani in forum Qt Programming
    Replies: 13
    Last Post: 2nd September 2009, 17:56
  3. QMessageBox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2008, 08:27
  4. Re: Help on QMessageBox
    By arunvv in forum Newbie
    Replies: 2
    Last Post: 25th March 2008, 23:45
  5. how to use tr() for QMessageBox?
    By gfunk in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2007, 11:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.