Saturday, February 16, 2013

QT Console Application Template Tutorial

Writing a command line program in QT may be a little harder than you would expect; particularly if you want to use the full features of QT including Signals, Slots and Threads.

If you start writing a QT program as a standard “main” program in “c” none of the QT messaging structure will operate as expected.

I have written many QT command line programs, some are commercial products.  Here is a template that I have developed which seems to work well.

Lets get started:

Here is the main.cpp file where it all starts.

#include <QtCore/QCoreApplication>
#include <QTimer>
#include "mainclass.h"
int main(int argc, char *argv[])
    QCoreApplication app(argc, argv);

    // create the main class
    MainClass myMain;

    // connect up the signals
    QObject::connect(&myMain, SIGNAL(finished()), 

             &app, SLOT(quit()));
    QObject::connect(&app, SIGNAL(aboutToQuit()), 

             &myMain, SLOT(aboutToQuitApp()));

    // This code will start the messaging engine in QT and in
    // 10ms it will start the execution in the routine;
    QTimer::singleShot(10, &myMain, SLOT(run()));
    return app.exec();


A few things worth noting here.
  • A instance of the MainClass class called myMain is created.  
  • A signal in that MainClass called "finished" actually quits the application.
  • A signal from the app works with a slot in myMain called aboutToQuitApp
  • A 10ms timer sends a signal to the slot run in the myMain class.  This bootstraps your code.
  • The last line “return app.exec()” starts all of the QT messaging including the Slots and Signals system across various threads.
  • By the time myMain gets the signal on the “run” Slot the QT application structure is up and running.

Now lets create the header file for the MainClass;  mainclass.h

#include <QObject>
#include <QCoreApplication>
class MainClass : public QObject
    QCoreApplication *app;

    explicit MainClass(QObject *parent = 0);
    /// Call this to quit application
    void quit();

    /// Signal to finish, this is connected to Application Quit
    void finished();

public slots:
    /// This is the slot that gets called from main to start everything
    /// but, everthing is set up in the Constructor
    void run();

    /// slot that get signal when that application is about to quit
    void aboutToQuitApp();
#endif // MAINCLASS_H

Lets look at a couple of things here.

  • Call “quit()” when you want to exit the application
  • The “finished” signal is sent to the app to close the application
  • The “run() slot is where your code “starts” gets called 10ms after the application is started
  • “aboutToQuitApp() slot gets called after the “finished” signal is executed by the QT application.

The last thing to look at is the actual mainclass.cpp code.

#include "mainclass.h"
#include <QDebug>
MainClass::MainClass(QObject *parent) :
    // get the instance of the main application
    app = QCoreApplication::instance();
    // setup everything here
    // create any global objects
    // setup debug and warning mode

// 10ms after the application starts this method will run
// all QT messaging is running at this point so threads, signals and slots
// will all work as expected.
void MainClass::run()
    // Add your main code here
    qDebug() << "MainClass.Run is executing";
    // you must call quit when complete or the program will stay in the
    // messaging loop

// call this routine to quit the application
void MainClass::quit()
    // you can do some cleanup here
    // then do emit finished to signal CoreApplication to quit
    emit finished();

// shortly after quit is called the CoreApplication will signal this routine
// this is a good place to delete any objects that were created in the
// constructor and/or to stop any threads
void MainClass::aboutToQuitApp()
    // stop threads
    // sleep(1);   // wait for threads to stop.
    // delete any objects

There are a few things to discuss here:

  • The constructor gets a instance of the QT application and sets it to “app”
  • The “run” slot is where your code will actually start execution.
  • When you are through running your code you must call “quit” to stop the application.  This will tell the QT application to terminate.
  • While the QT application is in the process of terminating it will execute the slot aboutToQuitApp().  This is a good place to do any clean-up work.

Well I hope this helps you get a QT Console application up and running quickly.

Comments are welcome.

Sunday, July 1, 2012

Does Google have the solution for Energy Independence?

Where does the US get their oil?  Depends on where you get your information, but the numbers appear to be in the following range:

