Cross platform development for Windows and Mac OS X

Killing two birds with one stone

Internet Security Threat Report 2014

VS.NET 2005 Goes Cross-Platform

The Designer tool can be executed stand-alone, but if you’re developing on Windows, it can be tightly integrated into VS.NET 2005 through a VSIP plug-in. (Like what Borland should have done with Delphi, but don’t get me started on that…) With the plug-in installed, VS.NET loses its mild-mannered .NET persona, ripping off its T-shirt to reveal a fully-fledged C++ cross platform development system with a very credible GUI front-end. Did the ill-fated Borland C++BuilderX project get you slobbering after the idea of a cross-platform version of C++ Builder? Well, slobber no more; this is the reality.

What’s especially cool is the way in which the plug-in handles all the build steps behind the scenes. What Designer generates is an XML file with an extension of .ui – for user interface. The XML describes all the widgets on the form, their property settings, even the position of the little signal/slot connection boxes when viewing in this mode. The .ui file is passed as input to another Qt utility – uic, or User interface Compiler, which converts the user interface arrangement into a C++ header file prefixed with “ui_” that’s integrated it into your project. Like I said, all these machinations are transparent; it’s just a matter of hitting F5 in VS.NET and it all happens.

To show how this all pans out in practice, I took a very simple program from page 7 of the excellent book “C++ GUI Programming with Qt3” by Jasmin Blanchette and Mark Summerfield (Note: I understand that there’s a Qt 4 update to this book available – I’ve just ordered it from Amazon! [you can also buy this book at Register Books - Ed]) and built it on the Mac using Trolltech’s command-line tools.

Figure 3: Screenshot of a sample Qt program running on the Mac.

Unfortunately, the program didn’t work “as is” because I’m using Qt 4 and some of the classes used by the authors (most notably QHBox, one of the layout controls) are now deprecated.

For copyright reasons, I can’t show you the revised code listing because it’s probably too similar to theirs but, basically, I used a QWidget control as a containing box, adding the spin-box and slider to the box. I then created a horizontal layout control, QHBoxLayout, and used the addWidget method to add the two controls to this layout. Finally, the setLayout method is used to add the horizontal layout control to the containing box. This is all explained in Trolltech’s excellent reference documentation; and you can see the final result running on the Mac in Figure 3.

I typed this code in by hand, but I wouldn’t want to do this manually with a complex user interface. So, over to Windows and I repeated the exercise, this time entering absolutely no code, but using the Designer to lay out the form and connect the relevant slots on the slider and spin-box.

The heart of what’s generated by the uic compiler is a class called Ui_QTVCDemoClass (the project was called QTVCDemo, and the base form class is QTVCDemoClass). When the program starts running, a special method of Ui_QTVCDemoClass, setupUi, is called, passing it the instance of the form class that needs to be initialised. The code below shows the un-retouched output generated by uic:


#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QMainWindow>
#include <QtGui/QSlider>
#include <QtGui/QSpinBox>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_QTVCDemoClass
    QWidget *centralWidget;
    QHBoxLayout *hboxLayout;
    QHBoxLayout *hboxLayout1;
    QSpinBox *spinBox;
    QSlider *horizontalSlider;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *QTVCDemoClass)
    QTVCDemoClass->resize(QSize(346, 58).expandedTo(QTVCDemoClass->minimumSizeHint()));
    centralWidget = new QWidget(QTVCDemoClass);
    hboxLayout = new QHBoxLayout(centralWidget);
    hboxLayout1 = new QHBoxLayout();
    spinBox = new QSpinBox(centralWidget);


    horizontalSlider = new QSlider(centralWidget);



    statusBar = new QStatusBar(QTVCDemoClass);
    statusBar->setGeometry(QRect(0, 39, 346, 19));
    QObject::connect(spinBox, SIGNAL(valueChanged(int)), horizontalSlider, SLOT(setValue(int)));
    QObject::connect(horizontalSlider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));

    } // setupUi

    void retranslateUi(QMainWindow *QTVCDemoClass)
                              "Enter Your Age", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

namespace Ui {
    class QTVCDemoClass: public Ui_QTVCDemoClass {};
} // namespace Ui

#endif // UI_QTVCDEMO_H

Notice the way that the slider’s valueChanged signal causes the spin-box value to update, whereas the spin-box valueChanged signal does the same for the slider. Notice also that the User Interface compiler automatically generates calls to QApplication::translate, making it dead easy to localise your application using other tools supplied by Trolltech. Speaking of which, all strings are UTF8 Unicode as well. (Hello, Borland? Are you listening?)

The key point here, of course, is that although there’s a lot of code, I didn’t have to write any of it! Just a few moments spent with Designer and then the job’s done. .NET-savvy readers will immediately realise that, from a functional point of view, Ui_QTVCDemoClass:: setupUi corresponds directly to the InitializeComponent method of the form class spitted out by the .NET form designer.

Figure 4: Screenshot of the previous sample Qt program running on Windows.

Figure 4 shows the Designer-generated code running on Windows XP. Not quite as pretty as the Mac OS X version, but you knew I was going to say that, didn’t you?


Cross platform programming has come a long way since the days of XVT, Zinc and MEWEL. Qt gives you a solid RAD-based development toolkit for creating software indistinguishable from ‘native’ applications. In fact, on the Mac, I find the combination of Qt and C++ far more intuitive than the weird Objective-C language and the frighteningly non-intuitive Interface Builder. If you haven’t yet dipped a toe into cross-platform programming, give it a go! In future articles, I hope to look at other cross-platform solutions such as wxWidgets and Free Pascal. ®

Secure remote control for conventional and virtual desktops

More from The Register

next story
Euro Parliament VOTES to BREAK UP GOOGLE. Er, OK then
It CANNA do it, captain.They DON'T have the POWER!
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
NSA SOURCE CODE LEAK: Information slurp tools to appear online
Now you can run your own intelligence agency
Post-Microsoft, post-PC programming: The portable REVOLUTION
Code jockeys: count up and grab your fabulous tablets
Twitter App Graph exposes smartphone spyware feature
You don't want everyone to compile app lists from your fondleware? BAD LUCK
Microsoft adds video offering to Office 365. Oh NOES, you'll need Adobe Flash
Lovely presentations... but not on your Flash-hating mobe
prev story


Seattle children’s accelerates Citrix login times by 500% with cross-tier insight
Seattle Children’s is a leading research hospital with a large and growing Citrix XenDesktop deployment. See how they used ExtraHop to accelerate launch times.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
Why CIOs should rethink endpoint data protection in the age of mobility
Assessing trends in data protection, specifically with respect to mobile devices, BYOD, and remote employees.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Website security in corporate America
Find out how you rank among other IT managers testing your website's vulnerabilities.