MethylBlue
  1. Filelight
  2. Codeine
  3. Wocka
  4. Blog
  5. Detritus
  6. Home
RSS

Overriding Qt Classes

February 26th, 2008

We had a need to override the QSystemTrayIcon::showMessage() function on mac specifically across multiple projects. The trouble is ensuring that developers use the override function even if they don’t know about it. So:


namespace Moose
{
    class QSystemTrayIcon : public ::QSystemTrayIcon
    {
    public:
        void showMessage( const QString& title, const QString& text )
        {
            //code
        }
    };
}

#define QSystemTrayIcon Moose::QSystemTrayIcon

Name the file “QSystemTrayIcon” and save it in a directory like QtOverrides. Then add the following to your qmake project file:

CXX = $$CXX -IQtOverrides

Which forces our override directory to be earlier in the include path than the Qt includes.

This method scares me somewhat, due to the define, but I couldn’t see a neater way of doing it.

Now if a developer #includes <QSystemTrayIcon> they will be using our stealth reimplementation.

Of course this method isn’t virtual, but that shouldn’t be a problem, people don’t tend to use QWidgets in a way that requires polymorphism. Well that’s a lie, but most methods are safe, on final-type classes.

Respond »