[Interest] Are slots even needed these days?
Henry Skoglund
fromqt at tungware.se
Sat Mar 19 08:58:49 CET 2016
Hi, I thought I was the only one disliking the PMF syntax.
But there's hope, to aid my feeble brain I wrote some macros, so that
the new syntax resembles the old, but still catching errors in compile time:
#define COMPOSEPMF(QOBJPTR,FUNC) \
&std::decay<decltype(*QOBJPTR)>::type::FUNC
#define CONNECT(SENDER,FUNC1,RECEIVER,FUNC2) \
connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER,COMPOSEPMF(RECEIVER,FUNC2))
Those macros let you write code like this:
...
CONNECT(ui->pushButton,clicked,this,buttonClicked);
...
Less horrid (!) they use std::decay to retrieve the type and prefix it
to the function, thus setting up correct PMF syntax in all its glory.
However, it wasn't long before I stumbled upon the casting problem, like
in QCombobox and QSignalMapper. So I had to write 3 more macros:
#define COMPOSECASTPMF(QOBJPTR,ARGS,FUNC) \
static_cast<void (std::decay<decltype(*QOBJPTR)>::type::*)(ARGS)> \
(COMPOSEPMF(QOBJPTR,FUNC))
#define CONNECTSCAST(SENDER,ARGS,FUNC1,RECEIVER,FUNC2) \
connect(SENDER,COMPOSECASTPMF(SENDER,ARGS,FUNC1),RECEIVER,COMPOSEPMF(RECEIVER,FUNC2))
#define CONNECTRCAST(SENDER,FUNC1,RECEIVER,ARGS,FUNC2) \
connect(SENDER,COMPOSEPMF(SENDER,FUNC1),RECEIVER,COMPOSECASTPMF(RECEIVER,ARGS,FUNC2))
With those I can write code like so:
...
ui->comboBox->addItems({"Now","is","the","time"});
CONNECTSCAST(ui->comboBox,const QString &,
currentIndexChanged,this,indexChanged);
...
auto sm = new QSignalMapper(ui->pushButton);
CONNECTRCAST(ui->pushButton,clicked,sm,void,map);
...
So yeah, the cast wreaks a bit of a havoc, but you only have to specify
the casting argument(s), std::decay takes care of prefixing the type.
Note: the macros only support either a sending or a receiving cast,
hopefully a cast on both sides will never be needed...
Also, beginning now with 5.6 you don't even have to specify CONFIG +=
c++11 in your .pro file, the macros work right out of the box.
Rgrds Henry
On 2016-03-16 18:24, John Weeks wrote:
> Nobody's mentioned the fact that an overridden virtual slot requires an
> absolutely horrid cast in order to use the new PMF syntax.
>
> John Weeks
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
More information about the Interest
mailing list