How to download files from QWebView? - c++

I created a small web browser with QT Creator and QWebView. I's working very good and the pages are loading very fast. But how can I make my browser capable of downloading files? I looked through the signals and functions list, but I did't find something that could help me.
How can I found out if a QUrl contains a link to a file other than text/html so I can download it?

QWebView has a 'QWebPage' member which you can access it's pointer with webView.page() . This is where you should look. QWebPage has two signals: downloadRequested(..) and unsupportedContent(..). I believe dowloadRequest is only emitted when user right clicks a link and selects 'Save Link' and unsupportedContent is emitted when target URL cannot be shown (not an html/text).
But for unsupportedContent to be emitted, you should set forwardUnsupportedContent to True with function webPage.setForwardUnsupportedContent(true). Here is a minimal example I have created:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webView->page()->setForwardUnsupportedContent(true);
connect(ui->webView->page(),SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(download(QNetworkRequest)));
connect(ui->webView->page(),SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::download(const QNetworkRequest &request){
qDebug()<<"Download Requested: "<<request.url();
}
void MainWindow::unsupportedContent(QNetworkReply * reply){
qDebug()<<"Unsupported Content: "<<reply->url();
}
Remember, MainWindow::download(..) and MainWindow::unsupportedContent(..) are SLOTs !

Related

On QTreeView double_Clicked event: how to know which folder was clicked?

I'm still pretty new to C++ and qt creator. I have a TreeView displaying a directory, and upon double-clicking a folder I'd like to be able to get the directory of the folder, so i can do something with the contents. I notice the double_Clicked event passing along a "const QModelIndex& index" and I suppose this holds some information on which folder of the tree was clicked. I can't find any documentation on this signal, and what the "index" passed along could be. Does anyone have an explanation, tutorial, example, documentation, ... for me? I've been searching for awhile and trying things out but I can't find the solution. Or additionally: how can I check what gets passed along? How could I print this, or know what it is?
You can set up your treeView in the constructor like this:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QFileSystemModel(this);
model->setRootPath("");
ui->treeView->setModel(model);
ui->treeView->setRootIndex(model->index("/home/waqar/"));
}
And in the double_Clicked() slot, use the QModelIndex to get the name of the folder you clicked on:
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
//displays the name of the folder you clicked on in the terminal
qDebug () << index.data(Qt::DisplayRole).toString();
//get full path
QString path = model->filePath(index)
}

QtListWidgetItem with Pixmap crashes if to many

I'm a noob, so sorry if my question feels dumb.
I use Qt Creator to make a kind of image viewer.
I added a QListWidget and added items with a pixmap. So far, so good.
Now I try to read the hole directory and add all 438 images.
The app crashes with this message:
Cn::Process::NotifyOutOfMemory(). 17:47:36: The program has
unexpectedly finished. 17:47:36: The process was ended forcefully.
If I reduce the count to 85. The app opens, but does only show 77 images.
I tried to fix this by changing addItem to addItems but don't know how to get the QListWidgetItem in a QList or on any other way. And than it is the question of this is a solution.
Can someone give me a kick in the right direction?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDir dir("C:/");
QStringList items; // String???
foreach(QFileInfo var, dir.entryInfoList ()){
if(var.isFile ()){
//items += // What to do here ??
ui->listWidget->addItem (new QListWidgetItem(QPixmap(var.absoluteFilePath ()), var.fileName ()));
}
ui->listWidget->addItems (items);
}
}
Michael

Resizing web page inside a spliter in qt

My question is simple, but I have been struggling to find the solution. I have the QMainWindow showed in the image, constructed in the QtCreator.
I want to load an html web page in the QWidget csWindow, for that I have placed a Qlabel label_pic where I load my web Page. This is code so far:
MainWindow::MainWindow(QWidget *parent, Project *project) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->project = project;
QWebEngineView *view = new QWebEngineView(ui->label_pic);
view->load(QUrl("http://localhost/myWeb.html"));
////works fine, for an image
//QPixmap pix(":/img/imgs/someImage.png");
//ui->label_pic->setPixmap(pix);
//I also can load the web page in the QWidget csWindow but with the same result
//QWebEngineView *view = new QWebEngineView(ui->csWindow);
//view->load(QUrl("http://localhost/myWebb.html"));
}
The page loads fine but it does not fit into the corresponding space, It is created with a fixed size and never resize. I want the web page to be resized when I move the spliters, but I have not succed doing it.
I have tried several approaches, first just put an image in label_pic, enable the property scaled contents and works fine. Now, I want to do the same with the web page.
Thanks in advance.
The page loads fine but it does not fit into the corresponding space
This is because the size of the QWebEngineView is not know until it completes loading, so what you need is to connect to its signal loadFinished and resize label_pic :
connect(view, &QWebEngineView::loadFinished, [this]() {this->ui->label_pic->resize(this->ui->csWindow->size());});
I want the web page to be resized when I move the splitters
Then also you need to connect to signal QSplitter::splitterMoved from all your splitters and resize both csWindow and label_pic like this:
connect(ui->splitter, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_2, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_3, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
and note that this would work best if you set a layout for your window, either from designer or adding code, for instance:
QGridLayout *layout = new QGridLayout;
layout->addWidget(ui->splitter_3);
this->ui->centralWidget->setLayout(layout);
and remember you should make all connect statements before you load the view.

How to block a Url using c++ qt application

I want to create a QT application with webkit browser with below feature:
By default, google website (http://google.com) is displayed to user and it must allow access to all the url except google hangout.
I created below program which will open the google url but don't know how to block the desired url:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebkitWidgets/QWebView>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setup(this);
ui.webView->load(QUrl("http://google.com"));
}
MainWindow::~MainWindow()
{
delete ui;
}
I´m going to make this a brief answer. Qt provides a number of great examples with the installation, you should look at the Tutorial examples for the Webview one, it´s a QML application -- and it is really easy to modify it to your needs.
As the navigation bar is a separate element, which is separately scripted, you can make it callback to C++ where you can apply a filter to it. The example you should look for is called ´quicknanobrowser´.
Edit
To clarify, based on a comment, you can also act on the signal when a new page is loaded. This would be placed inside BrowserWindow.qml, ctrl+f for "onNewViewRequest", which is another signal that is acted upon in the same manner:
onLoadingChanged: {
if(loadRequest.url == "www.blockedurl.com")
{
// Do what you want here
}
}

QWebView always fails to load website

I am running QtCreator 3.2.0, based on Qt 5.3.1 on Windows 8.1.
It seems that no matter what website i am trying to load, the onLoadFinished slot always returns false. I tried to load websites with and without SSL, both failed.
When I tried to load local resources everything worked well. So i monitored my network with wireshark and my Qt application does not even send a request, i also checked the QUrl with QUrl::isValid(), tried to use QWebView::load() instead of QWebView::setUrl() and ran my application as Administrator. Nothing worked.
I can't find any errors in my code and i somewhat feel like this is a bug, but I'm not certain about that.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->webView->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
this, SLOT(errorcheck(QNetworkReply*)));
QUrl url("http://www.nasa.gov/");
ui->webView->setUrl(url);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_webView_loadFinished(bool arg1)
{
ui->label->setText(arg1 ? "success" : "failure");
}
void MainWindow::errorcheck(QNetworkReply* QNR) {
qDebug() << QNR->errorString();
}
The output is: "Host www.nasa.gov not found"
Try
connect(QWebView->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
this, SLOT(errorcheck(QNetworkReply*)));
void MainWindow::errorcheck(QNetworkReply* QNR) {
qDebug()<<QNR->errorString;
}
And check if any error occure.