C++ (Qt) #include <iostream> #include <QString>#include <QObject>#include <QCoreApplication> #include "Shower.h"#include "Downloader.h" int main( int argc, char *argv[] ){ QCoreApplication app( argc, argv ); Downloader downloader; Shower shower; QObject::connect( &downloader, SIGNAL( signalWithContent( QString* ) ), &shower, SLOT( show( QString* ) ) ); QString url = "http://qtobserver.freeoda.com/installing_netbeans.php"; downloader.fetch( url ); return app.exec();}
C++ (Qt)#ifndef DOWNLOADER_H#define DOWNLOADER_H #include <memory>#include <string> #include <QObject>#include <QString>#include <QNetworkReply>#include <QNetworkRequest>#include <QNetworkAccessManager> class Downloader : public QObject{ public: Downloader( ) { m_manager = std::unique_ptr<QNetworkAccessManager>( new QNetworkAccessManager( this ) ); } void fetch( const QString &url ) { m_reply.reset( m_manager->get( QNetworkRequest( QUrl( url ) ) ) ); connect( m_reply.get( ), SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); } signals: void signalWithContent( QString *content ); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( &content ); } private: std::unique_ptr<QNetworkReply> m_reply; std::unique_ptr<QNetworkAccessManager> m_manager;}; #endif // DOWNLOADER_H
C++ (Qt)#ifndef SHOWER_H#define SHOWER_H #include <QObject>#include <QString>#include <QTextCodec>#include <QByteArray>#include <QDebug> class Shower : public QObject{ Q_OBJECT public slots: void show( QString *content ) { qDebug() << *content; }}; #endif // SHOWER_H
C++ (Qt)while(!asleep()) sheep++;
C++ (Qt)signals: void signalWithContent( const QString &content );
C++ (Qt)signals: void signalWithContent( QString content );