Why nothing is display when I use QWebpage - qt

it's very stupid question but I don't understand why nothing is display in my qt console application when I use QWebpage :
This is my basic code :
#include <QCoreApplication>
#include <QtWebKitWidgets>
#include <QDebug>
#include <stdio.h>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/*QWebPage page;
page.settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
page.settings()->setAttribute(QWebSettings::AutoLoadImages, true);
page.settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
page.settings()->setAttribute(QWebSettings::PrintElementBackgrounds, true);
page.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
page.mainFrame()->load(QUrl("http://myflowerpower.parrot.com/#plantdb/3"));
qDebug () << "source html : \n";
qDebug() << page.currentFrame()->toHtml();
QString htmlResult = page.currentFrame()->toHtml();*/
printf("test");
std::cout << "test std::cout\n";
qDebug() << "Debug Message";
qWarning() << "Warning Message";
qCritical() << "Critical Error Message";
bool result = a.exec();
return result;
}
If I uncomment the code, nothing is display, but if I comment QWebpage stuff it's work without problem. Have any idea ?

QCoreApplication can't handle GUI elements.
You need to use QApplication instead to support the GUI and the event framework.
And make sure that you have what is needed in your .pro file.
QT += widgets webkitwidgets
Hope that helps.

Related

Qt: Handle 'Save Print Output As' dialog with QPainter::begin()

here is a minimal reproducable example of my problem:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPrinter printer;
QPainter painter;
// this will open a file dialog to save a pdf file
if (!painter.begin(&printer)) {
qDebug() << "how to get the difference between 'Dialog Canceled' and 'File in use' here?";
}
else {
//do the painting stuf...
}
return a.exec();
}
My question is - as already mentioned in the code - how do I know if the dialog is canceled by the user or the pdf file is just in use (for example opened in Adobe Reader)?
I already tried to check the printer.outputFormat() and the printer.docName() but they are both empty.

How to show every qDebug with new window?

I make a QT console program.
I want to show every command by new window.
How could I do that ??
main.cpp
I use system is win10 with QT.
#include <QCoreApplication>
#include <QDebug> //在文字視窗輸出文字功能函式
#include <QDir> //搜尋資料夾功能函式
#include <QFileInfo> //顯示檔案資訊
#include <QString> //字串函式
#include <QStringList> //字串陣列函式
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir; //設定資料夾位置
qDebug() << mDir.exists(); //輸出確定是否有mDir資料夾
QString command = "frtomem -p WnaYooAQ ";
foreach(QFileInfo mitm, mDir.entryInfoList(Qfilter)) //列出所有資料夾中篩選的檔案內容
{
qDebug() << command << mitm.fileName();
}
return a.exec();
}
I just can show it by one console window.
can't separete it.

QRemoteObjectNode::remoteObjectAdded signal does not fire

While learning how to use QtRO tech preview module, I tried a simple 3 node network ( A registry node, a source object remoting node, and a client node).
registry node main.cpp:
#include <QCoreApplication>
#include <QRemoteObjectRegistryHost>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRemoteObjectRegistryHost host(QUrl("tcp://127.0.0.1:5557"));
return a.exec();
}
source node main.cpp:
#include <QCoreApplication>
#include <QRemoteObjectHost>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer timer;
timer.start(10000);
QRemoteObjectHost host;
if(!host.setHostUrl(QUrl("tcp://127.0.0.1:5556"))) qDebug() << "Host url " << host.lastError();
if(!host.setRegistryUrl(QUrl("tcp://127.0.0.1:5557"))) qDebug() << "Reg url " << host.lastError();
if(!host.enableRemoting(&timer, "HostTimer")) qDebug() << "Remoting error " << host.lastError();
return a.exec();
}
client node main.cpp:
#include <QCoreApplication>
#include <QRemoteObjectNode>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRemoteObjectNode node(QUrl("tcp://127.0.0.1:5557"));
QObject::connect(&node, &QRemoteObjectNode::remoteObjectAdded,
[](const QRemoteObjectSourceLocation& info){
qDebug() << "New source added : " << info;
});
qDebug() << "Waiting for registry ";
node.waitForRegistry(10000);
qDebug() << "Already here sources : " << node.registry()->sourceLocations();
QTimer timer;
timer.start(5000);
QObject::connect(&timer, &QTimer::timeout,
[&](){
qDebug() << "New sources list : " << node.registry()->sourceLocations();
});
return a.exec();
}
I begin by starting the registry node, then the client node and finally the source node, with this launch sequence I expect to get the remoteObjectAdded fired up, but It didn't.
Your help will be highly appreciated.
I answer my question.
I used the wrong signal source in the connect method call.
the client node will be as :
#include <QCoreApplication>
#include <QRemoteObjectNode>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRemoteObjectNode node(QUrl("tcp://127.0.0.1:5557"));
QObject::connect(node.registry(), &QRemoteObjectRegistry::remoteObjectAdded,
[](const QRemoteObjectSourceLocation& info){
qDebug() << "New source added : " << info;
});
qDebug() << "Waiting for registry ";
node.waitForRegistry(10000);
qDebug() << "Already here sources : " << node.registry()->sourceLocations();
QTimer timer;
timer.start(5000);
QObject::connect(&timer, &QTimer::timeout,
[&](){
qDebug() << "New sources list : " << node.registry()->sourceLocations();
});
return a.exec();
}
It is the registry who fire the signal not the node, It sound logic after all the source nodes are connected to the registry, so it fire the remoteObjectAdded signal.