·      Domestic 40-50%
·      Friendly Countries; Canada, Mexico and such: 25% - 35%
·      Risky Countries; Persian Gulf, Nigeria, Venezuela: 25% - 30%

For the purpose of this article I am going to separate driving into two categories: 
1)    Places where my human form is needed; Personal Mode
2)    Places where anyone can be on my behalf; Errand Mode

Here are some examples of each:
1)    Personal Mode
a.    Work
b.    Church
c.     Golf
d.    Entertainment
2)    Errand Mode
a.    Grocery Store
b.    Hardware Store
c.     Pharmacy
d.    Walmart

There are no real statistics so I monitored all the driving my family does for one month and found that about 38% of all of our driving was for errands.  I’ll call that typical.

Now to Google:  Google has developed a technology for mapping which auto drives cars.  What if a special vehicle was made that used this technology?  This vehicle would not carry humans, but would be capable of carrying 2-3 cubic feet of goods.  This vehicle would be electric, have no emissions, and could get the equivalent of 150-200mpg. 

Imagine that instead of going to Target, Lowes Hardware or your Pharmacy that you simply go on-line, select what you need.  Instead of driving a gas vehicle (at 20 mpg) to get the product the product is delivered to you within an hour in an electric delivery vehicle (EDV).

EDV’s would be owned by businesses such as Walmart, Lowes  and your local grocery store.

The model is already known to work. Millions of people order from Amazon and take delivery from UPS.  With the EDV concept instead of two days delivery, your order would be delivered within two hours.  Local retailers would be enthusiastic, realizing this mode would allow them to re-gain market share.

With Google’s technology the US can take a huge step in Energy Independence and Carbon Emissions.  Time to get started…Google.

Saturday, March 10, 2012

Window Phone 7.5 Reiew

I am a big fan of Windows Phone 7, I love the interface.  I find it easy to use and super clean.   

As a developer I really like the development tools for the Windows Phone.  I have found that WP7 development tools/language run circles around the tools for Android and iPhone. 

I don’t think that my application needs are that strange compared to other users but I have found that the application lineup for WP7 to be very lacking.

I have build a table here; it kind of mixes up categories and programs somewhat, but I think that you will get the general idea.

Promised forever for the WP7
Podcast Player
iCatcher and Downcast are great programs
DoggCatcher and PodCatcher are both great
WP7 Apps are just awful.  None of them allow you to make a playlist.
4.5 Stars
4.5 Stars
Who knows when
2.5 Stars
3.5 Stars
3.0 Stars

2.5 Stars
4.5 Stars
4.0 Stars
3.0 Stars

ESPN ScoreCenter
3.5 Stars
4.2 Stars
4.0 Stars
I think this app is much better on the WP7
Yahoo Sportacular
4.5 Stars
4.5 Stars
Best sports app is not available on WP7
News Consolidators
FlipBoard and Currents are great
Google Currents only, but good.

Weather Apps
Lots of good choices
Lots of good choices
Lots of good choices

Paid apps are good
Free Google Navigation is good
No good choices

3.5 Stars
4.5 Stars
2.5 Stars very weak

3.0 Stars
4.0 Stars
Not even usable

4.5 Stars
4.6 Stars
3.5 Stars



No app but you can use the integrated client.

Dropbox, SkyDrive, SugarSync
Yes, Yes, Yes
All work great
Yes, 3rd party app, Yes.  All work great
No, Yes, No Skydrive works well

Email Client
Integrated Email Client is good.
None of the Email Clients are as good as iPhone.  Interfaces are clumsy.
Integrated Email Client is good; very clean.

A couple of conclusions you can make from this chart.
  • Windows Phone 7 is missing many important apps.
  • Many of the apps for the Windows Phone are sub-par as compared to both the Android and iPhone counterparts. 
  •  Android apps have increased in number and quality over the last 1-2 years. 
  • You can’t use the excuse of “lack of apps” to keep you from moving from the iPhone to Android. 
  • You can use the excuse of “lack of apps” to keep you from moving to WP7.
I would love to move to the Window Phone; but they have some work to do.