Qt World Summit: Register Today!

exaplaination of SendMessage of MFC equivalent in Qt

  • Hi Guys,
    I am porting some files from existing VC++ project into qt and there I have seen sendmessage function
    example :
    @SendMessage( dialog_->m_hWnd, WM_MULTIBOXMESSAGE, 0, 0 );@

    where @WM_MULTIBOXMESSAGE@ is just a #defined variable declared in some files it is Used to define private messages for use by private window classes, usually of the form WM_USER+x, where x is an integer value. so here it is WM_MULTIBOXMESSAGES WM_USER+ 1.

    I am wondering what exactly this bq. sendmessage do in Windows GUI and if there is equivalent of this in Qt.

    I have searched a lot in google but I really could not able to understand its usage . SO could you guys please explain it to me in Layman's language. Thanks

  • Can someone please help me ???

  • Lifetime Qt Champion


    Please allow at least 24 to 72 hours before bumping your own thread. Not all people on this forum live in the same timezone as you.

    As for your question, depending on the result of SendMessage, you might use signals & slots, or events etc.. The best is to analyze what your MFC software does and then compare with Qt's examples/demos and the documentation.

    Hope it helps

  • Thanks Mate for replying me ... Though I was just interested in some simple explanation of what exactly SendMessage do..

  • "SendMessage explanation":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

    do not mix up with "PostMessage":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx

    For a pure Qt environment, the equivalent to sending a user message would be to use signals and slots.

    (Side note: assuming here that both signal sender and receiver are in the same thread - if they are not, the behavior is more like the one described under PostMessage above).

    The above links do not work, because this forum engine tries to be smart about them.
    Here are the plain links for copy and paste:

    SendMessage: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx
    PostMessage: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx@

  • Hi Guys , Thanks for reply. What I am trying to achieve are as follows:

    1. Qt is connected to firmware and different threads in qt are generating messages through different dialog boxes . I want to display all these messages into the QTablewidget of parent window. Now do you guys suggest me to go with signal and slots .? I think its bit difficult with signals and slots as how would the Qt application knows when it received the message. Secondly Is it possible I can do something from sendEvent like function ?

  • When using SendMessage from different threads, the event function that is called will run in the context of the thread that called it. For one thing, this doesn't work with user interfaces - they must be in the main thread in Qt.
    (When using other libraries, it is possible to have a window run in a different thread - but even then you should have very good reasons for that. An architecture where all user interactions are handled in the single GUI thread is usually safer).

    Signals and Slots provide a safer way: When connecting a signal from one thread to a slot from another thread, the message will be dispatched via the event loop (using PostMessage internally), the slot will be called in the context of the receiving thread.

  • Thanks Robot for the nice explanation. Well I will move ahead and try to implement the gui as suggested by you guys. In case if I face any issues , I would like to trouble you guys again. Thanks a lot

  • bq. For a pure Qt environment, the equivalent to sending a user message would be to use signals and slots.

    imho that is not true ... qt has the possibility to define custom/user events and dispatch them which is something different than signal and slots

    bq. When using SendMessage from different threads, the event function that is called will run in the context of the thread that called it

    I found it confusing, what is event function? ... on windows SendMessage can be used between different threads, I think the msdn doc is self explanatory

    ok, signal and slots is most proposed and widely used to communicate between objects living in different threads, but afaik there are scenarios when it could not be applied: for instance I was in the situation to compile an OSD application over a dvb core code, multithreaded, without being allowed to change it since it was developed and maintained by other non qt developer, and needed to take data updated by threads and show in OSD(GUI) avoiding qt's policy for creating widgets and updating them ...

    so I think it can be send events between different threads with an implementation like:

    @class MyApplication: public QApplication
    MyApplication(int &argc, char **argv):QApplication(argc,argv){}

    bool notify(QObject * receiver, QEvent * event)
        if(QString(receiver->metaObject()->className()) == "QTableWidget" && event->type()==QEvent::KeyPress)
            return true;
        return QApplication::notify(receiver,event);


    ... actually qt checks if current thread and receiver's thread are different in the QApplication::notify() default implementaion

    in my case above I used something different, attaching pointers to data and getting them in a gui timer, because I was not constrained to be synchronized with any data change ...

  • Hi Folks,
    Porting VS2010 project in QT .

    I guess, I wasn't very clear with my earlier post so here I am explaining it again.

    The issue is .. I have lots of sub Qdialog windows which when user click generates some messages. I want those messages to be on my QTablewidget of my main Application window.
    Now As suggested by some members that I should look how things have done in VS2010 and try to replicate same in QT . So Here is my design .. Please let me know your suggestion /criticism.

    1. vs 2010 -> On Main application window on @MESSAGE_MAP@ we have
      @ON_MESSAGE( WM_NOTICE, OnAddMessage )@
      where WM_NOTICE = WM_USER+1;

    doing same in QT I need signal and slot . so Something like
    @connect( sender , SIGNAL(QtSingleApplication::messageReceived ( const QString &message ) ) , this , SLOT ( on_add_message( const QString & message ) );@
    now what should I replace here with

      • 'sender' ? , who will be the sender in my case ?,
    • SIGNAL (QtSingleApplication::messageReceived ) is right ?

    • Slot -- there is no issue here .. I can implement that code in which I will place the message in QTable widegt in sorting order.

    1. Now if I look into inner QDialog windows source code of existing project which was developed in VC++
      they have something like

    @void Message_information::add( const SMS& message )
    //SMS is a structure and fields are SYSTEMTIME, Enum , CString
    CCriticalSection critical_section;
    CSingleLock lock( &critical_section, true );
    messages_.insert( message ); // where messages_ is an object std::multiset

    SendMessage( dialog_->m_hWnd, WM_MULTIBOXMESSAGE, 0, 0 );

    Now doing same in Qt

    @void Message_information::add( const SMS& message )
    QMutex mutex;
    messages_.insert( message ); // where messages_ is an object std::multiset

    //SendMessage( dialog_->m_hWnd, WM_MULTIBOXMESSAGE, 0, 0 );
    QtSingleApplication::sendMessage ( // send multiset values here );

    } @

    What paramemter should I Add in SendMessage? IS infact sendMessage is correct function to call?

    This 'add' function is being called somewhere else .
    know this sounds duplicate of other questions and I have looked into the link provided my some members but I am sorry I couldn't able to grasp much. --
    Any suggestion or criticism might help me .. Thanks Thanks a lot for al the help

  • I'm still not sure I fully understand your situation, but I'll try.

    As far as I understand, you have existing dialogs based on QDialog. You have a main windows which contains a QTableWidget. I assume your main window is based on QWidget or QMainWindow.

    I assume that your main window has some way to know the dialogs (a list of pointers, or something like that).

    A simple way to send a message from a dialog would be to

    • Add a signal to the dialog
    • Add a slot to the main window
    • Connect the signal with the slot whenever a new dialog is opened.

    The connect would look something like this:
    @connect(pDialog, SIGNAL(notifyMessageReceived(QString)),pMainWindow,SLOT(handleMessage(QString)));@

  • Hello pratsam,

    If you want to use SendMessage() MSDN functions or any other functions into Qt project then you can.
    -For that you must have Qt under MSVC .
    -You have to include the path in your .pro file


    INCLUDEPATH += "C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include"
    LIBS +="C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib/user32.lib"
    //choose your path on window 7
    then add header files that you want in .h/.cpp file.

    and then you will have SendMessage() like functions in your project.

    Hope It'll help you..

  • bq. -For that you must have Qt under MSVC .

    imho, on windows you can use whatever compiler or IDE with WIN API: i.e. Qt with mingw should be fine too

    for catching the native windows messages you should use:

    bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray & eventType, void * message, long * result) [pure virtual]


    bool QWidget::nativeEvent(const QByteArray & eventType, void * message, long * result) [virtual protected]

  • Thanks everyone for the help.
    Well @NIcu , IamSumit .. I am very new to GUI programming in QT as well as in windows.

    I am developing qt application in vs2010 with qt plugin and CMake builds the .sln and project files in VS2010. so In short Im not using .pro files.

    ii) Secondly I have added #include<Windows.h> #include<WinUser.h> in my sub class windows.cpp amd it doesnt throw me error relating to header files so this is positive.

    When I searched on google I have found that to send messages from sub class Dialog windows to main window in VS2010 we need to follow following steps:

    1. @define WM_MULTIBOXMESSAGE i@n parent windows
    2. declare the prototype for example
      @ afx_msg LRESULT OnOffsetChange(WPARAM, LPARAM);@
      but If I include header file for afx_msg ith throws an error
    • Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]

    *so instead on afx_msg can i use void function?
    3) if i define sendmessage in my child window how would I access it on my main window ? I know for signal and slots I can emit signal on child window and catch it in slot in mainwindow but here how would I do ?


    1) f"or catching the native windows messages you should use:" -> you mean to catch the message via sendMessage function ? in parent class window ?

    1. @_bool QWidget::nativeEvent(const QByteArray & eventType, void * message, long * result) [virtual protected]_@ ->

    do I need to implement the above function in parent class ? and for that do I need to use signal and slots to catch the message or do I need to to use VS specifi cfunction like @ON_MESSAGE(WM_OFFSETCHANGE, OnOffsetChange)@ function

Log in to reply