QHostInfoResult: use the same semantics as the contextless connect()
The 3-arg QObject::connect() overload (the "contextless" one) simply forwards to the 4-arg overload, using the sender object as both the sender and the receiver/context. QHostInfo does not use connect() directly in order to emit notifications that a lookup is finished. Instead, it uses some of QObject private plumbing. When handling a lookup request that specifies a function to call when done, but no context object, QHostInfo passes nullptr as context/receiver. Change QHostInfoResult's behavior to always have a "receiver": in case the caller didn't specify one, use `this` (= the sender). As a drive-by, this allows to streamline some code. Change-Id: Ic2e63ac18ba36269036950b6f6b7fecea51d0c99 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>bb10
parent
8550d60aca
commit
797edbc58c
|
|
@ -73,10 +73,10 @@ Q_APPLICATION_STATIC(QHostInfoLookupManager, theHostInfoLookupManager)
|
|||
}
|
||||
|
||||
QHostInfoResult::QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot)
|
||||
: receiver{receiver}, slotObj{std::move(slot)}, withContextObject{slotObj && receiver}
|
||||
: receiver{receiver ? receiver : this}, slotObj{std::move(slot)}
|
||||
{
|
||||
if (receiver)
|
||||
moveToThread(receiver->thread());
|
||||
Q_ASSERT(this->receiver);
|
||||
moveToThread(this->receiver->thread());
|
||||
}
|
||||
|
||||
QHostInfoResult::~QHostInfoResult()
|
||||
|
|
@ -101,7 +101,7 @@ void QHostInfoResult::postResultsReady(const QHostInfo &info)
|
|||
return;
|
||||
}
|
||||
// we used to have a context object, but it's already destroyed
|
||||
if (withContextObject && !receiver)
|
||||
if (!receiver)
|
||||
return;
|
||||
|
||||
static const int signal_index = []() -> int {
|
||||
|
|
@ -129,11 +129,13 @@ bool QHostInfoResult::event(QEvent *event)
|
|||
{
|
||||
if (event->type() == QEvent::MetaCall) {
|
||||
Q_ASSERT(slotObj);
|
||||
auto metaCallEvent = static_cast<QMetaCallEvent *>(event);
|
||||
auto args = metaCallEvent->args();
|
||||
// we didn't have a context object, or it's still alive
|
||||
if (!withContextObject || receiver)
|
||||
|
||||
// we used to have a context object, but it's already destroyed
|
||||
if (receiver) {
|
||||
auto metaCallEvent = static_cast<QMetaCallEvent *>(event);
|
||||
auto args = metaCallEvent->args();
|
||||
slotObj->call(const_cast<QObject*>(receiver.data()), args);
|
||||
}
|
||||
|
||||
deleteLater();
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -56,8 +56,8 @@ protected:
|
|||
|
||||
private:
|
||||
QHostInfoResult(const QHostInfoResult *other)
|
||||
: receiver(other->receiver), slotObj{copy(other->slotObj)},
|
||||
withContextObject(other->withContextObject)
|
||||
: receiver(other->receiver.get() != other ? other->receiver.get() : this),
|
||||
slotObj{copy(other->slotObj)}
|
||||
{
|
||||
// cleanup if the application terminates before results are delivered
|
||||
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
|
||||
|
|
@ -66,9 +66,10 @@ private:
|
|||
moveToThread(other->thread());
|
||||
}
|
||||
|
||||
// receiver is either a QObject provided by the user,
|
||||
// or it's set to `this` (to emulate the behavior of the contextless connect())
|
||||
QPointer<const QObject> receiver = nullptr;
|
||||
QtPrivate::SlotObjUniquePtr slotObj;
|
||||
const bool withContextObject = false;
|
||||
};
|
||||
|
||||
class QHostInfoAgent
|
||||
|
|
|
|||
Loading…
Reference in New Issue