Qt GUI app: warning if QObject::connect() failed?

半城伤御伤魂 提交于 2019-12-05 01:34:21

Call the static function QErrorMessage::qtHandler().

As per the documentation, this 'installs a message handler using qInstallMsgHandler() and creates a QErrorMessage that displays qDebug(), qWarning() and qFatal() messages'.

Alternatively, install a message handler with qInstallMsgHandler().

Another alternative (described in a qt-interest post) is something like this:

#ifdef _DEBUG
#define connect( connectStmt ) Q_ASSERT( connect( connectStmt ) ) 
#endif

...and for what it's worth, here are some signals and slots debugging suggestions I compiled: http://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/

Esben Mose Hansen

The solution I like for this is to set

QT_FATAL_WARNINGS=1

in the environment of the program when you debug. That makes the program crash, giving you a nice backtrace, especially if you run the code in a debugger. If you don't want the crash, see the answer above.

My approach is to rebind the Qt logging engine with qInstallMsgHandler and do my own logging both to file and console.

This way, I know that all of the error/warning messages are recorded and I can analyze them even after the program has stopped executing.

P.S: QtCreator intercepts those messages and displays them in the application output pane.

If your using Visual Studio you can add a console to any QT application.
Go to the project properties, under Linker->Settings change the "SubSystem" to say "Console"

Now recompile your code and you'll the console will appear when you activate the application. If you want to get rid of it, just change the SubSystem again to "Windows"

I'm not sure if this is possibly with QtCreator.

Another option is to use native win32 calls like AttachConsole() to manually create the console and attach it to stdout and stderr. see here for more details on this.

Most of the time I only want some attention now and then: Just put a breakpoint on line "int dummyPutBreakpointHere= 23;"

in main.C:

static QtMessageHandler defaultMessageHandler;
void myRazorsharpMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) 
{
    if ( type > QtDebugMsg ) {
        int dummyPutBreakpointHere= 23;
    }
    defaultMessageHandler(type, context, msg);
}
...
later in main(): defaultMessageHandler= qInstallMessageHandler(0);
Patrice Bernassola

You can use the official Qt IDE: QtCreator. It contains an output console where you will see any problem with signals. Signal error are output in debug AND release run.

stijn

you can redirect stdout/stderr quite easily: make a class that derives from std::basic_streambuf and overloads xsputn() and overflow(), then use eg std::cerr.rdbuf( instanceOfYourRedirectClass ) to redirect all stderr ouptut to a callback function you supply.

Here's a simplified version of what I use; depending on your needs you might have to add extra logic to fiddle with the handling of end of line characters etc.

template< class Elem = char, class Tr = std::char_traits<Elem> >
class Redirector : public std::basic_streambuf<Elem, Tr>
{
  typedef void (*pfncb) ( const Elem*, std::streamsize );

public:
  Redirector( std::ostream& a_Stream, pfncb a_Cb ) :
    m_Stream( a_Stream ),
    m_pCbFunc( a_Cb ),
  {
      //redirect stream
    m_pBuf = m_Stream.rdbuf( this );
  };

  ~Redirector()
  {
      //restore stream
    m_Stream.rdbuf( m_pBuf );
  }

  std::streamsize xsputn( const Elem* _Ptr, std::streamsize _Count )
  {
    m_pCbFunc( _Ptr, _Count );
    return _Count;
  }

  typename Tr::int_type overflow( typename Tr::int_type v )
  {
    Elem ch = Tr::to_char_type( v );
    m_pCbFunc( &ch, 1 );
    return Tr::not_eof( v );
  }

 protected:
  std::basic_ostream<Elem, Tr>& m_Stream;
  std::streambuf*               m_pBuf;
  pfncb                         m_pCbFunc;
};

Usage:

  void outcallback( const char *ptr, std::streamsize count )
  {
    if( *ptr != gc_cEOL )  //ignore eof
      OutputDebugString( ptr );
  }

  Redirector<> redirect( std::cout, mycallback );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!