[英]Passing custom arguments to a slot in Qt/C++
I'm having some troubles trying to pass a custom widget to a custom slot inside my Qt App. 我在尝试将自定义小部件传递到Qt应用程序内的自定义插槽时遇到了一些麻烦。
Here an example of what I need: (Note the slots) 这是我需要的示例:(请注意插槽)
void MainWindow::HttpRequest(const QString & URL, QCustomWidget *Feed) {
manager = new QNetworkAccessManager(this);
reply = manager->get(QNetworkRequest(QUrl(URL)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(HttpImageError(Feed)));
connect(reply, SIGNAL(finished()), this, SLOT(HttpImageFinished(Feed)));
}
I've already searched on Google and I've found that QSignalMapper
is used to pass arguments to slots, but QSignalMapper
is usable only to pass integers, QStrings
, QObjects
and QWidgets
. 我已经搜索在谷歌和我发现,
QSignalMapper
用于参数传递给插槽,但是QSignalMapper
可用只是为了消磨整数, QStrings
, QObjects
和QWidgets
。 I need to pass a custom widget. 我需要传递一个自定义小部件。 I've also read that there are tricks to wrap the custom widget inside a struct or something like that, but I'm very confused on how to do that.
我还读到有一些技巧可以将自定义窗口小部件包装在struct之类的东西中,但是我对如何做到这一点感到非常困惑。
Does anybody can help me? 有人可以帮助我吗?
With Qt 5 and C++11 you can use a new lambda syntax: 通过Qt 5和C ++ 11,您可以使用新的lambda语法:
connect(reply, &QNetworkReply::NetworkError, [this, Feed]() {
this->HttpImageError(Feed);
});
Here an additional Feed
parameter to the slot function is added to lambda capture block. 此处,插槽功能的其他
Feed
参数添加到了lambda捕获块。
Just for further information... I found another solution to the problem, in fact to pass a custom widget I've subclassed the QSignalMapper class (I haven't studied OOP at the University yet, so be patient with me! :P), here some code: 只是为了获取更多信息...我找到了解决该问题的另一种方法,实际上是通过传递一个自定义小部件,我将其子类化为QSignalMapper类(我还没有在大学学习OOP,所以请耐心等待我!:P) ,这里是一些代码:
QCustomMapper.h QCustomMapper.h
#ifndef QCUSTOMMAPPER_H
#define QCUSTOMMAPPER_H
#include <QSignalMapper>
#include <QHash>
#include "customwidget.h"
class QCustomMapper : public QSignalMapper
{
Q_OBJECT
public:
explicit QCustomMapper(QObject *parent = 0);
void setMapping(QObject *sender, CustomWidget *customWidget);
void removeMappings(QObject *sender);
QHash<QObject *, CustomWidget *> customHash;
Q_SIGNALS:
void mapped(CustomWidget *);
public slots:
void senderDestroyed() {
removeMappings(sender());
}
void map(QObject *sender);
void map();
};
#endif // QCUSTOMMAPPER_H
QCustomMapper.cpp QCustomMapper.cpp
#include "qcustommapper.h"
QCustomMapper::QCustomMapper(QObject *parent) : QSignalMapper(parent) {
}
void QCustomMapper::setMapping(QObject *sender, CustomWidget *customWidget) {
customHash.insert(sender, customWidget);
connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
}
void QCustomMapper::removeMappings(QObject *sender) {
customHash.remove(sender);
}
void QCustomMapper::map(QObject *sender) {
if (customHash.contains(sender)) {
emit mapped(customHash.value(sender));
}
}
void QCustomMapper::map() {
map(sender());
}
NOTE: I don't know if this is an "elegant" method or if the subclassing is done correctly.. (I've done it without the right knoledgments) anyway it's working fine for me! 注意:我不知道这是不是“优雅”的方法还是子类化是否正确完成了。(我没有正确的方法就完成了它)对我来说一切正常! (miracles happen :D) I hope that this post will help someone that has the same problem!
(奇迹发生了:D)我希望这篇文章能对遇到同样问题的人有所帮助!
Bye, Matteo. 再见,Matteo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.