qt QLabel/QPushButton...显示图片的方式

别管...我自己记录着玩儿的都是cv ai产物

1.使用 QImage:
QImage image(picPath);
ui->label_pic->setPixmap(QPixmap::fromImage(image));
2.使用 QIcon:
QIcon icon(picPath);
ui->label_pic->setPixmap(icon.pixmap(ui->label_pic->size()));
3.使用资源文件:
ui->label_pic->setPixmap(QPixmap(":/images/image.png")); // 图片位于资源文件中的 images 目录下的 image.png
4.直接设置背景:
ui->label_pic->setStyleSheet("background-image: url(" + picPath + ");");
5.区别和特点:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用QThread指针实现的Qt界面,可以读取文件并将其显示在两个QLabel中,同时可以点击开始和暂停按钮来控制图片的更新: ```c++ #include <QMainWindow> #include <QLabel> #include <QThread> #include <QTimer> #include <QVBoxLayout> #include <QPushButton> #include <QFileDialog> class ImageThread : public QThread { Q_OBJECT public: ImageThread(QLabel* label, QString fileName, QObject* parent = nullptr) : QThread(parent), m_label(label), m_fileName(fileName) {} void run() override { QImage image(m_fileName); while (!isInterruptionRequested()) { m_label->setPixmap(QPixmap::fromImage(image)); msleep(100); } } private: QLabel* m_label; QString m_fileName; }; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget* parent = nullptr) : QMainWindow(parent), m_leftLabel(new QLabel(this)), m_rightLabel(new QLabel(this)), m_thread(nullptr), m_timer(nullptr) { QWidget* centralWidget = new QWidget(this); QVBoxLayout* layout = new QVBoxLayout(centralWidget); QPushButton* openButton = new QPushButton("Open", centralWidget); connect(openButton, &QPushButton::clicked, this, &MainWindow::openFile); QPushButton* startButton = new QPushButton("Start", centralWidget); connect(startButton, &QPushButton::clicked, this, &MainWindow::startThread); QPushButton* pauseButton = new QPushButton("Pause", centralWidget); connect(pauseButton, &QPushButton::clicked, this, &MainWindow::pauseThread); layout->addWidget(openButton); layout->addWidget(m_leftLabel); layout->addWidget(m_rightLabel); layout->addWidget(startButton); layout->addWidget(pauseButton); setCentralWidget(centralWidget); } private slots: void openFile() { QString fileName = QFileDialog::getOpenFileName(this, "Open Image", "", "Image Files (*.png *.jpg *.bmp)"); if (!fileName.isEmpty()) { m_leftLabel->setPixmap(QPixmap(fileName)); m_rightLabel->setPixmap(QPixmap(fileName)); m_fileName = fileName; } } void startThread() { if (m_thread == nullptr) { m_thread = new ImageThread(m_rightLabel, m_fileName, this); m_thread->start(); } else { m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, m_thread, &QThread::quit); connect(m_thread, &QThread::finished, m_timer, &QTimer::deleteLater); m_timer->start(100); m_thread = nullptr; } } void pauseThread() { if (m_thread != nullptr) { m_thread->requestInterruption(); m_thread->wait(); delete m_thread; m_thread = nullptr; } } private: QLabel* m_leftLabel; QLabel* m_rightLabel; ImageThread* m_thread; QTimer* m_timer; QString m_fileName; }; ``` 这个例子中,我们创建了一个ImageThread类来执行图片更新的任务。该类在run()函数中加载图片并不断更新QLabel的内容。我们还有一个MainWindow类来实现界面。在这个类中,我们创建了一个openFile()槽函数来打开文件对话框并将选定的文件显示QLabel中。我们还创建了一个startThread()槽函数来启动和暂停线程。如果线程未运行,则该函数创建一个新线程并开始执行任务。如果线程正在运行,则该函数停止线程并删除该线程对象。最后,我们还有一个pauseThread()槽函数来暂停线程。如果线程正在运行,则该函数请求中断并等待该线程完成。然后,它删除该线程对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值