Qt QNetworkAccessManager & simple web service return

I have a simple web service with the following URL:
http://localhost:8080/WebSvc1/webresources/generic/data?ctype=Ping
This returns a simple XML data:
<CALL TYPE='Ping'><IP>10.0.0.10</IP></CALL>
I'm trying to write a Qt program to call this web service.
The code that makes the call is below:
QUrl qrl("http://localhost:8080/WebSvc1/webresources/generic/data?ctype=Ping");
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
printf ("Calling url: [%s]\n", qPrintable(url));
QNetworkReply *reply = 0;
reply = manager->get(QNetworkRequest(qrl));
qDebug() << reply->readAll();
I'm expecting/hoping the readAll will get the XML text data and print it (via qDebug).
Instead I see nothing and the program just hangs.
UPpdate, also have this:
void obj::replyFinished(QNetworkReply *reply)
{
qDebug() << reply->readAll();
}
I've included an example (forcing a synchronous request <-> reply exchange to easy the debugging process) that should work for you:
QUrl qrl("http://localhost:8080/WebSvc1/webresources/generic/data?ctype=Ping");
qDebug() << "Calling url: " << qrl.toString();
manager = new QNetworkAccessManager();
QNetworkReply* reply = manager->get(QNetworkRequest(qrl));
QEventLoop eventLoop;
connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Network error: " << reply->error();
}
else
{
qDebug() << reply->readAll();
}
Notice that the "emitter" of the finished signal is not the QNetworkAccessManager but the reply itself.
I think your error could be with your web service. I tried your code out (slightly modified) with httpbin.org, and was getting proper replies. Maybe take a look at your code with httpbin.org and then see if you can track down what's wrong with your service.
MainWindow.cpp
#include "MainWindow.hpp"
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QDebug>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), manager(this) {
load();
}
void MainWindow::load() {
const QUrl url(QStringLiteral("http://httpbin.org/xml"));
QNetworkReply* reply = manager.get(QNetworkRequest(url));
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
replyFinished(reply);
});
qDebug() << reply->readAll(); // The reply won't be ready by now, so
// testing here isn't very helpful.
}
void MainWindow::replyFinished(QNetworkReply* reply) {
qDebug() << reply->readAll();
reply->deleteLater();
}
MainWindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
public slots:
void load();
void replyFinished(QNetworkReply* reply);
private:
QNetworkAccessManager manager;
};
#endif // MAINWINDOW_HPP
main.cpp
#include "MainWindow.hpp"
#include <QApplication>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
network.pro
QT += core gui widgets network
TARGET = network
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
SOURCES += \
main.cpp \
MainWindow.cpp
HEADERS += MainWindow.hpp

Qt is successfully creating a file but not writing to it with QTextStream

Hey I'm trying to mess around with Qt and for some reason the following code will create the desired text file, but never writes anything to it. Am I doing something wrong? I believe I've copied the example in the documentation pretty accurately.
qDebug() << output
works as expected, but even though the file is created, nothing is ever written to it.
#include <QCoreApplication>
#include <QtDebug>
#include <QString>
#include <QDateTime>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filename = "";
filename.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
filename.append(".txt");
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
QString output = "TEST";
qDebug() << output;
out << output;
return a.exec();
}
The data does not get written to disk immediately: It sits in a buffer until it's flushed.
Close the file after you've finished writing.
(In my experience, the file is closed anyway when you quit the program, but it's good practice to do this explicitly)

Resources