Overlay while loading page into QWebView - c++

Let's say we have a barebones QWebView:
#include <QApplication>
#include <QWebView>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QWebView view;
view.show();
view.setUrl(QUrl("http://google.com"));
return app.exec();
}
How can I display a graphic overlay, preferably fullscreen with transparency and minimal animation (like timer/beachball/etc.) from when the page starts loading till it's finished? Should also be triggered when url changes from within the QWebView.

You can use the LoadingOverlay class provided in this answer to draw an overlay over any QWidget. In your case, show the overlay on top of the QWebView when the signal loadStarted is triggered and hide it, when the signal loadFinished is triggered.
The following code should get you started. I put the code from the linked answer into overlay.h, the subclass of QWebView which handles the showing/hiding of the overlay is in webview.h:
webview.h
#include "overlay.h"
#include <QWebView>
class WebView : public QWebView
{
Q_OBJECT
public:
WebView(QWidget * parent) : QWebView(parent)
{
overlay = new LoadingOverlay(parent);
connect(this,SIGNAL(loadFinished(bool)),this,SLOT(hideOverlay()));
connect(this,SIGNAL(loadStarted()),this,SLOT(showOverlay()));
}
~WebView()
{
}
public slots:
void showOverlay()
{
overlay->show();
}
void hideOverlay()
{
overlay->hide();
}
private:
LoadingOverlay* overlay;
};
main.cpp
#include <QApplication>
#include "overlay.h"
#include "webview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ContainerWidget base;
Webview w(&base);
base.show();
w.load(QUrl("http://google.com"));
return a.exec();
}

Related

Browser geolocation doesn't work with QWebEngineView

I have a task to implement simple desktop wrapper on QT for our website. Everything works well except for geolocation. Every time browser has to request permission for geolocation usage nothing is happened, geolocation doesn't work. I tried a few solutions from SO and other resources, but nothing helped me. Here is my code:
#include <QLabel>
#include <QWebEnginePage>
class WebEnginePage: public QWebEnginePage{
Q_OBJECT
public:
WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent) {
connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
}
protected:
bool certificateError(const QWebEngineCertificateError &certificateError){
return true;
}
private Q_SLOTS:
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
}
};
#include <QApplication>
#include <QWebEngineView>
#include <QWebEngineSettings>
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setPage(new WebEnginePage());
QWebEngineSettings* settings = view.settings();
settings->setAttribute(QWebEngineSettings::WebAttribute::LocalStorageEnabled, true);
settings->setAttribute(QWebEngineSettings::WebAttribute::AllowGeolocationOnInsecureOrigins, true);
view.setUrl(QUrl(here is my url));
view.resize(1024, 750);
view.show();
return app.exec();
}
Could you give me some suggestions, what's wrong with my code?

How do I make a QTreeView fill the whole dialog and resize with it?

I have a QTreeView and I can't find a way of making it fill the whole dialog window and resize with the window when it is resized.
Something like this:
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include <QTreeView>
class MyDialog: public QDialog
{
public:
MyDialog()
{
QHBoxLayout* l = new QHBoxLayout(this);
setLayout(l);
QTreeView* v = new QTreeView(this);
l->addWidget(v);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog d;
d.exec();
return a.exec();
}

Qt: cursor blinking cause repaint of parent widget?

Bellow you can see minimal example code to demonstrate problem.
If you run it and give focus to QLineEdit, you get output every second: paintEvent, paintEvent and so on.
I can not understand why MyW::paintEvent called on every
cursor blinking in child widget? As you see I do not configure QLineEdit,
by default on my linux box it have no transparent elements,
but still for some reason cursor cause all widgets to redraw their content?
#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QLineEdit>
class MyW final : public QWidget {
public:
MyW() {
//setAutoFillBackground(false);
}
void paintEvent(QPaintEvent *e) {
e->accept();
qDebug("paintEvent");
QPainter painter{this};
painter.fillRect(rect(), Qt::green);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyW w;
w.resize(600, 600);
w.show();
auto le = new QLineEdit{&w};
le->show();
return app.exec();
}

Running Urho3D and Qt from main

I am using the Urho3D engine with Qt for an application. The problem is that both Urho3D and QApplication require to be ran from main(). For now I am using it in separate processes but IPC makes it complicated.
Is there any way to solve this issue? Thanks
My platform is Urho3D 1.5, Qt 4.71 and Windows 7 x64 and VS2015 (C++)
I'm new to both c++ and Urho3D, but I've successfully achieved it.
Simple code, haven't had further test:
awidget.h:
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <Urho3D/Engine/Application.h>
class aWidget : public QWidget
{
Q_OBJECT
public:
explicit aWidget(QWidget *parent = 0)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(pressed()));
}
public slots:
void pressed()
{
Urho3D::Context* context = new Urho3D::Context();
Urho3D::Application *application = new Urho3D::Application(context);
application->Run();
}
};
#endif // AWIDGET_H
main.cpp:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
aWidget *widget = new aWidget();
widget->show();
return app.exec();
}
By the way, I'm using Qt 5.9.0
So the answer is quite simple. Instead of running QApplication by calling
app->exec();
it is needed to manually and regularily call this from your main loop:
app->processEvents();
This will take care that all events used by Qt are processed and QApplication will respond accordingly.
Example:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
bool shallrun = true;
aWidget *widget = new aWidget();
widget->show();
while (shallrun)
{
app->processEvents();
...
}
...
}

Test if QQuickView (QWindow) is full-screen in Qt 5.0.x

For a QWidget we can test if it is displayed in full-screen with flags() & Qt::WindowFullScreen.
The same doesn't seem to work with QQuickView (which is a QQuickWindow which is a QWindow), as QWindow::flags() always returns Qt::WindowMinimized, whatever the reason might be. I display the window using this code:
QQuickView w;
w.setSource(...);
w.show(); /* or */ w.showFullScreen();
In Qt 5.1, QWindow::visibility() was introduced. It returns a new enum type which contains QWindow::FullScreen and behaves properly.
How can I test if a QWindow is shown in full-screen in Qt 5.0.x? I want to implement a "toggle full-screen" function. Keeping track of the current state seems to be the wrong way (yet it would be a possible work-around). I don't understand why QWindow::flags() returns Qt::WindowMinimized...
Code to reproduce issue (press RETURN to see the output of QWindow::flags()):
test.qml
import QtQuick 2.0
Rectangle {
signal test();
width: 100; height: 100
focus: true
Keys.onReturnPressed: test()
}
main.cpp
#include <QGuiApplication>
#include <QQuickView>
#include <QQuickItem>
#include <QDebug>
class Test : public QObject {
Q_OBJECT
public slots:
void test() {
QQuickItem *item = qobject_cast<QQuickItem*>(sender());
QQuickWindow *window = item->window();
qDebug() << window->flags(); // Will print 0x1 == Qt::WindowMinimized
}
};
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QQuickView w;
w.setSource(QUrl("...(relative path to the qml file from above)..."));
w.show(); // Please also test w.showFullScreen();
QObject::connect(w.rootObject(), SIGNAL(test()),
new Test, SLOT(test()));
return a.exec();
}
#include "main.moc"
test.pro
QT += quick
TEMPLATE = app
SOURCES += main.cpp
Use method QWindow::windowState(). It returns key Qt::WindowFullScreen that you seek.
bool isFullScreen = w.windowState().testFlag(Qt::WindowFullScreen);