Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Start external application. Lost focus.

Start external application. Lost focus.

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qomg
    wrote on last edited by
    #1

    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()
            }
        }
    }
    

    }
    @

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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().

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qomg
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved