Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Intercettare cambio di focus
Qt 6.11 is out! See what's new in the release blog

Intercettare cambio di focus

Scheduled Pinned Locked Moved Solved Italian
3 Posts 2 Posters 1.4k Views
  • 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.
  • B Offline
    B Offline
    Bruschetta
    wrote on last edited by
    #1

    Salve a tutti, sto utilizzando il QWebEngineView (ed in particolare l'esempio minimal) perimplementare un semplice browser borderless da lanciare via console.
    Grazie all'esempio è stato molto facile ma adesso vorrei intercettare il momento in cui l'applicazione perde il focus in maniera tale da poter chiamar euna close() e chiudere cosìilbrowser(dato che è borderless e senza "tasti").

    Sapreste darmi qualche consiglio o link sucome implementare la cosa?Grazie in anticipo per le eventuali risposte.

    #include "mainwindow.h"
    #include <QApplication>
    #include <QApplication>
    #include <QWebEngineView>
    #include <QDebug>
    #include <cstdlib>
    
    QUrl commandLineUrlArgument()
    {
        const QStringList args = QCoreApplication::arguments();
        for (const QString &arg : args.mid(1)) {
            if (!arg.startsWith(QLatin1Char('-')))
                return QUrl::fromUserInput(arg);
        }
        return QUrl(QStringLiteral("https://www.hdblog.it"));
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWebEngineView view;
        view.setUrl(commandLineUrlArgument());
        //Got dimension values of the browser by command line
        if(argc>1)
        {
            int x = atoi(argv[2]);
            int y = atoi(argv[3]);
            view.resize(x, y);
        }else{
            view.resize(1024, 750);
        }
        //Got position values of the browser by command line
        if(argc>3){
            int x = atoi(argv[4]);
            int y = atoi(argv[5]);
             view.move(x,y);
        }else{
             view.move(0,0);
        }
        //Always on top
        view.setWindowFlags(Qt::WindowStaysOnTopHint);
        //Hidetitlebar
        view.setWindowFlags(Qt::CustomizeWindowHint);
        //Without borders
        view.setWindowFlags(Qt::FramelessWindowHint);
        view.show();
    
      
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Prova:

      QObject::connect(&a,&QGuiApplication::applicationStateChanged,&view,[&view](Qt::ApplicationState state)->void{
      if(!(state & Qt::ApplicationActive)) view.close();
      });
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      B 1 Reply Last reply
      1
      • VRoninV VRonin

        Prova:

        QObject::connect(&a,&QGuiApplication::applicationStateChanged,&view,[&view](Qt::ApplicationState state)->void{
        if(!(state & Qt::ApplicationActive)) view.close();
        });
        
        B Offline
        B Offline
        Bruschetta
        wrote on last edited by
        #3

        @VRonin Grazie Ronin perla risposta,proverò anche la tua soluzione, alla fine io ho aggiunto un SIGNAL ed uno watcher

        //Watcher of the focus change
        void Watcher::onFocusChanged(QWidget * old, QWidget * now)
        {
            Q_UNUSED(old);
        
            QWidget * window;
            if (now == 0) {
                window = 0;
            } else {
                window = now->window();
            }
        
            if (window != 0) {
                qDebug() << window->windowTitle();
            } else {
                qDebug() << "No active window";
                exit (EXIT_FAILURE);
            }
        }
        
         Watcher watcher;
        
          QObject::connect(
                      &app, SIGNAL(focusChanged(QWidget*,QWidget*)),
                      &watcher, SLOT(onFocusChanged(QWidget*,QWidget*)));
        
        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