PDA

View Full Version : Networking classes need QWidget?



RolandHughes
29th August 2012, 01:18
Can someone please explain why on God's green earth the network classes __need__ GUI QWidget resources?

This makes them completely unusable from a console app.

Has someone filed a bug?

ChrisW67
29th August 2012, 04:01
Can someone please explain why on God's green earth the network classes __need__ GUI QWidget resources?
What makes you think they do? QtNetwork4 is not dependent on QtGui4 on my machine.

This makes them completely unusable from a console app.
Really? This program compiles and runs fine without Gui:


#include <QtCore>
#include <QtNetwork>
#include <QDebug>

class Fetcher: public QObject {
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p), reply(0) {}

void fetch(const QUrl &url) {
reply = nam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
}

public slots:
void slotFinished() {
qDebug() << Q_FUNC_INFO << "Error" << reply->error();
QByteArray ba = reply->readAll();
qDebug() << ba;
reply->deleteLater();

emit finished();
}
signals:
void finished();
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
};

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

Fetcher f;
f.fetch(QUrl("http://www.qtcentre.org/robots.txt"));
QObject::connect(&f, SIGNAL(finished()), qApp, SLOT(quit()));

return app.exec();
}
#include "main.moc"

It has "QT -= gui" in the PRO file and is consequently not linked to QtGui4.

Has someone filed a bug?
Plenty of people have filed bugs. This is not a bug though.

RolandHughes
30th August 2012, 18:17
Try adding QWebPage and QWebElementCollection. Even though you turn everything off and have it set to display absolutely nothing, it still needs the GUI. Why would anyone do this? To identify/verify site content with a crawler on a server.

ChrisW67
31st August 2012, 04:18
QtWebkit is a Qt-esque wrapper around the Webkit browser (not crawler) library. If you use Webkit or QtWebkit, which has a browser widget whether you use it or not, then you need the rendering library (QtGUI in this case). QWebPage has a bunch of GUI rendering functionality that you cannot avoid. If you don't like it, don't use QtWebkit or do the work to separate non-GUI from GUI components of WebKit and contribute it to the community. Depending on your exact requirements there are other ways to go about parsing the muck called HTML.


This makes them completely unusable from a console app.
Only partly true. You have to use QApplication but you do not need to display a GUI. On Linux X11 libraries must be present, and you can fake a running X server with Xvfb. For example, a headless QtWebKit program that extracts image links from www.google.com:


$ unset DISPLAY
$ ./grab
grab: cannot connect to X server
$ Xvfb 2>/dev/null >&2 :10 &
$ DISPLAY=:10 ./grab
Image at: "/images/icons/product/chrome-48.png"
Image at: "/logos/2012/montessori-hp.jpg"