QLabel直接加载url设置图标
#include <QApplication> #include <QLabel> #include <QPixmap> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QUrl> class ImageLabel : public QLabel { Q_OBJECT public: ImageLabel(const QUrl& url, QWidget *parent = nullptr) : QLabel(parent) { QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, &ImageLabel::onImageDownloaded); manager->get(QNetworkRequest(url)); } private slots: void onImageDownloaded(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { QPixmap pixmap; pixmap.loadFromData(reply->readAll()); setPixmap(pixmap); } reply->deleteLater(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QUrl imageUrl("https://example.com/image.png"); ImageLabel label(imageUrl); label.show(); return app.exec(); } #include "main.moc"