Qt4过渡至Qt5
来源:互联网 发布:vb进度条控件在哪 编辑:程序博客网 时间:2024/05/16 10:18
技术在不断进步,新知识也理应不断学习!Qt5的发布带给我无尽的好奇心,然而,受项目影响,一直使用VS2008 + Qt4.8也未曾及时更新。这几天,果断装上VS2010 + Qt5.1,开始研究。Qt4过渡到Qt5不算显著,然而,“模块化”的Qt代码也需要改变项目配置,如使用“headers”,和配置项目构建(如:改变*.pro文件)。
QtWidgets作为一个独立的模块
例如编译时错误
- error:
QMainWindow: No such file or directory - error:
QToolButton: No such file or directory - error:
QWidget: No such file or directory
解决办法:
在*.pro文件里添加:
- QT
+= widgets
更改
- #include
为
- #include
程序现在应该就可以运行了,但是有时可能需要更加明确的包含
- #include
QtWebKitWidgets也是一个独立的模块:
例如编译时错误
- error:
invalid use of incomplete type 'class QWebFrame' - error:
forward declaration of 'class QWebFrame'
解决办法:
在*.pro文件里添加:
- QT
+= webkitwidgets
注意:当有QT += webkitwidgets的时候,就不再需要QT += widgets
此外,更改
- #inclue
为
- #include
打印机不工作
如果你的代码有以下几行:
- #include
- #include
将以下内容添加到项目文件中:
- Qt
+= printsupport
同样,有时可能仍无法正常工作,需要指定:
- #include QPrinter>
- #include
QPrintDialog>
toAscii()和fromAscii()已被弃用
替换
- fromAscii()
- toAscii()
为
- fromLatin1()
- toLatin1()
例如,给定的Qt4代码
- QByteArry
configfileti =TMP_Config. toAscii();
变为
- QByteArry configfileti
= TMP_Config. toLatin1();
QCoreApplication::UnicodeUTF8已被弃用
- Href_Gui->setWindowTitle(QApplication::translate("Href_Gui",
"Url / www", 0, QApplication::UnicodeUTF8)); - label->setText(QApplication::translate("Href_Gui",
"Text:", 0, QApplication::UnicodeUTF8)); - label_2->setText(QApplication::translate("Href_Gui",
"Url:", 0, QApplication::UnicodeUTF8)); - label_3->setText(QApplication::translate("Href_Gui",
"Target / Name:", 0, QApplication::UnicodeUTF8));
变为
- Href_Gui->setWindowTitle(QApplication::translate("Href_Gui",
"Url / www", 0)); - label->setText(QApplication::translate("Href_Gui",
"Text:", 0)); - label_2->setText(QApplication::translate("Href_Gui",
"Url:", 0)); - label_3->setText(QApplication::translate("Href_Gui",
"Target / Name:", 0));
QWorkspace已被弃用
更换
- #include
为
- #include
QDrag问题
拖动功能的应用程序将需要一些调整。如:
QDrag *drag = new QDrag(event->widget());
在Qt5中将产生错误
- error:
no matching function for call to 'QDrag::QDrag(QWidget*)'
要解决这个附加组件,其中包括:
- #include
qFindChildren已被弃用
这种方式会弹出一个错误:
- error:
'qFindChildren' was not declared in this scope
为了解决这个问题,将qFindChildren替换为findChildren,例如
- toString(const
QObject* obj, int indentLevel) const { - [...]
if (m_children) { QList<</span>QObject*> childlist = qFindChildren<</span>QObject*>(obj, QString()); - [...]
替换
- QList<</span>QObject*>
childlist = qFindChildren<</span>QObject*>(obj, QString());
为
- QList<</span>QObject*>
childlist = obj->findChildren<</span>QObject*>(QString());
qVariantValue已被弃用
编译器会出现
- error:
'qVariantValue' was not declared in this scope
此功能相当于的QVariant::value(value)。因此,如果指定QVariant val应改写
- QTime
t = qVariantValue<</span>QTime>(val);
为
- QTime
t = val.value<</span>QTime>();
- s.setValue("color/favorite",
qVariantValue<</span>QColor>(m_color));
为
- s.setValue("color/favorite",
m_color.value());
qVariantCanConvert已被弃用
替换
- Q_ASSERT(qVariantCanConvert<</span>QString>(variant));
- Q_ASSERT(qVariantCanConvert<</span>QSize>(variant));
- Q_ASSERT(qVariantCanConvert<</span>QFont>(fontVariant));
为
- Q_ASSERT(variant.canConvert(QMetaType::QString));
- Q_ASSERT(variant.canConvert(QMetaType::QSize));
- Q_ASSERT(fontVariant.canConvert(QMetaType::QFont));
Qt::escape已被弃用
- error:
'escape' is not a member of 'Qt'
所以应该更改下面代码:
if (result == QString()) result = Qt::escape(val.toString()); else result = Qt::escape(result); return result;
为
if (result == QString()) result = QString(val.toString()).toHtmlEscaped(); else result = QString(result).toHtmlEscaped(); return result;
QDesktopServices::storageLocation已被弃用
- error:
'storageLocation' is not a member of 'QDesktopServices' - error:
'DataLocation' is not a member of 'QDesktopServices'
使用QStandardPaths::StandardLocation,替换
- QString
path = s.value("db.path",QDesktopServices::storageLocation(QDesktopServices::DataLocation)).toString();
为
- QString
path = s.value("db.path",QStandardPaths::standardLocations(QStandardPaths::DataLocation)).toString();
QtMutimedia替换了Phonon
CONFIG += qtestlib 已被弃用
如果在项目文件中使用,则编译器会发出警告,尽管如此代码将照常运行:
- Project WARNING:
CONFIG+=qtestlib is deprecated. Use QT+=testlib instead.
QWeakPointer怪异
如下代码
- quint64 decodedPointer
= line.toULongLong(); - MetaData
*md = reinterpret_cast<</span>MetaData*>(decodedPointer); - QWeakPointer<</span>MetaData>
wp(md);
结果
- error:
no matching function for call to 'QWeakPointer::QWeakPointer(MetaData*&)'
为了解决这个问题,将下面代码添加到项目文件:
- DEFINES
+= QT_DISABLE_DEPRECATED_BEFORE=0
QtConcurrent库的失踪了?
- C:\Qt\Qt5.0.2\5.0.2\mingw47_32\include\QtConcurrent\qtconcurrentthreadengine
.h:133: error: undefined reference to `_imp___ZN12QtConcurrent16Thread EngineBaseD2Ev'
- m_current
= QtConcurrent::blockingMappedReduced(slices, functor, stitchReduce,QtConcurrent::UnorderedReduce );
则将需要包含头:
- #include QtConcurrent>
到项目文件,并添加下面一行:
- LIBS
+= -lQt5Concurrent
固定的#include <>头
插件加载
部署的系统没有使用C++11
推荐阅读
- C++
API Changes [qt-project.org] - The porting guide
[qt-project.org] - Porting Desktop Applications from Qt 4 to Qt 5
[blog.ics.com] - Porting from Qt 4 to Qt 5
[kdab.com] - Automated porting from Qt 4 to Qt 5
[kdab.com] - Converting a Qt 4 project to Qt 5 – General Advice and Difficulties
[codeproject.com]
类别:
- Developing_Qt
- Developing_with_Qt
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5
- Qt4过渡至Qt5 (错误大全)
- Java线程:创建与启动
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(DP)
- android 调用webService的具体方法
- 使用hibernate3+struts2+ajax+Jquery实现无刷新登录和退出
- 第六章:Specialized Layers
- Qt4过渡至Qt5
- netlink
- ubuntu不能进入登录界面,但可以进入命令行模式
- oracle数据类型-1
- C中调用C++与C++调用C
- Eclipse @Override must override a superclass method 问题解决
- 项目用到的技术
- raw下db文件的使用
- Oracle 递归查询详解