I'm having a WebBrowser application that uses the QWebEngineView (Qt 5.9). I want to load a page, where a Youtube video is embedded. The page loads perfectly but the video is not working.
The message I get when it tries to start is:Requests to the server have been blocked by an extension.
I tried to activate plugins and set the feature permission but none of it makes any difference.
m_webView->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
m_webView->page()->setFeaturePermission(QUrl("https://www.youtube.com/watch?v=rNSnfXl1ZjU"),
QWebEnginePage::MediaAudioVideoCapture,
QWebEnginePage::PermissionGrantedByUser);
I'm kind of new to Qt. Do I need to enable video codecs?
Thank you very much in advance, I hope you can help me.
The following small App, works as desired. In case that I uncomment the settings section in my small App, it locks the playing of the YouTube video.
I suggest that you manually activate all settings one by one to figure out what prevents the video from running in your environment.
The console output might be also very helpful to find the cause for your problem.
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <QApplication>
int main(int argc, char **args)
{
QApplication app(argc, args);
auto view = new QWebEngineView;
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::AllowGeolocationOnInsecureOrigins, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::Accelerated2dCanvasEnabled, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::AllowRunningInsecureContent, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::AllowWindowActivationFromJavaScript, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::JavascriptCanAccessClipboard, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::FullScreenSupportEnabled, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::JavascriptCanOpenWindows, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::JavascriptEnabled, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::ScreenCaptureEnabled, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::PluginsEnabled, false);
//view->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalStorageEnabled, false);
view->setUrl(QUrl("https://www.youtube.com/watch?v=rNSnfXl1ZjU"));
view->show();
app.exec();
}
With all settings deactivated I'm left with the following console output:
js: Refused to display 'https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252Fsignin_passive%26hl%3Dde%26feature%3Dpassive%26app%3Ddesktop%26action_handle_signin%3Dtrue&hl=de&passive=true&service=youtube&uilel=3' in a frame because it set 'X-Frame-Options' to 'deny'.
Related
I'm running a Qt 5.9.4 app with QWebView on a touch linux device displaying HTML webpages.
When users pinch their fingers on the screen, they zoom into the webpage. I want to disable that behaviour, because all the webpages should be 100% fullscreen.
I could temporarily disable it in the webpages (since I own their code) by adding user-scalable=no to the viewport, but that wont work in the long run because not every webpage will have that.
I tried to use eventFilter on different elements of my app, but couldn't get any Gesture or even Mouse event to catch.
This is how I create my webview (in a QDialog):
void Dialog::createWebView() {
view = new QWebEngineView(this);
profile = new QWebEngineProfile();
page = new QWeEnginePage(profile);
view->setPage(page);
view->setUrl(QUrl(path));
}
This is my eventFilter class:
EvFilter::EvFilter(QObject *parent) : QObject(parent)
{
}
bool EvFilter::eventFilter(QObject *obj, QEvent *event)
{
qDebug() << event->type() << "\n";
return QObject::eventFilter(obj, event);
}
I have tried doing
EvFilter* evf = new EvFilter();
view->installEventFilter(evf);
And also on every other element (profile, page, dialog) but couldn't seem to get any events corresponding to mouse or gesture.
What am I doing wrong? How could I prevent that behaviour in the entire webview?
After adding the event listener to the QApplication object, I can detect TouchBegin, TouchUpdate, TouchCancel and TouchEnd events, but nothing else (no Gesture event). I dont know how could I detect if the touchevent is the zoom gesture.
I think you could use this on the Dialog (didn't test it). I had some other widget showing a QML QtWebView and what I did to prevent the widget's children to get Pinch Gestures is:
this->grabGesture(Qt::PinchGesture, Qt::DontStartGestureOnChildren);
This prevents the underlying widget from receiving the PinchGesture, so that you can handle it yourself.
I need to update screen to show how button is moving.
Here is my code:
void mouseReleaseEvent(QMouseEvent *event){
double b=(button->x()*event->y())/(button->x()-1);
double k=(button->y()-b)/button->x();
int time=0;
fnl=false;
if(event->button()==Qt::LeftButton)
{
while(!fnl)
{
int mX=button->x()-1;
int mY=k*(button->x()-1)+b;
button->setText(QString::number(b));
button->move(mX,mY);
QThread::sleep(1);
//here I need to update screen and show button
}
}
}
But it does not update GUI. It simply plays inside the loop.
Never use QThread::sleep() in the GUI thread, It will block the GUI thread from doing anything. Instead, You can use QTimer to schedule something to run at a later point in time. Also, Your slots/functions should be as short and optimized as possible in order to return control to the event loop and be able to handle other events that may happen.
You may want to have a look at this question for a similar problem.
The same technique can be applied here to solve the problem by replacing your while loop with a slot and a QTimer whose interval is set to 0. But Qt can do all that job using the animation framework, Here is an example of a button that gets moved when clicked:
#include <QtWidgets>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
//create and show button
QPushButton button("Animated Button");
button.move(QPoint(100, 100));
button.show();
//create property animator object that works on the position of the button
QPropertyAnimation animation(&button, "pos");
//set duration for the animation process to 500ms
animation.setDuration(500);
//when the button is clicked. . .
QObject::connect(&button, &QPushButton::clicked, [&]{
//set the starting point of the animation to the current position
animation.setStartValue(button.pos());
//set the ending point to (250, 250)
animation.setEndValue(QPoint(250, 250));
//start animation
animation.start();
});
return a.exec();
}
Qt also provides many examples for using the animation framework. . .
A timer is the best option. If you want to use brute force you can call
qApp->processEvents();
inside your loop. Ugly but gets the job done.
I am trying to print an image file on printer using QWebview but instead of image blank page is printed. Please find the below code.
void ChartViewer::onprintBtnClicked()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open File",QString(),"Pdf File(*.png)");
qDebug()<<"Print file name is "<<fileName;
if(fileName.endsWith(".png"))
{
QPrinter printer;
QWebView *view = new QWebView;
QPrintDialog *dlg = new QPrintDialog(&printer,this);
printer.setOutputFileName(fileName);
if(dlg->exec() != QDialog::Accepted)
return;
view->load(fileName);
view->print(&printer);
}
}
If I use view->show() then it has shown the image properly but printed page is coming blank. Request you to please look into the above code and correct me where I am doing wrong.
Regards,
Lekhraj
You load some png file into fileName. Then you set QPrinter to print to that png file with printer.setOutputFileName(fileName);.I suppose it is wrong, it should be some different pdf file probably.
I'm not sure if I understand what are you trying to do? How to print image file using QPrinter? Into pdf file? Why are trying to use QWebView?
You can use QImage to load image file and then paint with QPainter on QPrinter.
#include <QtGui>
#include <QtCore>
int main(int argc, char** argv) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getOpenFileName(0,"Open File",QString(),"PNG File(*.png)");
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer,0);
if(dlg->exec() == QDialog::Accepted) {
QImage img(fileName);
QPainter painter(&printer);
painter.drawImage(QPoint(0,0),img);
painter.end();
}
delete dlg;
QTimer::singleShot(1, &app, SLOT(quit()));
app.exec();
return 0;
}
Some of your issues may overlap with your other question
https://stackoverflow.com/questions/8297239/how-to-print-pdf-file-in-qt
You try to print the QWebView immediately after you call its load() function. But the QWebView has not yet loaded the content and the view is therefore blank. You need to connect the QWebView's loadFinished signal to some slot where you can call the print() function. Read the QWebView's documentation.
I try to design a gui for an augmented reality application using the kinect. The idea is, to use the hands detected by the kinect skeleton tracking to control an application via gestures.
This question is not about gestures, as this part of my appilcation works fine.
In my application i want to simulate a mouse click whenever a click gesture is performed. To do this, i am sending two events one, mousebuttonpressed, and one mousebuttonreleased, as a normal click is also a series of press and release.
The whole thing works fine on a QWebView. In the browser window, i can "click" on links.
But for some reason i cannot "click" on a QPushButton. The clicked() signal is not emitted.
I have a short example to illustrate my problem:
First the main function:
int main(int argc, char **argv){
QApplication app( argc, argv );
QWebViewButtons mainapp( 0, Qt::Window );
app.setActiveWindow( &mainapp );
mainapp.show();
mainapp.setApplication( &app ); //this is a setter to also get access to the main application in the widget
return app.exec();
}
This is my own widget:
QWebViewButtons::QWebViewButtons(QWidget* parent, Qt::WindowFlags flags ): QWidget(parent, flags ){
this->m_ui.setupUi( this );
QObject::connect( this->m_ui.pushButton, SIGNAL(clicked(bool)), this, SLOT( buttonClicked(bool) ) );
}
void QWebViewButtons::mousePressEvent( QMouseEvent* event ){
printf("mouse click, %d, %d\n", event->pos().x(), event->pos().y() );
}
void QWebViewButtons::buttonClicked( bool clicked ){
printf("slot button clicked\n");
}
void QWebViewButtons::keyPressEvent( QKeyEvent* event ){
printf("Key pressed\n");
QPoint pos( this->width()/2, this->height()/2 );
QPoint global = this->mapToGlobal(pos);
QWidget *w = this->m_app->widgetAt(global);
//printf("widget under point of click: %s", w);
QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonPress, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
QApplication::sendEvent( w, new QMouseEvent( QEvent::MouseButtonRelease, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier ) );
}
I followed the suggestion here:
Qt Artificial mouse clicks doesnt work properly
and send to send my mouse events directly to the QPushButton. But that didn't help.
I also tried to send the mouse events to "this", or the main app.
Now I am running out of ideas.
What I want to have is, that the buttonClicked() slot is called if i press any key. But i only is called if i click the button with my mouse.
How can i accomplish this? Or is my basic idea completely false?
Thanks for your ideas.
So, do I get that right? When doing your "click-gesture" you come into keyPressEvent? If so, you can check whether the "click" has been done above the button and explicitly call the button's clicked() signal. But that's not what you want?
And what exactly is QWebViewButtons? The area the user does his gestures in?
Have you debugged into the keyPressEvent to see if your sendEvent has the correct widget (w)? I can't see why the widget should not recieve the event...
And remember that when new'ing a QMouseEvent and sending it via sendEvent is is never deleted. When using sendEvent you should create your event on the stack.
Maybe you should have a look at this thread, where a QTestEventList is recommended: Mimicking/faking a Mouse Click and Mouse Wheel using Qt. But I can imagine that you don't want test functions do the trick ;)
Ok, the trick was to really send the click to the EXACT widget at the desired click position. In my application i had some trouble because of semi transparent widgets lying over each other.
So the widgetAt(global) command wouldnt help me.
But you can use childAt(global) instead. Of course you need to know from which widget you want to find the child.
That did the trick for me. :-)
I have a listview filled with items. By default, the 0th item will be selected.
If I try to navigate the list using the mobile keypad, it's not gaining focus - instead I need to use my mobile select key for focus. In this process my mobile left soft key gets changed to “Done”. Why is the "Done" menu appearing?
How do I provide default focus to the listview? And how do I avoid the display of “Done” at left soft key?
Here is my code:
#include "Test_Doco.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListView *listui = new QListView();
listui->setSelectionMode(QAbstractItemView::SingleSelection);
listui->viewport()->setFocusPolicy(Qt::WheelFocus);
listui->viewport()->setFocus();
QStandardItemModel* listModel = new QStandardItemModel();
for(int i =0; i<10;i++)
{
QStandardItem *item1 = new QStandardItem("AOL");
listModel->appendRow(item1);
}
QModelIndex index = listModel->index(0,0);
listui->setCurrentIndex(index);
listui->setModel(listModel);
listui->showMaximized();
return a.exec();
}
Edit: I have updated the code. Please check it.
For the default focus, stop calling listui->viewport()->setFocus(); and call listui->setFocus() to give it focus when it is created.
As for the display of "Done", I'm not too sure, but you might need to post more code to show the dialog you are creating. Most have a set of default buttons or a button set to default. The "Done" key might be related to that. As seen here "Exit" is the softkey shown.
The issue is w.r.t Qt 4.6.2 and the issue is fixed in Qt 4.6.3