在Qt开发时,如果不知道怎么实现功能,可以先看看相关类的代码,例如,要实现QLabel显示图片,如果没做过肯定不知道怎么写,看看QLabel的代码:
public Q_SLOTS:
void setText(const QString &); //设置文本
void setPixmap(const QPixmap &); //设置pixmap
#ifndef QT_NO_PICTURE
void setPicture(const QPicture &);
#endif
#if QT_CONFIG(movie)
void setMovie(QMovie *movie);
#endif
void setNum(int); //设置整数
void setNum(double);
void clear();
查看Qt文档可知,用setPixmap可以显示图片,解释如下:
This property holds the label's pixmap,If no pixmap has been set this will return nullptr. Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.
写段代码试试看
QString path = QString("D:\\TestFiles\\123.jpg");
QImage img;
img.load(path);
QPixmap px