Coding C++ Applications with Qt Designer

by Cameron Laird

The original outline for this article emphasized a C++ tutorial far more than the final version. We've reversed that design and reduced the tutorial to just a sidebar. The first reason for this is there's so much news from the Qt world that needs to be told, as Boudewijn does with such enthusiasm in the article here. Qt's capabilities for embedded systems, for example, are an interesting topic beyond the scope of this piece.

Besides that, the most important thing to learn about C++ coding with Qt Designer is simply that you'll be successful with it. Boudewijn's ten-line Main Window application demonstrates how little it takes to get rolling with Qt Designer once you've properly installed it.

From its beginning, one of Qt's strengths is the documentation Trolltech delivers. Qt programming involves concepts--slots and signals, for example--unfamiliar to beginning C++ coders. Event-handling seems to challenge many programmers. However, the Qt documentation is sufficiently polished to make a good impression on most of the engineers we've met who have tried Qt. Almost without exception, they agree that learning Qt was easier than they expected.

The conclusion: if you want to learn how to code Qt, you'll probably be best served by making a standard installation and studying the material it offers. Make sure you look at the application demonstration.

When it does come time to write your own procedural code, most of it will be quite succinct. All it takes to open a text file into a window for visual editing, for example, is:

void MainWindow::fileOpen()
{
    QFile f( fileName );
    if ( !f.open( IO_ReadOnly ) )
              return;

    QTextStream ts( &f );
    mainEditor->setText( ts.read() );
    mainEditor->setModified( FALSE );
    setCaption( fileName );
    statusBar()->message( 'Loaded document'
                          + fileName, 2000 );
}

If you're experienced with C++, you'll find Qt Designer a satisfying way to program high-quality GUI applications. It nicely balances what-you-see-is-what-you-get conveniences with the precision of being in charge of your own executable code.