5

如何从 PYQT 中的 url 设置 Qicon,你能给我举个例子吗?

4

2 回答 2

8

一个基本的例子是:

from PyQt4.QtGui import *
from PyQt4.QtCore import QUrl
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

app = QApplication([])
url = "http://www.google.com/favicon.ico"
lbl = QLabel("loading...")
nam = QNetworkAccessManager()

def finishRequest(reply):
    img = QImage()
    img.loadFromData(reply.readAll())
    lbl.setPixmap(QPixmap(img))

nam.finished.connect(finishRequest)
nam.get(QNetworkRequest(QUrl(url)))
lbl.show()
app.exec_()
于 2012-05-24T13:50:21.670 回答
0

使用requests.get方法下载图像并QIcon从中创建一个。

import sys
import requests

import PySide6
from PySide6.QtWidgets import QTableView, QWidget, QApplication, QGridLayout, QHeaderView
from PySide6.QtCore import Qt, QAbstractTableModel
from PySide6.QtGui import QColor, QIcon, QPixmap

from datetime import datetime

class MagicIcon():
    def __init__(self, link):
        self.link = link
        self.icon = QIcon()
        try:
            response = requests.get(self.link)
            pixmap = QPixmap()
            pixmap.loadFromData(response.content)
            self.icon = QIcon(pixmap)
        except:
            pass

class MainWindow(QWidget):
    def __init__():
        super().__init__()
        self.setWindowIcon(MagicIcon(
            "https://img.icons8.com/external-flatarticons-blue-flatarticons/65/000000/external-analysis-digital-marketing-flatarticons-blue-flatarticons-1.png"
        ).icon)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = MainWindow()
    wid.show()
    sys.exit(app.exec())
于 2022-02-20T04:16:30.587 回答