ui.Display_3->setPixmap(*iserialPic);中setPixmap
时间: 2024-03-29 21:35:36 浏览: 14
`setPixmap(const QPixmap &)` 是 `QLabel` 类的一个函数,用于在标签中显示一个 `QPixmap` 对象。`QPixmap` 是 Qt 框架中的一个类,用于处理图片。在你的示例中,`ui.Display_3` 是一个 `QLabel` 对象,`setPixmap(*iserialPic)` 表示将一个 `QPixmap` 对象指针 `iserialPic` 指向的图片设置为 `ui.Display_3` 标签中的显示内容。这样就可以在标签中显示图片了。
相关问题
QT改错,#include "widget.h" #include "ui_widget.h" #include <QTime> #include <QTimer> #include <QPixmap> #include <iostream> #include <string> #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); this->count = 0; this->tm = new QTimer(this); this->tm->start(1000); this->ui->label_2->setText(QTime::currentTime().toString("yyyy-MM-dd hh:mm:ss")); QTime time(0,0,0,0); this->ui->timeNumber->display(time.toString("hh:mm:ss")); //初始化label QPixmap pic(":/images/1.ipg"); this->ui->label->setPixmap(pic); for (int i = 1; i <= 9; i ++){ QString file_path = ":/images/"; file_path = QString::number(i) + ".jpg"; ph_set.push_back(file_path); qDebug() << file_path << endl; } connect(this->tm,&QTimer::timeout,this,&Widget::update_tm); } Widget::~Widget() { delete ui; } void Widget::update_tm() { this->count ++; QTime time(0,0,0,0); this->ui->timeNumber->display(time.addSecs(count).toString("mm:ss")); this->ui->label_2->setText(QTime::currentTime().toString("hh:mm:ss")); } void Widget::on_beforeButton_clicked() { } void Widget::on_afterButton_clicked() { }
代码中的图片路径有误,应该将第13行的 "ipg" 改为 "jpg"。正确的代码如下:
```
#include "widget.h"
#include "ui_widget.h"
#include <QTime>
#include <QTimer>
#include <QPixmap>
#include <iostream>
#include <string>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->count = 0;
this->tm = new QTimer(this);
this->tm->start(1000);
this->ui->label_2->setText(QTime::currentTime().toString("yyyy-MM-dd hh:mm:ss"));
QTime time(0,0,0,0);
this->ui->timeNumber->display(time.toString("hh:mm:ss"));
//初始化label
QPixmap pic(":/images/1.jpg");
this->ui->label->setPixmap(pic);
for (int i = 1; i <= 9; i ++){
QString file_path = ":/images/";
file_path = QString::number(i) + ".jpg";
ph_set.push_back(file_path);
qDebug() << file_path << endl;
}
connect(this->tm,&QTimer::timeout,this,&Widget::update_tm);
}
Widget::~Widget()
{
delete ui;
}
void Widget::update_tm()
{
this->count ++;
QTime time(0,0,0,0);
this->ui->timeNumber->display(time.addSecs(count).toString("mm:ss"));
this->ui->label_2->setText(QTime::currentTime().toString("hh:mm:ss"));
}
void Widget::on_beforeButton_clicked()
{
}
void Widget::on_afterButton_clicked()
{
}
```
void MainWindow::noAgreeWithData(){ int64 currentime=QDateTime::currentMSecsSinceEpoch(); //&& !this->ui->nopass->isChecked() if(ui->tableWidget->rowCount()>0 && currentime-firstTriggerTime<25000){ QLabel *qlable=new QLabel(ui->frame); QPixmap pixmap(":/image/nopass.png"); pixmap.setDevicePixelRatio(3); // 设置缩放比例,如果需要的话 QPainter painter(&pixmap); painter.setOpacity(0.7); // 设置透明度为50% painter.drawPixmap(0, 0, pixmap); painter.end(); qlable->setPixmap(pixmap); qlable->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); qDebug()<<"ui->frame->size()"<<ui->frame->size(); QSize size(550,550); //qlable->setMargin(200); qlable->setGeometry(600, 100, 550, 550); qlable->setFixedSize(size); qlable->setAttribute(Qt::WA_DeleteOnClose); qlable->show(); this->ui->nopass->setChecked(true); sec =25; verifyGoods->stop(); ui->goodsLabel->hide(); ui->goodsNumber->display(sec); ui->goodsNumber->hide(); QString msg=settings->value("voiceprompt/the_actual_items_carried_do_not_match_the_exit_slip_msg","实际携带物品与出门条不相符").toString(); QVariantMap map; instantGateToMap(map); //非法闯入 this->trespassing(map,7,PlayToSpeech::THE_ACTUAL_ITEMS_CARRIED_DO_NOT_MATCH_THE_EXIT_SLIP); this->unlawfuldialog(msg); } }如何在noAgreeWithData结束释放内存*qlable?
在`noAgreeWithData()`函数结束后释放`qlable`的内存,你可以使用`delete`操作符来释放它。同时,你还可以将`qlable`设置为父对象,这样当父对象销毁时,它也会自动销毁。
以下是修改后的代码:
```cpp
void MainWindow::noAgreeWithData() {
int64 currentime = QDateTime::currentMSecsSinceEpoch();
//&& !this->ui->nopass->isChecked()
if (ui->tableWidget->rowCount() > 0 && currentime - firstTriggerTime < 25000) {
QLabel* qlable = new QLabel(ui->frame);
// ... 其他代码 ...
qlable->setPixmap(pixmap);
qlable->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
qDebug() << "ui->frame->size()" << ui->frame->size();
QSize size(550, 550);
qlable->setGeometry(600, 100, 550, 550);
qlable->setFixedSize(size);
qlable->setAttribute(Qt::WA_DeleteOnClose);
qlable->show();
// 设置qlable的父对象为ui->frame
qlable->setParent(ui->frame);
// ... 其他代码 ...
}
}
```
通过将`ui->frame`作为`qlable`的父对象,当`ui->frame`销毁时,它会自动删除其所有子对象,包括`qlable`。
请确保在适当的时机删除指针以避免内存泄漏。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)