[英]Replacing deprecated QtSignalMapper class to forward Signals in Qt5
I have this code which makes a mdi window written for Qt 4: 我有这样的代码,它为Qt 4编写了一个mdi窗口:
class MdiWindow : public QMainWindow
{
Q_OBJECT
public:
MdiWindow( QWidget *parent = nullptr)
...
private:
QWorkspace* workspace
QSignalMapper* mapper
}
MdiWindow::MdiWindow( QWidget *parent ) : QMainWindow( parent )
{
...
workspace = new QWorkspace;
setCentralWidget( workspace );
connect( workspace, SIGNAL(windowActivated(QWidget *)), this, SLOT(enableActions()));
mapper = new QSignalMapper( this );
connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
....
}
According to the QT documentation QWorkspace
should be replaced with QMdiArea
. 根据QT文档,
QWorkspace
替换为QMdiArea
。
I did that and wrote the first connect like this: 我这样做了,并这样写了第一个连接:
connect(workspace, &QMdiArea::subWindowActivated,
this, &MdiWindow::enableActions);
But what about QSignalMapper
also that is also deprecated. 但是
QSignalMapper
也被弃用了。
So how can I update this line: 因此,我如何更新此行:
mapper = new QSignalMapper( this );
connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
I read QSignalMapper
can be replaced with lamdas but how in this case? 我读过
QSignalMapper
可以用lamda代替,但是在这种情况下怎么办? If i understand right mapper
forwards all the signals from this to the active window of workspace
如果我理解正确的
mapper
,则将所有信号从此转发到workspace
的活动窗口
Previously you used QSignalMapper::setMapping()
to make sure that you'd be sent data you need when the SLOT()
was called. 以前,您使用
QSignalMapper::setMapping()
来确保在调用SLOT()
时收到需要的数据。 Now you can just encapsulate this logic inside a lamba, so if you did (like in Qt example ): 现在,您可以将此逻辑封装在lamba中,因此,如果这样做(如Qt示例 ):
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
}
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));
you can now do (somewhat): 您现在可以做(某种程度上):
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, &QPushButton::clicked, [=]() {
emit clicked(texts[i]);
});
}
If the setMapping()
is not being used, then it probably could have been connected directly to a SLOT()
already. 如果未使用
setMapping()
,则它可能已经直接连接到SLOT()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.