Results 1 to 6 of 6

Thread: Static functions and class members

  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Static functions and class members

    I am currently having a problem in using a third party SDK. It involves some Qt, but since it's primarly C++ related I post it here.

    The SDK consists of a range of classes, all derived from the same base class. There is a static callback function in this base class, which is used for error handling. Normally this callback is just set by the user once in the beginning, typically by allowing for some dialog to be displayed when errors occur.

    I would like to use a QErrorMessage object inside my callback function, but there is a problem there. I have a QMainWindow based class, where I construct the QErrorMessage in the QMainWindow constructor. I noticed QErrorMessage can not be created on the stack, otherwise the dialog disappears immediately. So I have to do it this way.

    Now, if I don't make the QMainWindow class the parent of QErrorMessage, I get weird behaviour. In the other scenario, I have a whole lot of problems. The QErrorMessage object is a member of my QMainWindow class and is not-static. Therefore, it can not be used in my static callback function. If I make the QErrorMessage static, it can not have the QMainWindow object as a parent. And trying to work around this just has me going round and round and ...

    Basically, I'm just looking for a work-around to give static functions (a static error handling function inside my QMainWindow object) access to members of the class. I know it can not be done normally, therefor the work-around question

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Static functions and class members

    Let's start from the beginning - what "weird behaviour" do you get when not having the window as the parent?

  3. #3
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static functions and class members

    There is a problem when multiple errors occur in a row. Normally they should be queued. If I do not make the QMainWindow a parent, only the first error is shown and the others are just dismissed. This has nothing to do with the checkbox for ignoring the same errors in the future, it is just a difference in behaviour regarding the parent selection. I have to admit this does not make that much sense to me.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Static functions and class members

    Do you use threads?

  5. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static functions and class members

    No, I don't use threads. I tried it as a work-around, but got stuck there too. Also seemed to be too complicated for what I need.

    Here is some code to illustrate things :

    .h file :

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "OctoObject.h"
    6.  
    7. class OctoNormalise;
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
    15. ~MainWindow();
    16.  
    17. private:
    18. OctoNormalise* normalise;
    19.  
    20. //All static objects to allow for the use of the static callback function
    21. static QErrorMessage* errorMessage;
    22. static void _stdcall errorHandler(OctoObject::error_code error);
    23.  
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    .cpp file :

    Qt Code:
    1. #include "QTGUI"
    2. #include "mainwindow.h"
    3. #include "OctoNormalise.h"
    4.  
    5. QErrorMessage* MainWindow::errorMessage = 0;
    6.  
    7. MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    8. : QMainWindow(parent, flags)
    9. {
    10. //Create the QErrorMessage Object
    11. errorMessage = new QErrorMessage;
    12.  
    13. //Set the error handler
    14. OctoObject::setErrorHandler(&MainWindow::errorHandler);
    15.  
    16. //Create an object from the SDK as a test (this force the generation of several errors the way it is done now)
    17. normalise = new OctoNormalise;
    18.  
    19. }
    20.  
    21. void __stdcall MainWindow::errorHandler(OctoObject::error_code error)
    22. {
    23. //Select the dialog text based on the error coming from the SDK
    24.  
    25. switch (error)
    26. {
    27. case OctoObject::HaspErr : errorMessage->showMessage(QString("No valid HASP key available")); break;
    28. case OctoObject::FileOpenErr : errorMessage->showMessage(QString("File could not be found/opened")); break;
    29. // ...
    30. }
    31. }
    32.  
    33. MainWindow::~MainWindow()
    34. {
    35. //Delete all used objects
    36. delete normalise;
    37. delete errorMessage;
    38.  
    39. //Resets the error handler to a dummy function (not actually NULL, this is implemented in the SDK to be handled safely)
    40. OctoObject::setErrorHandler(NULL);
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static functions and class members

    Hmm, I noticed there are strange things going on with the callback pointer, it seems to change. Maybe this is not entirely Qt related, I am going to contact the supplier.

    Off course, this does not change the fact it is a pity the static function can not access members of the class. Any suggestions there remain interesting .

Similar Threads

  1. Custom Event or Queued Connection ?
    By vishwanath in forum Qt Programming
    Replies: 2
    Last Post: 22nd November 2006, 13:55

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.