Hide Application when outside the desktop application is clicked - c++

I made an utility/tray application using flutter and i want it to hide when i click outside the application. How can we do this? I researched quite some time but i could not get the answer to this.
I looked out for window_manager, bitsdojo, googling, youtube and documentation. But could not get the answers.

You can use window_manager, which helps to find the focus.
import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WindowListener {
#override
void initState() {
windowManager.addListener(this);
super.initState();
}
#override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
#override
Widget build(BuildContext context) {
// ...
}
#override
void onWindowFocus() {
// do something when app gets into focus state
}
#override
void onWindowBlur() {
// do something when app gets into inactive/blur state
}
}

Related

How to the same Interface to different QT Classes

Goal
I want to benchmark some drawing libraries and gather diagnostics. I thought of implementing in each benchmark the same interface and then instantiating different classes in the main window.
The Challenge
The problem is that some benchmarks use QWidget and other QOpenGLWidget. So even if I implement the same functionality I can not use it, without dynamic casting to each possible instance.
What I tried so far
My first thought was to create an interface and use virtual multiple inheritance. But that doesn't seem to work and I am not sure if that's even the right solution.
I also thought of the Composite Pattern or Adapter Pattern, but seem some problems, as I want to override some functions of QWidget like resizeEvent in each benchmark. Of course I could duplicate that code or put it into some non-member function. But maybe there is something more elegant?
QWidgets Addin
class BenchmarkAddin : virtual public QWidget {
public:
BenchmarkAddin() {
connect(&timer_, SIGNAL(timeout()), this, SLOT(update()));
}
double get_fps() {
// use frames_ and time to estimate fps
}
void count_frame() {
++frames_;
}
void set_parameters(int param1_) {
param1_;
}
protected:
void resizeEvent(QResizeEvent* event) override {
init();
}
virtual void init() = 0;
int param1_;
private:
int frames_;
QTimer timer_;
}
Raster Benchmark
class RasterBenchmark : public BenchmarkAddin {
protected:
void init() override {
// create buffers
}
void paintEvent(QPaintEvent* event) override {
// do drawing using param1_
count_frame();
}
}
OpenGL benchmark
class OpenGLBenchmark : virtual public QOpenGLWidget, public BenchmarkAddin {
protected:
void paintGL() override {
// do GL drawing using param1_
count_frame();
}
}
Main Window Example Usage
BenchmarkAddin *widget;
if (benchmark == "raster") {
widget = new RasterBenchmark(this);
else
widget = new OpenGLBenchmark(this);
widget.set_parameters(100);
...
std::cout << widget.get_fps() << std::endl;
Obviously this doesn't work, as QOpenGLWidget doesn't use virtual inheritance for QWidget. Also there is a problem with Qt's object meta system.
Question:
Any idea how I could implement an interface that is both accessible within a subclass of QWidget and QOpenGLWidget?

Trying to write an "Observer" pattern

Did I correctly implement this pattern? The idea of my example is that there are document and application (app performs an observer role). At the beginning each document should attach the observer. Further, document could close all documents except himself by emitting a signal to observer (application).
Requirement - example should use an event model.
class Application
{
std::vector<Document *> docs;
public:
void add_document(Document *doc)
{
docs.push_back(doc);
}
public slots:
void close_non_active_docs_button_pressed(Document *active_doc)
{
for (auto idoc: this->docs)
{
if (idoc == active_doc)
continue;
idoc->close();
}
}
}
class Document
{
Application *app;
public:
void attach_to_app(Application *app)
{
this->curr_app = app;
app->add_document(this);
}
void close(){...};
public signals:
void close_non_active_docs_button_pressed(Document *active_doc);
...
emit close_non_active_docs_button_pressed(this);
}
///CONNECT(...)

How to use QWebEnginePage::OpenLinkInNewTab [Qt5.8]

When I click on link to any question in my feed on Quora using this code, the link doesn't open but it doesn't print "Hello". Could you please tell me where am I wrong? I'm pretty sure that link on quora emits the OpenLinkInNewTab signal. Please help, thanks.
class WebView : public QObject {
void newTabRequested() {
std::cout<<"Hello"<<std::endl;
}
public:
char* home_page;
QAction* newTabAction=new QAction();
QWebEngineView* view=new QWebEngineView();
WebView(char* page=(char*)"https://google.com") {
this->home_page=page;
this->exitFullScreen->setShortcut(Qt::Key_Escape);
createWebView();
this->view->settings()
->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows,true);
this->newTabAction=this->view->pageAction(QWebEnginePage::OpenLinkInNewTab);
connect(this->newTabAction,&QAction::toggled,this,&WebView::newTabRequested);
}
void createWebView() {
this->view->load(QUrl(this->home_page));
}
};
I think the problem is that newTabRequested is not a slot. Try
class WebView : public QObject{
Q_OBJECT
private slots:
void newTabRequested(){
std::cout<<"Hello"<<std::endl;
}
// ...
}

How to emulate linkClicked(QUrl) signal in QWebEngineView?

I am porting my project from Qt WebKit to Qt WebEngine in Qt5.6. I want to emit linkClicked(QUrl) signal when a href is clicked on the QWebView, but QWebEngineView has no signal linkClicked(QUrl).
How to emulate linkClickedSignal(QUrl)?
Porting from Qt WebKit to Qt WebEngine.
Thank you #Alexis P. I have got it.
class MyWebPage : public QWebEnginePage
{
Q_OBJECT
public:
MyWebPage(QObject* parent = 0) : QWebEnginePage(parent){}
bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
//QDesktopServices::openUrl(url);
emit linkClicked(url);
return false;
}
return true;
}
signals:
void linkClicked(const QUrl&);
};
In my window class:
webView = new QWebEngineView(ui->verticalLayoutWidget);
webView->setPage(new MyWebPage());
ui->verticalLayout->addWidget(webView);
connect(webView- >page(),SIGNAL(linkClicked(QUrl)),this,SLOT(linkClicked(QUrl)));
I am not sure it will be useful for you, but in my app using QWebEngineView, I have clickable links which must open the corresponding website in a browser.
The way I am doing it is like that :
class MyQWebEnginePage : public QWebEnginePage
{
Q_OBJECT
public:
MyQWebEnginePage(QObject* parent = 0) : QWebEnginePage(parent){}
bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
QDesktopServices::openUrl(url);
return false;
}
return true;
}
};
As you can see, I just reimplemented the virtual method acceptNavigationRequest of QWebEnginePage in order to retrieve the url from the link clicked : url. I don't know it is what you want to achieve, but I hope that helps.

Like Dialog in facebook android sdk is closed immediately

I'm using Facebook Android SDK (3.19) in my app and following this link https://developers.facebook.com/docs/android/like-button to use Like Button. When I clicked like button, Like Dialog was shown and dismissed immediately. Also, onActivityResult was not called. I don't know why. Please somebody help me to fix this. Thanks.
Edit: My code
public class ContentFragment extends Fragment{
LikeView likeView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Settings.sdkInitialize(getActivity());
Settings.setPlatformCompatibilityEnabled(false);
Settings.setIsDebugEnabled(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_fragment, null);
likeView = (LikeView) view.findViewById(R.id.like_view);
likeView.setObjectId("http://shareitexampleapp.parseapp.com/photo1/");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LikeView.handleOnActivityResult(getActivity(), requestCode, resultCode, data);
Toast.makeText(getActivity(), "onActivityResult", Toast.LENGTH_SHORT).show();
}
}
Edit2: CLOSED. It's my mistake. My test account was not added to my facebook app's testers. Thanks.