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?
时间: 2024-04-15 11:30:11 浏览: 113
Brackets-MainWindow:永远不会忘记主窗口!
在`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`。
请确保在适当的时机删除指针以避免内存泄漏。
阅读全文