QSize s2=ui->label->size();替换成不用UI的形式
时间: 2024-02-17 13:01:58 浏览: 30
如果不使用UI,可以使用QWidget类的findChild函数来获取相应的部件指针,然后使用指针访问该部件的size函数来获取其大小。示例代码如下:
```cpp
QWidget *widget = findChild<QWidget*>("label"); // 获取名为"label"的部件指针
QSize s2 = widget->size(); // 获取该部件的大小
```
需要注意的是,findChild函数的模板参数需要与要查找的部件类型相匹配。如果找不到名为"label"的部件,findChild函数会返回一个空指针。
相关问题
void Widget::mouseMoveEvent(QMouseEvent *event) { //点击在按钮btn上,无法获取event->pos() if(this->drag) { //计算label要移动到的新位置 QPoint npos=event->pos()+this->pos;//一直补偿差值!!!!! QSize s1=this->size();//窗口大小 QSize s2=ui->label->size();//label大小 if(npos.x()<0)//不超左边 { npos.setX(0); } else if(npos.x()>s1.width()-s2.width())//不超右边 { npos.setX(s1.width()-s2.width()); } if(npos.y()<0)//不超上边 { npos.setY(0); } else if(npos.y()>s1.height()-s2.height())//不超下边 { npos.setY(s1.height()-s2.height()); } ui->label->move(npos);//移动到新位置 } }
这段代码实现了鼠标拖动窗口中一个标签(label)的功能,并限制了标签的移动范围不超过窗口范围。但是,如果拖动的时候鼠标指针位于按钮(btn)上,无法获取到正确的鼠标位置,导致标签位置计算错误。
解决方法可以通过判断鼠标指针所在的部件,如果不是按钮(btn)则进行标签位置计算和移动,否则不进行操作。示例代码如下:
```cpp
void Widget::mouseMoveEvent(QMouseEvent *event) {
if (this->drag) {
QWidget *child = childAt(event->pos());
if (!child || child == ui->label) { // 如果鼠标指针不在任何部件上或在标签上
QPoint npos = event->pos() + this->pos(); // 计算标签要移动到的新位置
QSize s1 = this->size(); // 窗口大小
QSize s2 = ui->label->size(); // 标签大小
if (npos.x() < 0) // 不超出左侧
npos.setX(0);
else if (npos.x() > s1.width() - s2.width()) // 不超出右侧
npos.setX(s1.width() - s2.width());
if (npos.y() < 0) // 不超出上侧
npos.setY(0);
else if (npos.y() > s1.height() - s2.height()) // 不超出下侧
npos.setY(s1.height() - s2.height());
ui->label->move(npos); // 移动标签到新位置
}
}
}
```
这样就可以避免鼠标指针在按钮(btn)上时导致标签位置计算错误的问题了。
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`。
请确保在适当的时机删除指针以避免内存泄漏。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)