Intercettare cambio di focus
Solved
Italian
-
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(); }
-
@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*)));