SendMessage of MFC equivalent in Qt

2020-07-13 06:07发布

问题:

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 in

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.

2) 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;
  mutex.lock();
messages_.insert( message ); // where messages_ is an object std::multiset

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

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

this ‘add’ function is being called somewhere else . I 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 .. hanks a lot for al the help

回答1:

It appears that in your case you have multiple QDialogs, which should send something to a single MainApplication, right?

Is there a particular reason you cannot do it via direct function call? Such as:

MyMainWindows * pMainWindow;

...

void MyMainWindows::addMessage( const SMS& message )
{
...
}

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

  pMainWindow->addMessage( messages_ );
  mutex.unlock();
}

This would have the same effect as signal-slot with direct connection, and close to what SendMessage does.

If there is any particular reason you cannot use this construction, please identify it as it will make a difference what type of signal/slot you should use.

If you are fine with this construction but would like to use signals-slots instead of direct calls, also please let us know as it is fairly easy to convert this into signal-slot code (as long as your app is running the event loop and the code generating signals is inherited from QObject)