Results 1 to 6 of 6

Thread: Can't create an object : initialisation problem ?

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Can't create an object : initialisation problem ?

    Hello,

    My application is composed of an "Icone" (Personal class that setup a QSystemTrayIcon with a QMenu).

    This Icone creates a "Veilleur" object (Personal class that simulate a service in order to administrate a client side connection to a server).

    So the "Veilleur" creates a "MailChecker" (Personal class that checks for new mails on a server).

    At the end, the "MailChecker" use a QTcpSocket to connect to the server.

    The problem is that today I need to developp the "Veilleur" class, and a strange thing as appeared : I can't add anything !
    For example, if I want to add a QAction (private or public) the application will fall down.
    The application as no problem to pass the builder : no errors, no warning... Nothing.
    But when I launch it, I get a Windows error box saying me that the application as encoutered an error and must be closed...

    I can't understand the problem, so I put here my code... As reduced as possible .

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. #include "Icone.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9. Icone mon_icone;
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 




    Icone.h
    Qt Code:
    1. #ifndef ICONE_H
    2. #define ICONE_H
    3.  
    4. #include "Veilleur.h"
    5.  
    6. #include <iostream>
    7. #include <QSystemTrayIcon>
    8. #include <QWidget>
    9.  
    10. class QAction;
    11. class QMenu;
    12.  
    13. class Icone : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Icone();
    19.  
    20. protected:
    21. [...]
    22.  
    23. private slots:
    24. [...]
    25.  
    26. private:
    27. // QActions and QMenus
    28. // [...]
    29.  
    30. QSystemTrayIcon *trayIcon;
    31.  
    32. Veilleur mon_veilleur;
    33. };
    34.  
    35. #endif
    To copy to clipboard, switch view to plain text mode 




    Icone.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "Icone.h"
    4.  
    5. Icone::Icone()
    6. {
    7. Creer_actions(); // Initialise the QActions
    8. Creer_menu(); // Initialise the QMenus
    9. Creer_TrayIcon(); // Initialise the trayicon and bind it the QMenus
    10. }
    To copy to clipboard, switch view to plain text mode 




    Veilleur.h
    Qt Code:
    1. #ifndef Veilleur_h
    2. #define Veilleur_h
    3.  
    4. #include "MailChecker.h"
    5.  
    6. #include <QTextStream>
    7. #include <QObject>
    8. #include <QTimer>
    9.  
    10. // Added in order to try to add a QAction
    11. #include <iostream>
    12. #include <QWidget>
    13. class QAction;
    14. class QMenu;
    15.  
    16. class Veilleur : public QObject
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. Veilleur();
    22. [...]
    23.  
    24. private slots:
    25. [...]
    26.  
    27. private:
    28. MailChecker mChecker;
    29. //MailChecker mCheckersecond; // Crash the application if uncommented
    30. //QAction *Act_Tic; // Crash the application if uncommented
    31. //QAction *Act_Ticlol; // Crash the application if uncommented
    32. };
    33. #endif
    To copy to clipboard, switch view to plain text mode 





    Veilleur.cpp
    Qt Code:
    1. #include <iostream>
    2. #include <QApplication>
    3.  
    4. #include "Veilleur.h"
    5.  
    6. Veilleur::Veilleur() : QObject()
    7. {
    8. }
    To copy to clipboard, switch view to plain text mode 




    MailChecker.h
    Qt Code:
    1. #ifndef MailChecker_h
    2. #define MailChecker_h
    3.  
    4. #include <QThread>
    5. #include <QTextStream>
    6. #include <QSettings>
    7. #include <QObject>
    8. #include <QTcpSocket>
    9.  
    10. class QTcpSocket;
    11.  
    12. class MailChecker : public QObject
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MailChecker();
    18. [...]
    19.  
    20. private slots:
    21. void afficher_erreur_socket(QAbstractSocket::SocketError socketError);
    22.  
    23. private:
    24. [...]
    25.  
    26. QTcpSocket *socket_r;
    27. };
    28.  
    29. #endif
    To copy to clipboard, switch view to plain text mode 




    MailChecker.cpp
    Qt Code:
    1. #include <iostream>
    2.  
    3. #include <QApplication>
    4. #include <QHostAddress>
    5. #include <QTcpSocket>
    6. #include <QStringList>
    7.  
    8. #include "MailChecker.h"
    9.  
    10.  
    11. MailChecker::MailChecker() : QObject()
    12. {
    13. connect(socket_r, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficher_erreur_socket(QAbstractSocket::SocketError)));
    14. }
    15.  
    16. void MailChecker::afficher_erreur_socket(QAbstractSocket::SocketError socketError)
    17. {
    18. switch (socketError)
    19. {
    20. case QAbstractSocket::RemoteHostClosedError:
    21. break;
    22. case QAbstractSocket::HostNotFoundError:
    23. std::cout << "\n ERREUR SOCKET : Host not found" << std::endl;
    24. break;
    25. case QAbstractSocket::ConnectionRefusedError:
    26. std::cout << "\n ERREUR SOCKET : Connection refused by peer" << std::endl;
    27. break;
    28. default:
    29. std::cout << "\n ERREUR SOCKET : \n" << socket_r->errorString().toStdString() << std::endl;
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 



    I'm sure I made my nOOb another time... But I'm a nOOb .
    I can't understand what's the problem,
    I can't understand why the application crashes if I add an object,
    I can't understand why the application has been compile and built but crashes...

    I think it's a class initialisation problem, but I can't see what.

    Perhaps coul you help me ?
    Thanks

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't create an object : initialisation problem ?

    Quote Originally Posted by Nyphel View Post
    Hello,
    Perhaps coul you help me ?
    Thanks
    Your problem is

    Have a look on line 1386
    http://qt-webdav.svn.sourceforge.net...pp?view=markup
    delete mTray; && line 164 void Dav_Main::setupTray() the construct of QSystemTrayIcon

    if you QSystemTrayIcon is not clean qt freeze an crasch .... at end delete QSystemTrayIcon
    otherwise you become many many icons on task ... running on linux && window...

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't create an object : initialisation problem ?

    Thanks patrik08, I think that was an error, but it doesn't resolve my problem .

    It seems that the "Veilleur" class is the only one in wich I can't add objects.

  4. #4
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't create an object : initialisation problem ?

    Small UP, I don't find the solution...
    So perhaps someone may find it

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Can't create an object : initialisation problem ?

    A good practice is to allocate objects derived from QObject on the heap (with operator "new"). QObjects allocated on the stack in together with a parent-child relationship tend to cause problems (*). I suggest diving into docs; QObject, Object Trees and Object Ownership..

    I'm not sure if this is the problem of yours because I can't see any parent being passed for any object in the example piece of code.. But QObject parent-child relationship sure is a concept a developer must understand while developing with Qt.

    (*) A QObject deletes all it's children. A child gets destructed twice in case it was allocated on the stack, 1) by the parent 2) when going out of scope => crash.
    J-P Nurmi

  6. #6
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't create an object : initialisation problem ?

    Hello,

    Someone esle gave me the solution on another forum.
    My error was to use the QTcpSocket in the constructor of MailChecker while it wasn't initialised :

    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. {
    3. connect(socket_r, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficher_erreur_socket(QAbstractSocket::SocketError)));
    4. }
    To copy to clipboard, switch view to plain text mode 

    The good code is :

    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. {
    3. socket_r = new QTcpSocket();
    4.  
    5. connect(socket_r, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficher_erreur_socket(QAbstractSocket::SocketError)));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Is was initialising my Socket directly in a function that :
    - initialise the socket
    - connect to host
    - submit a request
    - listen the answer
    - close the socket
    - destroy the socket

    Thanks Dudule from developpez.net C++ forums



    PS : Thanks jpn, your answer is usefull for me, because I didn't know that

Similar Threads

  1. QT4 scope problem
    By the_bis in forum Newbie
    Replies: 5
    Last Post: 29th January 2007, 23:01
  2. Problem combining QWorkspace & QGLWidget
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2006, 21:45
  3. Replies: 16
    Last Post: 7th March 2006, 15:57
  4. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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.