提问者:小点点

从base64字符串加载QIcon


是否可以从base64字符串创建QIcon?

我看到我们可以将base64字符串加载到QByteArray中。我看到我们可以从QPixMap创建一个QIcon。

我尝试了以下方法:

const QByteArray data = QByteArray::fromBase64("iVBORw0KGgoAAAANSUhEUgAAAB8AAAAlCAYAAAC6TzLyAAAABHNCSVQICAgIfAhkiAAAABl0RVh0U29mdHdhcmUAZ25vbWUtc2NyZWVuc2hvdO8Dvz4AAALfSURBVFiF5ZY9TFNRFMd/77WFDnx0ogaInRRKApudQE1gcwFhMijOFp2VBOOE7tDFwYjAJmiiToBRu7VbTfjaSqBJnfrB8Gj73nV47Xu2BV6/CAbO8vLuu/f8/uece8+7UiaTFVyQyRcFvlxwLbKGtrtR8Xx7o8Dq+hxqeAmcrTjcvUiubss1jYvc2aY/lTS51WcVLWkY3DY0jXT9FgAivo26Pne+8NL6OiYWoLkFADW8ZFn/muG5Ly/IrT4l93XGHHS2YZ8ImHO+zSASB42Fa9EQ2p4elRb5RG5lCpSU7tDjwzbk1yda1L8muOzx4ZhcNFKsRUNkVx4j4ltA5fWX6mqvSors8hTiz47+7mzFPr6A7PGBkiITGIbjIwDs4/PIPSPFQdQMBnC24ZhcRO4fzYtJk1uZQousnVj/QmkKZpudffmqIpCSIvv2HihppPZOpMK5tjcbEYn9MADa3ndEMobN98gcz2UQsQjywJjhsuK0q6FF1I03xrs8MIZt8ElRJ9Mia+TWXxuplj0+7OPzZD9OQ/IQ+8Q8kruvergWWUMNBhDJWNG4PDCG3D+q1xkQ8S2yy1OGAMntxTbyHNnda3bBauGWIjw+5EG/sdmMjdjcQpN/swxcE9wQsbuBGv5g1Nlw6PZi8z1EvjGMGlxAHhgtSnVD4IaIaAg1uFAuwtWF/cH7M/9u1nAlRfbdfSR3L1KHF9njQ+roKUujSBygBgNovz/rjts7cfg3z3RtCdeiIb19li50dRULKnS0vAj55nBZUyk1y8uEiG+fPJ44RCQOYXcTNZgX5Pbqgq55LcFQEvmvnz+MD7fv3M1DDhDJmC7iOI0WDYGSNlvqKdY0s32qz4JZRi65uvVNkz/HtiHzm4hvIZQjxH4IlBRafAeSh2XH8DSr6w4nufuQwBRW5XpLeGbOW70qzLSfZZfn3l6tWaa9kvTVav9n5P+ez/Oyq7vh6v6l1mNXN+0XCv8Le+BNIvhmHeEAAAAASUVORK5CYII=");

QImage image;
image.loadFromData(data, "PNG");

QPixmap pixmap;
pixmap.fromImage(image);

QIcon icon(pixmap);

QPushButton* button = new QPushButton(this);
button->setIcon(icon);

什么都没显示出来。

PS:这里可以试试base64图像。


共1个答案

匿名用户

但是,不需要qimage:您可以直接从字节数组创建pixmap

QPixmap pixmap;
pixmap.loadFromData(data);