Skip to content

Instantly share code, notes, and snippets.

@alenstarx
Created March 26, 2018 07:42
Show Gist options
  • Save alenstarx/3505d0dcf55f67884ce6d77dec2b7d82 to your computer and use it in GitHub Desktop.
Save alenstarx/3505d0dcf55f67884ce6d77dec2b7d82 to your computer and use it in GitHub Desktop.
Connecting overloaded signals and slots in Qt 5
struct Foo {
void overloadedFunction();
void overloadedFunction(int, QString);
};
// requires C++14
qOverload<>(&Foo:overloadedFunction)
qOverload<int, QString>(&Foo:overloadedFunction)
// same, with C++11
QOverload<>::of(&Foo:overloadedFunction)
QOverload<int, QString>::of(&Foo:overloadedFunction)
// usage c++11
connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged),
slider, &QSlider::setValue);
// usage c++14
connect(spinbox, qOverload<int>(&QSpinBox::valueChanged),
slider, &QSlider::setValue);
// c++11 template
template<typename... Args> struct SELECT {
template<typename C, typename R>
static constexpr auto OVERLOAD_OF( R (C::*pmf)(Args...) ) -> decltype(pmf) {
return pmf;
}
};
// usage
connect(spinbox, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged), ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment