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?
Related
On Linux I can use this pipeline QMimeDatabase::mimeTypeForFile > QMimeType::iconName > QIcon::fromTheme to get icons for files. Afaik the latter works on Linux only.
How can I get icons for mimetype on macOS? Do I have to use icon(for:) and create the pixmap on my own, or are there better ways?
Solution
To get the icon for a file use QFileIconProvider::icon with QFileInfo like that:
QIcon fileIcon = QFileIconProvider().icon(QFileInfo(fileName));
Example
Here is a simple example I wrote for you to demonstrate the proposed solution:
#include <QApplication>
#include <QFileIconProvider>
#include <QFileDialog>
#include <QFileInfo>
#include <QBoxLayout>
#include <QPushButton>
class Widget : public QWidget
{
public:
Widget(QWidget *parent = nullptr) : QWidget(parent) {
auto *l = new QVBoxLayout(this);
auto *btnOpen = new QPushButton(tr("Open File"), this);
btnOpen->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
l->addWidget(btnOpen);
connect(btnOpen, &QPushButton::clicked, this, &Widget::onFileOpen);
resize(300, 300);
}
private slots:
void onFileOpen() {
const QString &fileName(QFileDialog::getOpenFileName(this, tr("Open File")));
if (fileName.isEmpty())
return;
auto *btnOpen = static_cast<QPushButton *>(sender());
btnOpen->setIcon(QFileIconProvider().icon(QFileInfo(fileName)));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
I am building a home security system with RPi and WebRTC. I simply need a way to trigger a browser to open at a given URL and to auto-grant access to the Microphone and Camera. I had hoped to use the WebEngine library with PyQt but WebEngine is not supported in PyQt for RPi. So I am trying Qt itself now. Unfortunately I am not familiar with C++, so i am struggling.
The example here has 90% of what I need. The code is replicated below. I just need to tweak it to grant access to the mic and camera when it is requested. I am hoping someone can assist me with this?
#include <QApplication>
#include <QWebEngineView>
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.qt.io"));
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setUrl(commandLineUrlArgument());
view.resize(1024, 750);
view.show();
return app.exec();
}
I answered this question but for PyQt5: Grant access to Cam & Mic using Python for PyQt WebEngine, I will only do a C ++ translation to Python, the base is the same.
#include <QApplication>
#include <QUrl>
#include <QWebEngineView>
class WebEnginePage: public QWebEnginePage{
Q_OBJECT
public:
WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
}
private Q_SLOTS:
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){
if(feature == QWebEnginePage::MediaAudioCapture
|| feature == QWebEnginePage::MediaVideoCapture
|| feature == QWebEnginePage::MediaAudioVideoCapture)
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
else
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
}
};
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.qt.io"));
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setPage(new WebEnginePage);
view.setUrl(commandLineUrlArgument());
view.resize(1024, 750);
view.show();
return app.exec();
}
#include "main.moc"
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();
...
}
...
}
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();
}
I am trying to hide my QT application from taskbar? I cannot find anything in Google so I asking here.
Solution from Qt Hide Taskbar Item (Qt Hide Taskbar Item) and this->hide() is not helping.
main.cpp
#include "status_bar.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
status_bar w;
w.show();
return a.exec();
}
status_bar.cpp:
#include "status_bar.h"
#include "ui_status_bar.h"
#include <stdlib.h>
#include <QTime>
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
status_bar::status_bar(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::status_bar)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
resize(QApplication::desktop()->width(),36);
ui->time->move(QApplication::desktop()->width()-ui->time->size().width(),10);
ui->username->setText(getenv("USER"));
timeupdate = new QTimer(this);
connect(timeupdate, SIGNAL(timeout()),
this, SLOT(UpdateClock()));
timeupdate->start(100);
}
void status_bar::UpdateClock()
{
ui->time->setText(QTime::currentTime().toString("HH:mm"));
}
status_bar::~status_bar()
{
delete ui;
}
EDIT:
With code like this window is empty.
class MyWindowWidget : public QWidget
{
public:
MyWindowWidget(QWidget *parent)
: QWidget(parent, Qt::Dialog)
{
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
status_bar window;
MyWindowWidget widget(&window);
widget.show();
return app.exec();
}
Solved by using Qt::Tool flag.
Qt::Tool flag has other problems for me, like this widget/windows is hidden when its state becomes inactive.
I would recommend you to use Qt::ToolTip