0

In Qt 4.7.1, I'm trying to start the Windows Photo Viewer when I click on a button.

Here is a demo project I've set up in VS2010:

// Container.h
#pragma once

#include <QtCore/QtCore>
#include <QtGui/QtGui>

#include <cstdlib>
class Container : public QObject{
    Q_OBJECT

public:
    QPushButton* m_button;

    Container(){
        m_button = new QPushButton("Open Image");
        bool status = connect(m_button, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
    }

    public slots:
    void onButtonClicked(){

        // works, with console popup:
         system(<path to image>);

        // doesn't work:
        // QProcess::execute(<path to png>);
    }

};

and the main file:

// Main.cpp
#include "stdafx.h"
#include "Container.h"
int main(int argc, char* argv[])
{
 QApplication app(argc, argv);

 Container c;

 c.m_button->show();

return app.exec();
}

My subsystem is set to Windows, for I do not wish a background console:

Now the problem. I would like to display the image without displaying a console window. Calling system() briefly opens a console, closes it and then shows the image. an example call is as follows:

system("G:\\image.png");

So how can I show the image without this console? Many thanks in advance!

EDIT: It has been pointed out this post may be a duplicate of : Qt Execute external program

Sorry, I've tried the solution explained there but could not get it running. In my particular case, I think the Subsystem plays a role.

Community
  • 1
  • 1
Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • Possible duplicate of [Qt Execute external program](http://stackoverflow.com/questions/19442400/qt-execute-external-program) – LogicStuff May 31 '16 at 07:49

1 Answers1

1

Use the QDesktopServices with something like

QDesktopServices::openUrl(QUrl::fromLocalFile(localFilePath));

The openUrl() function can be customized to implement your own dispatch mechanism (does not apply in your case - i think).

OnWhenReady
  • 939
  • 6
  • 15