[Interest] Design idioms for QtCore applications

K. Frank kfrank29.c at gmail.com
Fri Feb 8 14:02:00 CET 2013


Hello List!

I am playing around with non-gui, "QtCore" applications,( i.e., applications
that have #include <QtCore> rather than #include <QtGui>).

(To date, I have been using Qt for gui's.  If an app hasn't had a gui, I
didn't use Qt, and I would write the non-gui "business logic" in my
gui apps in Qt-independent code, adding a little translation layer
if I needed to convert stuff to things like QString.)

I'm looking for general advice on the design philosophy for QtCore
apps, how to use events, and design idioms for simple QtCore
apps.

For example, following much of the Qt example code, I typically
use the following idiom for gui apps:

   #include <QApplication>
   #include "my_main_window.h"
   int main (int argc, char *argv[]) {
     QApplication a(argc, argv);
     MyMainWindow mainWindow;
     mainWindow.show();
     return a.exec();
   }

(For me, MyMainWindow is typically a Qt-Designer built class.)

I never really thought much about how the gui events processed
by MyMainWindow interact with the a.exec() event loop.  I just
chalked it up to the "magic" of the gui framework.

By analogy, I would like to do something like:

   #include <QtCore>
   #include "my_non_gui_class.h"
   int main(int argc, char *argv[]) {
     QCoreApplication a (argc, argv);
     MyNonGuiClass mainObject;
     return a.exec();
   }

Inside of MyNonGuiClass I might like to do something like

   while (dontStop) {
     std::string command;
     std::cin >> command;
     processCommand (command);
   }

but, of course, the call to "std::cin >>" would block until input is provided.

I think part of what I'm missing is the connection between the event
loop, "a.exec()", and processing console input without blocking.

(I understand that one way to deal with blocking calls is to wrap them
in their own threads, but I'm looking for an approach more in the spirit
of a simple, single-threaded Qt-gui hello-world app.)

I hope it's clear what I'm trying to ask.

Any thoughts or advice on both how to do it and how it works under
the hood would be appreciated.


K. Frank



More information about the Interest mailing list