QWebView always fails to load website - c++

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.

Related

A simple communicate c/s app using QtUdpSocket

I am a beginner of Qt.I want to use QUdpSocket to write a server app without a GUI.I have writen the client GUI app and server GUI app.They work well.But the server without GUI doesn't work well.
I try to use a while(true) loop to make the app running ,because I think after the code 'return a.exec()' the app will stop.But my server app does't work.
Here is my code:
client with GUI:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
socket=new QUdpSocket(this);
//click pushbutton send message to server
connect(this->ui->pushButton,SIGNAL(clicked()),this,SLOT(send()));
}
Widget::~Widget()
{
delete ui;
}
//send message
void Widget::send()
{
QByteArray msg="client2:hello world";
socket->writeDatagram(msg.data(),msg.size(),QHostAddress::LocalHost,6666);
}
The client app and server with GUI app can work well.But When I try to make a server app without GUI,I found server can not get the message from client.
And here is my server.cpp without GUI:
#include "server.h"
Server::Server(QObject *parent) : QObject(parent)
{
socket=new QUdpSocket(this);
array=new QByteArray();
socket->bind(QHostAddress::LocalHost,6666,QAbstractSocket::DontShareAddress);
socket->open(QIODevice::ReadWrite); //without this line, the app will show:
//'QIOBevice::read(QUdpSocket):device not open',but it still can get the message from client.
connect(socket,SIGNAL(readyRead()),this,SLOT(printMsg()));
}
void Server::printMsg()
{
if(socket->hasPendingDatagrams()) //I forgot this line before.
{
array->resize(socket->pendingDatagramSize());
socket->readDatagram(array->data(),array->size());
socket->readAll();
qDebug()<<"yesyesyes";
}
}
I try many times,I think the problem is in my main.cpp:
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server s;
return a.exec();//after a.exec(),the app is still working!!!
}
In a GUI app, if I don't close the windows, the server app will always run, and the SIGNAL and SLOT I can understand.But in a console app, it seems that the app end fast and can't get the message from client(can't display the message).
You should not have a while(true) in your main, otherwise, you are effectively just freezing your thread and Qt application. This is exactly what app.exec(); is for, it will return when your application is closed (more details here)
If your application (with or without GUI) closes after calling app.exec(), it is due to one of the following:
You are closing it manually, e.g. with qApp->quit();
You are forcing an exit, e.g. with exit(1);
A fatal error or exception prematurely ends your application

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
}
}

How do i get QGeoPositionInfoSource to emit a signal in Qt 5.5.1 with iOS?

I would appreciate if anybody can help me with this (did not find anything relevant with Google).
I am trying to get positioning info in iOS using Qt. Have tried the simulator as well as a real iPhone. I get at source from QGeoPositionInfoSource that says "corelocation", but source does not seem to emit a signal since positionUpdated never gets called. Any ideas?
Here is my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
if (source){
connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),this, SLOT(positionUpdated(QGeoPositionInfo)));
source->setUpdateInterval(100);
source->startUpdates();
qDebug()<<"Source found";
qDebug()<<source->availableSources();
}else{
qDebug()<<"Failed source";
}
}
void MainWindow::positionUpdated(const QGeoPositionInfo &info)
{
qDebug() << "Position updated:" << info;
}
UPDATE-----------------------------
Getting the error from source says "An unidentified error occurred.". Not very helpful but at least its not an issue related to lacking privileges (error 0).
This post solved my problem. You have to edit the info.plist file (from xcode or with a text editor).
Location Services not working in iOS 8

Qt Creator no member named stackedWidget

I've just designed my ui in the QT-Creator and, as the main application is based on two panels, I decided to use the StackedWidget for "change the layout" without opening a new window.
So, I added a QTStackedWidget named: stackedWidget (as default).
The problem is in mainwindow.cpp, I added a custom SLOT that contain:
ui->stackedWidget->setCurrentIndex(1);
when I build this the compiler says:
mainwindow.cpp:25: error: no member named 'stackedWidget' in 'Ui::MainWindow'
ui->stackedWidget->setCurrentIndex(1);
~~ ^
also in the qt-creator itself I was unable to attach a signal to the stackedWidget because it doesn't show to me the setCurrentIndex SLOT...
any advice?
Please note that I'm a noob with C++ I just used Qt a couple of years ago with PyQt4.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void showOtherPage();
void showMainPage();
};
#endif // MAINWINDOW_H
mainiwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "MainWindow - Debug Mode ON";
connect(ui->btnOther, SIGNAL(clicked()), SLOT(showOtherPage()));
}
void MainWindow::showOtherPage()
{
qDebug() << "Showing Other Page";
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::showMainPage()
{
qDebug() << "Showing Main Page";
ui->stackedWidget->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
I had a very similar issue.
One of the UI actions was defined as the others and used.
I had a ui has no member named xyz compilation error for this one only, without possible explanation.
I could solve it by unchecking/checking the Shadow build option in the project compilation options!
Hope it saves someone the 30 minutes I just lost :)
Faced the very same issue today. Recreating the project does work, though I've found a solution that won't make one rewrite everything :) Run it in Debug. Once you've done it, problem's solved.
Creating a new project fixed it... I don't know why, I'm still looking for an explanation.
I started a new project, created the stackedWidget, and tested the code for the page-switching... it just simply works... and I still do not know WHY the other one fail to build...
Checked again and again names and everything.
I tried cleaning project and running qmake without any luck. I then drilled down into the project folder and deleted the ui_***.h file and everything started working. Might have had to do with the fact Visual Studio generated that file and I was working on another computer with Qt Creator for the same project.

How to download files from QWebView?

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 !