Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Start external application. Lost focus.



  • Hi I run VLC as a process and everything ok only my application loses focus and I can not kill it by pressing the button, because first I have to click back to the application window. Anyone know how to restore the focus?

    @#ifndef STARTEXTERNALAPPLICATION_H
    #define STARTEXTERNALAPPLICATION_H

    #include <QObject>
    #include <QProcess>

    class StartExternalApplication : public QObject
    {
    Q_OBJECT

    QProcess *myProcess;
    

    public:
    explicit StartExternalApplication(QObject *parent = 0);

    public slots:
    void start();
    void kill();
    };

    #endif // STARTEXTERNALAPPLICATION_H@

    @#include "startexternalapplication.h"
    #include <QApplication>

    StartExternalApplication::StartExternalApplication(QObject *parent) :
    QObject(parent)
    {
    myProcess = new QProcess( qApp );
    }

    void StartExternalApplication::start() {
    QString program = "C://Program Files//VideoLAN//VLC//vlc.exe";
    QStringList arguments;
    arguments << "E:\Photos & Videos\2014\CAM00065.mp4" << "--play-and-exit"; // << "-f";
    myProcess->start( program, arguments );
    }

    void StartExternalApplication::kill() {
    myProcess->kill();
    }@

    @#include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    #include "startexternalapplication.h"

    int main(int argc, char *argv[])
    {
    qmlRegisterType<StartExternalApplication>("StartExternalApplication", 1, 0, "StartExternalApplication");

    QApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    return app.exec&#40;&#41;;
    

    }
    @

    @import QtQuick 2.2
    import QtQuick.Controls 1.1
    import StartExternalApplication 1.0

    ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("StartExternalApplication")

    StartExternalApplication {
        id: app
    }
    
    Rectangle {
        width: parent.width /2
        height: parent.height
        color: "yellow"
        Text {
            text: qsTr("Click to open E:\\Photos & Videos\\2014\\CAM00065.mp4 in VLC")
            anchors.centerIn: parent
        }
        MouseArea {
            anchors.fill: parent
            onClicked: app.start()
        }
    }
    
    Rectangle {
        x: parent.width /2
        width: parent.width /2
        height: parent.height
        color: "blue"
        Text {
            text: qsTr("Press Space to kill process")
            anchors.centerIn: parent
        }
        Item {
            anchors.fill: parent
            focus: true
            Keys.onPressed: {
                if (event.key === Qt.Key_Space)
                    app.kill()
            }
        }
    }
    

    }
    @



  • Hi, you could try waiting for VLC to finish and then take back the keyboard focus, for example, after
    myProcess->start( program, arguments ); add
    myProcess->waitForFinished(-1);
    and then get keyboard focus back to your app by calling activateWindow().



  • [quote author="hskoglund" date="1410639442"]Hi, you could try waiting for VLC to finish and then take back the keyboard focus, for example, after
    myProcess->start( program, arguments ); add
    myProcess->waitForFinished(-1);
    and then get keyboard focus back to your app by calling activateWindow().
    [/quote]

    After closing VLC focus reintroduced. I would like to focus was restored immediately after the start of the process.


Log in to reply