Qt使用单个插槽处理多个webview

时间:2012-05-14 15:46:03

标签: c++ c multithreading qt qwebview

#include <QWebView>
#include <QObject>

class MyWebview  : public QObject {
    Q_OBJECT

private:
    QWebView *v1, *v2, *v3, *v4;

public:
    MyWebview() {
        v1 = new QWebView(0);
        v2 = new QWebView(0);
        v3 = new QWebView(0);
        v4 = new QWebView(0);

        v1->load(QUrl("http://google.com"));
        v2->load(QUrl("http://facebook.com"));
        v3->load(QUrl("http://bing.com"));
        v4->load(QUrl("http://twitter.com"));

        connect(v1, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
        connect(v2, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
        connect(v3, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
        connect(v4, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
    }

public slots:
    void    MyWebview::finished(bool ok)    {   // [ SLOT ]
        switch( /**/ ) {
            case /*if it is v1*/:
            disconnect(v1, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
            break;

        case /*if it is v2*/:
            disconnect(v2, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
            break;

        case /*if it is v3*/:
            disconnect(v3, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
            break;

        case /*if it is v3*/:
            disconnect(v4, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
            break;
        }

        // do something..
    }
}

如何解决这种情况?感谢。

1 个答案:

答案 0 :(得分:1)

使用QSignalMapper。它允许您识别信号的发送者。