Skip to content

Commit

Permalink
Enable building Qt Signal Tools with Qt 5
Browse files Browse the repository at this point in the history
QtSignalForwarder is partly obsoleted by the new signal connection
facilities in Qt 5, however to make porting easier enable Qt Signal
Tools and tests/examples to be built with both Qt 4 and 5.
  • Loading branch information
Robert Knight committed Jun 24, 2013
1 parent 0b40082 commit c602dc3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
4 changes: 4 additions & 0 deletions QtCallback.cpp
Expand Up @@ -165,7 +165,11 @@ bool QtCallbackBase::invokeWithArgs(const QGenericArgument& a1, const QGenericAr
}

if (!d->method.invoke(d->receiver.data(), args[0], args[1], args[2], args[3], args[4], args[5], args[6])) {
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
qWarning() << "Failed to invoke method" << d->method.methodSignature();
#else
qWarning() << "Failed to invoke method" << d->method.signature();
#endif
return false;
}
return true;
Expand Down
36 changes: 36 additions & 0 deletions SafeBinder.h
Expand Up @@ -4,6 +4,7 @@
#include "FunctionTraits.h"

#include <QtCore/QDebug>
#include <QtCore/QPointer>
#include <QtCore/QSharedPointer>

namespace QtSignalTools
Expand All @@ -30,6 +31,10 @@ struct StrongRef<QWeakPointer<T> >
if (m_strongRef) {
return m_strongRef.data();
} else {
// this may either be a QWeakPointer constructed
// from a QObject* which is not tracked by a QSharedPointer or
// the object may have been deleted, in which case data() will
// return 0
return m_weakRef.data();
}
}
Expand All @@ -38,6 +43,25 @@ struct StrongRef<QWeakPointer<T> >
QWeakPointer<T> m_weakRef;
};

// version for QPointer<T>.
template <class T>
struct StrongRef<QPointer<T> >
{
StrongRef(QPointer<T>& ref)
: m_ref(ref)
{
// There is no way to actually promote QPointer<T> to
// a strong reference, so we can't do that here as
// we do for other StrongRef implementations
}

T* data() const {
return m_ref.data();
}

QPointer<T> m_ref;
};

// version for weak_ptr<T>
template <template <class T> class WeakPointer, class T>
struct StrongRef<WeakPointer<T> >
Expand Down Expand Up @@ -127,12 +151,24 @@ SafeBinder<WeakPointer<T>,MemberFunc> safe_bind(const WeakPointer<T>& r, MemberF
return SafeBinder<WeakPointer<T>,MemberFunc>(r,f);
}

// Under Qt 4 QWeakPointer is the most efficient weak reference
// to QObject. Under Qt 5, QWeakPointer no longer integrates with
// QObject but QPointer was instead re-written to be more efficient
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
template <class T, class MemberFunc>
SafeBinder<QPointer<T>,MemberFunc> safe_bind(T* r, MemberFunc f,
typename enable_if<is_base_of<QObject,T>::value,T>::type* = 0)
{
return SafeBinder<QPointer<T>,MemberFunc>(r,f);
}
#else
template <class T, class MemberFunc>
SafeBinder<QWeakPointer<T>,MemberFunc> safe_bind(T* r, MemberFunc f,
typename enable_if<is_base_of<QObject,T>::value,T>::type* = 0)
{
return SafeBinder<QWeakPointer<T>,MemberFunc>(r,f);
}
#endif

}

12 changes: 6 additions & 6 deletions examples/ui-controls/UiDemo.cpp
@@ -1,11 +1,11 @@
#include "QtSignalForwarder.h"

#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QMouseEvent>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QSlider>
#include <QApplication>
#include <QLabel>
#include <QMouseEvent>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>

bool matchRightClick(QObject*, QEvent* event)
{
Expand Down
1 change: 1 addition & 0 deletions examples/ui-controls/ui-controls.pro
@@ -1,5 +1,6 @@
include(../common.pri)

TEMPLATE = app
QT += widgets

SOURCES += UiDemo.cpp
1 change: 1 addition & 0 deletions examples/web-page-fetcher/web-page-fetcher.pro
@@ -1,6 +1,7 @@
include(../common.pri)

TEMPLATE = app
QT += concurrent

HEADERS += WebPageDownloader.h
SOURCES += WebPageDownloader.cpp
10 changes: 8 additions & 2 deletions tests/TestQtCallback.cpp
Expand Up @@ -11,6 +11,12 @@

#include <iostream>

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#define SKIP_TEST(message) QSKIP(message)
#else
#define SKIP_TEST(message) QSKIP(message, SkipAll)
#endif

#if defined(QST_USE_CPP11_LIBS)
using namespace std;
using namespace std::placeholders;
Expand Down Expand Up @@ -187,7 +193,7 @@ void TestQtCallback::testSignalToLambda()
tester.emitASignal(7);
QCOMPARE(sum, 19);
#else
QSKIP("Compiler does not support C++11 lambdas", SkipAll);
SKIP_TEST("Compiler does not support C++11 lambdas");
#endif
}

Expand Down Expand Up @@ -261,7 +267,7 @@ void TestQtCallback::testProxyBindingLimits()
void TestQtCallback::testConnectPerf()
{
#if QT_VERSION >= QT_VERSION_CHECK(4,8,0)
QSKIP("Benchmark disabled", SkipAll);
SKIP_TEST("Benchmark disabled");

CallbackTester receiver;

Expand Down

0 comments on commit c602dc3

Please sign in to comment.