How do I create a simple Qt console application in C++?

前端 未结 7 1082
别那么骄傲
别那么骄傲 2020-11-28 22:06

I was trying to create a simple console application to try out Qt\'s XML parser. I started a project in VS2008 and got this template:

int main(int argc, char         


        
7条回答
  •  感动是毒
    2020-11-28 22:29

    Here is one simple way you could structure an application if you want an event loop running.

    // main.cpp
    #include 
    
    class Task : public QObject
    {
        Q_OBJECT
    public:
        Task(QObject *parent = 0) : QObject(parent) {}
    
    public slots:
        void run()
        {
            // Do processing here
    
            emit finished();
        }
    
    signals:
        void finished();
    };
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // Task parented to the application so that it
        // will be deleted by the application.
        Task *task = new Task(&a);
    
        // This will cause the application to exit when
        // the task signals finished.    
        QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
    
        // This will run the task from the application event loop.
        QTimer::singleShot(0, task, SLOT(run()));
    
        return a.exec();
    }
    

提交回复
热议问题