我花了一整天时间找到一个Qt代码来从某个网站(例如www.google.com)获取源代码,但是我发现的一切都没有用。那么,请有人发布代码如何从网站获取源代码? (我正在使用Qt创建者,我对插槽连接一无所知,所以如果可能的话,请编写没有插槽连接的代码(或者不管是什么)。
编辑:这是代码:
project.pro
#-------------------------------------------------
#
# Project created by QtCreator 2011-01-02T21:40:52
#
#-------------------------------------------------
QT += core gui
QT += webkit network
TARGET = facebook
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
的main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->buttonBack->setText(""); /*******************************/
ui->buttonForward->setText(""); /* Clear text from */
ui->buttonReload->setText(""); /* the buttons */
ui->buttonStop->setText(""); /*******************************/
ui->buttonBack->setIcon( QApplication::style()->standardIcon(QStyle::SP_ArrowBack)); /*******************************/
ui->buttonForward->setIcon( QApplication::style()->standardIcon(QStyle::SP_ArrowForward)); /* Set an icons */
ui->buttonReload->setIcon( QApplication::style()->standardIcon(QStyle::SP_BrowserReload)); /* on the buttons */
ui->buttonStop->setIcon( QApplication::style()->standardIcon(QStyle::SP_BrowserStop)); /*******************************/
//QString htmlOfPage;
/******** T E S T ********/
//ui->browser->setHtml("<html><body>Test</body></html>");
/******** T E S T ********/
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_buttonBack_clicked()
{
ui->browser->back();
}
void MainWindow::on_buttonForward_clicked()
{
ui->browser->forward();
}
void MainWindow::on_buttonReload_clicked()
{
ui->browser->reload();
}
void MainWindow::on_buttonStop_clicked()
{
ui->browser->stop();
}
void MainWindow::on_browser_loadFinished(bool )
{
//QWebView webview;
//webview.setUrl (QUrl("http://www.google.com"));
//QString s = webview.page()->mainFrame()->toHtml();
//htmlOfPage = ui->browser.page()->mainFrame()->toHtml();
}
我有一个文件mainwindow.h和mainwindow.ui,但我认为没有人需要这些文件,所以我没有发布它们。
答案 0 :(得分:2)
#include <QApplication>
#include <QDebug>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QIODevice>
#include <QUrl>
class MyClass : public QObject{
Q_OBJECT
public slots:
void onFinished() {
QIODevice * content = static_cast<QIODevice*>(QObject::sender());
qDebug() << content->readAll();
content->deleteLater();
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QNetworkAccessManager nam;
MyClass obj;
QNetworkReply * reply = nam.get(QNetworkRequest(QUrl("http://google.com")));
QObject::connect(reply, SIGNAL(finished()), &obj, SLOT(onFinished()));
app.exec();
}
#include "main.moc"