在使用自定义QMessageBox弹出警告时,控制台有时弹出报错日志,如
 

QMessageBox* msgBox = new QMessageBox();
msgBox->setWindowTitle("报错");
msgBox->setText(QString("丢包检测-帧号:%1 丢包数:%2")
    .arg(pFrameInfo->nFrameNum).arg(pFrameInfo->nLostPacket));
msgBox->setIcon(QMessageBox::Warning);
msgBox->setWindowModality(Qt::NonModal);
msgBox->setWindowFlags(msgBox->windowFlags() & ~Qt::WindowContextHelpButtonHint);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setAttribute(Qt::WA_DeleteOnClose); 
msgBox->show();	

弹出弹窗时报错:QWindowsWindow::setGeometry: Unable to set geometry 640x480+1171+640 (frame: 658x527+1162+602) on QWidgetWindow/"QMessageBoxClassWindow" on "\\.\DISPLAY1". Resulting geometry: 219x111+1171+640 (frame: 237x158+1162+602) margins: 9, 38, 9, 9 minimum size: 219x111 maximum size: 219x111 MINMAXINFO maxSize=0,0 maxpos=0,0 mintrack=237,158 maxtrack=237,158)

查询之后是由于弹窗大小不匹配,警告了一下,可在show之前手动设置大小或使用推荐大小:

msgBox->resize(400, 200); // 设置固定窗口大小

resize可设置固定大小,但是尺寸不好把控,建议使用sizeHint设置自动推荐的尺寸:

msgBox->resize(msgBox->sizeHint());	//自动计算使用推荐尺寸

最终,改为以下完善该代码不报错:

QMessageBox* msgBox = new QMessageBox();
msgBox->setWindowTitle("报错");
msgBox->setText(QString("丢包检测-帧号:%1 丢包数:%2")
    .arg(pFrameInfo->nFrameNum).arg(pFrameInfo->nLostPacket));
msgBox->setIcon(QMessageBox::Warning);
msgBox->setWindowModality(Qt::NonModal);
msgBox->setWindowFlags(msgBox->windowFlags() & ~Qt::WindowContextHelpButtonHint);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setAttribute(Qt::WA_DeleteOnClose); 
msgBox->resize(msgBox->sizeHint());
msgBox->show();	

除此之外OpenCV也会输出一大堆日志,可将日志等级提高防止输出,设置OpenCV日志级别为ERROR,只显示错误信息,将其放在程序启动前或构造函数:

cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