[Interest] Design idioms for QtCore applications

Jason H scorp1us at yahoo.com
Fri Feb 8 15:20:36 CET 2013

I've written a number of server applications.

They work the same, you just inherit from QCoreApplication and make sure you have events.

It would help to know what you're trying to do, but as long as you're not just processing command line arguments and exiting, you're going to need some way to acquire work (socket maybe?) and then do that work. Look at a QTcpServer example.

 From: K. Frank <kfrank29.c at gmail.com>
To: Qt-interest <interest at qt-project.org> 
Sent: Friday, February 8, 2013 8:02 AM
Subject: [Interest]  Design idioms for QtCore applications
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

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;
     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
Interest mailing list
Interest at qt-project.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130208/dbeb93d4/attachment.html>

More information about the Interest mailing list