Qt 5 introduces some source incompatible changes. Here we list some important ones, but you can find more complete lists in
<Qt 5 Install Dir>/qtbase/dist/changes-5.x.x
.
The following sections list the API changes in each module and provide recommendations for handling those changes.
void
指针。
QLibraryInfo::buildKey()
和相应
QT_BUILD_KEY
macro are removed. You must remove all references to this function and macro, as build key is not necessary now.
n
to -1. We recommend you to update the implementation of this virtual function in your sources.
QBool
is removed. In Qt 4,
QString::contains
(),
QByteArray::contains
(),和
QList::contains
() returned an internal
QBool
type so that the Qt3 code
if (a.contains() == 2)
would not compile anymore. If your code uses
QBool
, replace it with
bool
.
QUuid::operator QString()
implicit conversion operator is removed. Use the
QUuid::toString
() 函数代替。
QSystemLocale
is not public anymore. Use
QLocale
代替。
true
.
QTextCodecPlugin
is removed as all the text codecs are part of Qt Core now.
QFSFileEngine
,
QAbstractFileEngine
,
QAbstractFileEngineIterator
,和
QAbstractFileEngineHandler
are no longer public. We recommend you to avoid using these classes as there is no compatibility promise.
QT_MESSAGE_PATTERN
环境变量。
qInstallMsgHandler
() is deprecated, so we recommend using
qInstallMessageHandler
() 代替。
void *
在所有平台。
operator=
,
operator int
,
operator T*
,
operator!
,
operator==
,
operator!=
,和
operator->
) have been removed as they performed implicit loads and stores of unspecified memory ordering. Code dealing with loading and storing is expected to use
load()
,
loadAquire()
,
store()
,和
storeRelease()
, instead.
QFlags<Enum>::operator int()
matches the enum's underlying type (signed or unsigned) instead of always being
int
. This allows
QFlags
over enums of the unsigned type (for example,
Qt::MouseButton
).
StartWord
and
EndWord
enum values were replaced with the
StartOfItem
and
EndOfItem
values to enforce revision of the affected code.
setSharable()
and
isSharable()
in Qt containers are deprecated and will be removed in Qt 6. You should not use them in new applications. If you used them in existing applications, even though they are not documented, you should port away from them.
The Softkeys API is removed, so the following functions and enums are removed:
QAction::setSoftKeyRole()
QAction::softKeyRole()
QAction::SoftKeyRole
Qt::WA_MergeSoftkeys
Qt::WA_MergeSoftkeysRecursively
Qt::WindowSoftkeysVisibleHint
Qt::WindowSoftkeysRespondHint
QChar::NoCategory
is removed. The
QChar::Other_NotAssigned
enum value is returned for unassigned codepoints now.
lrelease -removeidentical
) instead for optimization.
QCoreApplication::setEventFilter()
and
QApplication::x11EventFilter/macEventFilter/qwsEventFilter/winEventFilter
are replaced with
QCoreApplication::installNativeEventFilter
() 和
QCoreApplication::removeNativeEventFilter
() for an API much closer to
QEvent
filtering.
注意:
The native events that can be filtered this way depend on the
QPA
backend chosen at runtime. On X11, XEvents are replaced with
xcb_generic_event_t
due to the switch to XCB, which requires porting the application code to XCB as well.
QCoreApplication::Type
and
QApplication::type()
are removed. These Qt 3 legacy application types did not match the application types available in Qt 5. Use
qobject_cast
instead to dynamically find out the exact application type.
QEvent::AccessibilityPrepare
,
QEvent::AccessibilityHelp
,和
QEvent::AccessibilityDescription
are removed.
QFont::handle()
is removed.
QMetaType::construct()
is renamed as
QMetaType::create
().
QMetaType::unregisterType()
is removed.
true
, where it was
false
before.
QMetaType::QWidgetStar
is removed. Use
qMetaTypeId<QWidget*>
() 或
QVariant::canConvert<QWidget*>
() 代替。
Q_DECLARE_METATYPE(type)
now requires
type
to be fully defined. This means,
Q_DECLARE_METATYPE(QSharedPointer<t>)
and
Q_DECLARE_METATYPE(QWeakPointer<t>)
also need
type
to be fully defined. In cases where a forward declared type is needed as a meta-type, use
Q_DECLARE_OPAQUE_POINTER(type)
.
QMetaMethod::signature()
is renamed as
QMetaMethod::methodSignature
(), and the return type is changed to
QByteArray
. This change is to enable generating the signature string on demand, rather than storing it in the meta-data.
void
, instead it returns
"void"
. The recommended way of checking whether a method returns
void
is to compare the return value of
QMetaMethod::returnType
() 采用
QMetaType::Void
.
const char
指针。
QCoreApplication::Encoding
are deprecated. Qt assumes that the source code is encoded in UTF-8.
QVariant(Qt::red)
,使用
QVariant(QColor(Qt::red))
to create a
QVariant
实例。
static_cast<int>(Qt::SolidLine)
to create a
QVariant
类型
int
with the same value as the enum.
registerTimer()
function is changed. All subclasses of
QAbstractEventDispatcher
must re-implement the function with this new signature:
virtual void registerTimer(int timerId, int interval,Qt::TimerType timerType, QObject *object) = 0;
typedef
of
QPair<int, int>
. It is now a
struct
with 3 members:
int timerId
,
int interval
,和
Qt::TimerType timerType
. Update the
QAbstractEventDispatcher::registeredTimers
() function with this change.
convertSeparators()
function is removed. Use
QDir::toNativeSeparators
() 代替。
QDir::NoDotAndDotDot
enum value is removed. Use
QDir::NoDot
or
QDir::NoDotDot
代替。
layoutAboutToBeChanged
signal, and
QAbstractItemModel::endMoveRows
() no longer emits the
layoutChanged
signal. All proxy models must connect to (and disconnect from) the
QAbstractItemModel::rowsAboutToBeMoved
and
QAbstractItemModel::rowsMoved
signals.
void*
and
quintptr
overloads, making calls with a literal
0
(
createIndex(row, col, 0)
) ambiguous. Either cast
quintptr(0)
or omit the third argument to get the
void*
overload.
-1
, replacing the separate constructors that did the same.
QString
,
QStringRef
,和
QByteArray
respectively, if the position passed is equal to the length (that is, right after the last character or byte). In Qt 4, they returned a null
QString
or a null
QStringRef
.
This is to guarantee consistent default conversion of strings. For locale-aware conversions use the equivalent QLocale 函数。
qint64
argument, and the
QDate::daysTo
() 和
QDateTime::daysTo
() functions now return a
qint64
值。
userProperty
为
QTimeEdit
and
QDateEdit
classes, insted of
QDateTime
as it was for the 4.7 and 4.8 releases. The USER property for these classes was removed before Qt 4.7.0 and added again in Qt 5.0.
ymd hms
) regardless of the underlying system time, time zone, or changes in the system zone. Therefore, the consistent behavior when serialising is to save and restore as the local time and not its UTC equivalent. This means that the
QDataStream
version is 14 since Qt 5.1.
qint64
, and as able to be used in
QDateTime::setMSecsSinceEpoch
() 和
QDateTime::toMSecsSinceEpoch
().
QTextCodec::codecForCStrings()
and
QTextCodec::setCodecForCStrings()
are removed as they were creating uncertainty/bugs in using
QString
easily and (to a lesser extent) performance issues.
QTextCodec::codecForTr()
and
QTextCodec::setCodecForTr()
are removed.
QUrl changed considerably in Qt 5 to comply better with the URL specifications and with brokenness out there. The following list summarizes the most important functional changes:
The most notable difference with this change is when dealing with QUrl::toString (). In earlier Qt versions, this function would return percent characters in the URL, but now it returns %25 like QUrl::toEncoded () 做。
userName
,
password
,
host
,
topLevelDomain
,
path
and
fileName
. This means a % in one of those fields is now returned (or set) as
%
而不是
%25
. If the former behavior was expected, pass
PrettyDecoded
to the getter and
TolerantMode
to the setter.
addQueryItem()
and
queryPairDelimiter()
. These have been moved to the new
QUrlQuery
类。
FullyEncoded
) 或
QUrl::toEncoded
().
Qt Concurrent has been moved from Qt Core to its own module. To link against the Qt Core module, add this line to the project file:
QT += concurrent
注意:
The
QtConcurrent::Exception
class is renamed
QException
and
QtConcurrent::UnhandledException
class is renamed
QUnhandledException
. They remain in Qt Core.
string
insted of
int
.
child
parameter anymore, and the corresponding
QAccessibleEvent::child()
function is removed.
hiResGlobalPos
argument anymore, as all coordinates are floating point-based now.
QIconEngineV2
is now merged into
QIconEngine
. Update your sources to use
QIconEngine
而不是
QIconEngineV2
.
QDesktopServices::storageLocation()
and
QDesktopServices::displayName()
are replaced by
QStandardPaths::writableLocation
() 和
QStandardPaths::displayName
() respectively. They are now in the
Qt Core
module. Make sure to read the
QDesktopServices::storageLocation
() documentation when porting from
QDesktopServices::DataLocation
.
QPixmap::grabWindow()
and
QPixmap::grabWidget()
are removed. Use
QScreen::grabWindow
() 代替。
commitData()
function is removed and replaced by the signal
QGuiApplication::commitDataRequest
().
QApplication
and
QGuiApplication
will emit this signal from 5.2 onward on supported platforms (Linux and Windows).
QWidget *widget()
被替换采用
QObject *target()
to avoid
QWidget
dependencies.
text(Text t, int child)
现为
text
(Text t)
rect(int child)
现为
rect()
setText(Text t, int child, const QString &text)
现为
setText(Text t, const QString &text)
role(int child)
现为
role()
state(int child)
现为
state()
navigate()
被替换采用
parent()
and
child()
to navigate the hierarchy.
relationTo()
被替换采用
relations()
.
QAccessibleInterface::userActionCount()
,
QAccessibleInterface::actionText()
,和
QAccessibleInterface::doAction()
are removed. We recommend using the
QAccessibleInterface
subclasses to implement the
QAccessibleActionInterface
代替。
Format_RGB888
now expects image data in RGB layout as opposed to BGR layout. This is to ensure consistency with RGB32 and other 32-bit formats.
false
), the the existent image data is invalidated, so that
isNull()
is guaranteed to return
true
在此情况下。
DeviceType
值。
TouchPointStateMask
and
TouchPointPrimary
are removed from the
Qt::TouchPointStates
枚举。
QTouchEvent::TouchPoint::isPrimary()
is removed.
int
而不是
QVariant::Type
.
QInputContext
is removed as related getter and setter functions in
QWidget
and
QApplication
are removed. Input contexts are now platform-specific.
QStyleOption*V{2,3,4}
classes are removed, and their members are merged with the respective base classes. The removed classes are left as typedefs for binary compatibility.
QProxyModel
is removed. Use
QAbstractProxyModel
and the related classes instead. A copy of
QProxyModel
is available in the
Ui Helpers
存储库。
QApplication::setGraphicsSystem()
is removed, because the introduction of
QPA
made it redundant.
QApplication::commitData
and
QApplication::saveState
virtual methods that were used for session management are removed. Connect to the
QApplication::commitDataRequest
and
QApplication::saveStateRequest
signals instead. Use
QApplication::isSavingSession
() 若
QWidget::closeEvent
if your window needs to know whether it is being called during shutdown.
QStyle::standardIconImplementation()
and
QStyle::layoutSpacingImplementation()
are removed, and the
QStyle::standardIcon
() 和
QStyle::layoutSpacing
() functions are made pure virtual now. The removed functions were introduced in Qt 4 for binary compatibility reasons.
QMotifStyle
,
QPlastiqueStyle
,
QCleanlooksStyle
,和
QCDEStyle
are replaced with a new
fusion
style. If your application depends on any of these removed styles, you can either use the
qtstyleplugins
project to get these styles or update your application to use the new fusion style. For more details about this change, see
https://blog.qt.io/blog/2012/10/30/cleaning-up-styles-in-qt5-and-adding-fusion/
.
Instead of creating instances or inheriting these classes directly, use:
The following functions are deprecated:
void setMovable(bool movable)
- Use
QHeaderView::setSectionsMovable
() 代替。
bool isMovable() const
- Use
QHeaderView::sectionsMovable
() 代替。
void setClickable(bool clickable)
- Use
QHeaderView::setSectionsClickable
() 代替。
bool isClickable() const
- Use
QHeaderView::sectionsClickable
() 代替。
void setResizeMode(int logicalindex, ResizeMode mode)
- Use
QHeaderView::setSectionResizeMode
() 代替。
ResizeMode resizeMode(int logicalindex) const
- Use
QHeaderView::sectionResizeMode
() 代替。
clicked()
signal on the left mouse click only, instead of all mouse clicks.
dataChanged()
signal in the model.
QRgb
.
QRgb
.
webkit
and
webkitwidgets
, in order to allow the dependency of the
widgets
module to be optional. The benefits will only be available once the QQuickWebView API is made public through C++ in a future version. The
webkitwidgets
module adds the
webkit
module automatically to the project, so in your application's project file,
QT += webkit
should now be modified to:
QT += webkitwidgets
#include <QtWebKit/QtWebKit>
should be modified to:
#include <QtWebKitWidgets/QtWebKitWidgets>
#include <QWebPage>
The include path will take care of selecting the right module.
#include <qwebkitversion.h>
should be replaced with two includes:
#include <qtwebkitversion.h> #include <qwebkitglobal.h>
QPageSetupDialog::PageSetupDialogOption
enum and the corresponding set and get functions,
QPageSetupDialog::options()
and
QPageSetupDialog::setOptions()
are removed.
QAbstractPageSetupDialog
class is removed. Use
QPrintDialog
代替。
PPK_SuppressSystemPrintStatus
is removed.
QSsl::TlsV1
enum value is renamed as
QSsl::TlsV1_0
.
QHttp
,
QHttpHeader
,
QHttpResponseHeader
,和
QHttpRequestHeader
classes are removed. Use
QNetworkAccessManager
代替。
QFtp
and
QUrlInfo
classes are no longer exported. Use
QNetworkAccessManager
instead. Programs that require raw FTP or HTTP streams can use the
Qt FTP
and
Qt HTTP
compatibility add-on modules that provide the
QFtp
and
QHttp
classes as they existed in Qt 4.
QAbstractSocket::connectToHostImplementation()
and
QAbstractSocket::disconnectFromHostImplementation()
are removed.
qintptr
instead of an
int
.
QNetworkConfiguration::bearerName()
is removed. Use the
bearerTypeName()
代替。
modelAboutToBeReset()
and
modelReset()
signals are sufficient to inform views that they must re-interrogate the model.
xxxImplementation
functions are also removed as the subclasses of
QSqlDriver
can re-implement those directly.
QTRY_VERIFY
and
QTRY_COMPARE
macros are now part of
QTestLib
. These macros were part of
tests/shared/util.h
earlier, but now they are part of the <
QtTest
/
QtTest
> header. In addition,
QTRY_VERIFY_WITH_TIMEOUT
and
QTRY_COMPARE_WITH_TIMEOUT
are provided to specify custom timeout values.
QTEST_NOOP_MAIN
macro is removed. If a test appears to be inapplicable for a particular build at compile-time, it should be omitted either using the .pro file logic or call
QSKIP
在
initTestCase()
method to skip the entire test. If you're using the later approach, report a meaningful explanation in the test log.
DEPENDS_ON
macro is removed as it misled some users to believe that they could make test functions depend on each other or impose an execution order on test functions.
QTest::qt_snprintf()
is removed. This was an internal test library function that was exposed in the public API due to its use in a public macro. Any calls to this function must be replaced with
QByteArray::qsnprintf()
.
QCOMPARE
macro, which provides more informative output in the event of a failure.
"mode"
parameter in
QSKIP
macro is removed. This parameter caused problems in test metrics calculation because of the
SkipAll
mode, which hid information about the skipped test data. Calling
QSKIP
in a test function now behaves like
SkipSingle
, which is skipping a non-data-driven test function or skipping only the current data row of a data-driven test function. Every skipped data row is now reported in the test log.
qCompare()
has been replaced by overloading. Code such as
qCompare<QString>(l, r)
no longer uses the
QString
-specific implementation and may fail to compile. We recommend that you replace specialization with overloading. Also, rather than pass explicit template arguments to
qCompare
, let the overload resolution pick the correct one, and cast arguments in case of ambiguous overloads (for example,
qCompare(QString(l), r)
). The resulting code will continue to work against older QtTestlib versions.
QTest::qWaitForWindowShown()
被替换采用
QTest::qWaitForWindowExposed
().
Apart from the QGLWidget class, the Qt OpenGL module should not be used for new code. Instead, use the corresponding OpenGL classes in Qt GUI .
QGLContext::generateFontDisplayLists()
and
QGLWidget::fontDisplayListBase()
are removed.
listBase
parameter is removed from
QGLWidget::renderText
() 函数。
OpenGL
. First, you must call
QGLWidget::makeCurrent
() at least once per each
QGLWidget::swapBuffers
() call, so that the platform has a chance to synchronize resizing the
OpenGL
surface. Second, before calling
QGLWidget::makeCurrent
() 或
QGLWidget::swapBuffers
() in a separate thread, you must call
QGLContext::moveToThread
() to explicitly let Qt know in which thread a
QGLContext
is currently being used. You also need to make sure that the context is not current in the current thread before moving it to a different thread.
qMacVersion()
is removed. Use
QSysInfo::macVersion
() 或
QSysInfo::MacintoshVersion
代替。
Qt::escape()
is deprecated. Use
QString::toHtmlEscaped
() 代替。
qIsDetached<>
is removed, becauses it is irrelevant for multi-threaded applications. There is no replacement for this function.
qttest_p4.prf
file is removed. Use
CONFIG+=testcase
and other flags instead.
-dwarf2
configure argument is removed. DWARF2 is always used on macOS now.
qmake -recursive
by default, because the subsequent build invokes qmake as needed. Use
-fully-process
to restore the old behavior.
CODECFORTR
variable is deprecated, because source code written with Qt 5 is expected to use UTF-8 encoding.