Qt 5.12迁移至5.15时setWindowFlags函数报错问题
Qt 5.12迁移至5.15时setWindowFlags编译错误解决
问题场景
在Ubuntu 20.04环境下将项目从Qt 5.12迁移到Qt 5.15时,调用setWindowFlags设置窗口标志出现编译错误。
出错代码
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
报错信息
mainwindow.cpp:28:5: error: cannot initialize object parameter of type 'QWidget' with an expression of type 'MainWindow'
尝试过的无效方法
Qt::WindowFlags flags; flags |= Qt::Window; flags |=Qt::FramelessWindowHint; setWindowFlags(flags);
MainWindow构造函数完整代码
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { translator.load("://languages/translation_en.qm"); qApp->installTranslator(&translator); //Initial UI setup. #ifndef DESKTOP /* Qt::WindowFlags flags; flags |= Qt::Window; flags |=Qt::FramelessWindowHint; setWindowFlags(flags); */ setWindowFlags(Qt::Window | Qt::FramelessWindowHint); //this is the line in question setWindowState(Qt::WindowFullScreen); #endif ui->setupUi(this); ui->remoteStatus->setVisible(false); ui->simpleRemoteStatus->setVisible(false); ui->simpleFrame->hide(); ui->dashboardFrame->hide(); ui->childFrame->hide(); startButtonDown = ui->simpleStartButton->isChecked(); stopButtonDown = ui->simpleStopButton->isChecked(); setupIcons(); // double hmiver = 380;//version number #ifdef CYCLE hmiver = 999; #endif //Setup for Modbus Slave thread = new QThread(this); data = new DataThread(); data->moveToThread(thread); connect(thread, SIGNAL(started()), data, SLOT(runProcess())); // setupMenus(); comsMod->RetainedData.HMIVer = hmiver; comsMod->RTData.HMIVer = hmiver; settingsMenu->aboutMenu->setHMIVer(hmiver); setupTimers(); connectAll(); //Starts RTM communications on device side DisplayCountTimer->setInterval(100); DisplayCountTimer->start(); //After 2 seconds, starts remaining processes StartModbusTimer->setInterval(2000); StartModbusTimer->start(); /* Used for cycle testing */ #ifdef USBTEST usbTest(); #endif /* Used for cycle testing */ #ifdef CYCLE this->on_productionButton_clicked(); #endif }
解决方法
这个错误源于Qt 5.15对setWindowFlags的重载解析逻辑变更,编译器错误地匹配了需要QWidget参数的重载,而非QMainWindow的成员函数。
可以通过以下方式修复:
- 用
this指针明确调用成员函数,帮助编译器正确解析:this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); - 显式转换窗口标志类型,引导编译器匹配正确重载:
setWindowFlags(static_cast<Qt::WindowType>(Qt::Window | Qt::FramelessWindowHint)); - 若项目中存在自定义的
setWindowFlags全局函数或其他类成员函数,需明确限定作用域:QMainWindow::setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
内容的提问来源于stack exchange,提问作者Brian
相关产品推荐
相关产品推荐

