简体   繁体   English

无法将loadFinished SIGNAL连接到自定义SLOT

[英]Trouble connecting loadFinished SIGNAL to custom SLOT

I'm new to Qt, C++ and signals and slots. 我是Qt,C ++以及信号和插槽的新手。 I'm trying to load in a webpage. 我正在尝试加载网页。 Then set a label_3's text to the title of the webpage. 然后将label_3的文本设置为网页的标题。 To do this I figured I had to connect the loadFinished signal to my custom function. 为此,我想我必须将loadFinished信号连接到我的自定义函数。 But I'm having trouble doing just that. 但我这样做有困难。

I've read up on the manual, different examples and other questions, but I'm stuck. 我已经阅读了手册,不同的例子和其他问题,但我被困住了。 This is a excerpt from code I have so far. 这是我到目前为止的代码的摘录。

How do I properly connect the signal loadFinished() to my function labelSetText()? 如何将信号loadFinished()正确连接到我的函数labelSetText()?

main.cpp main.cpp中

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp mainwindow.cpp

void MainWindow::on_pushButton_clicked()
{
    QString webAdress = ui->lineEdit->text();

    QWebView *view = ui->webView;
    view->load(QUrl(webAdress));

    QString taxt = view->title();

    connect(&view,  SIGNAL(loadFinished(bool)),
            this,   SLOT(labelSetText(taxt)));

    QWebPage * webPage = view->page();
}

void MainWindow::labelSetText(QString titleStr)
{
    ui->label_3->setText(titleStr);
}

mainwindow.h mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

namespace Ui {
class MainWindow;
}

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void labelSetText(QString titleStr);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

EDIT: This is the error I get 编辑:这是我得到的错误

E:\_Programming\C++\playAround\mainwindow.cpp:37: error: no matching function for call to 'MainWindow::connect(QWebView**, const char*, MainWindow* const, const char*)'
             this,   SLOT(labelSetText(taxt)));
                                             ^

That's not how connections work. 这不是连接的工作方式。 A signal-slot connection can only pass data from the signal into the slot. 信号槽连接只能将信号中的数据传递到插槽中。 It can't pass arbitrary variables like you do. 它不能像你那样传递任意变量。 The only way you could write your connect statement is as follows (the this argument is unnecessary): 你可以编写connect语句的唯一方法如下( this参数是不必要的):

connect(view, SIGNAL(loadFinished(bool)), SLOT(labelSetText(QString)));

This of course doesn't work, because the signal and slot are incompatible. 这当然不起作用,因为信号和插槽是不兼容的。 You of course don't need the intermediate slot, since a label already has the slot you want, but it doesn't help: 您当然不需要中间插槽,因为标签已经有您想要的插槽,但它没有帮助:

connect(view, SIGNAL(loadFinished(bool)), ui->label_3, SLOT(setText(QString)));

Note that you should not have connect(&view, ... since view is already a pointer-to-QObject. 请注意,您不应该connect(&view, ...因为view已经是指向QObject的指针。

To do it, you need to leverage C++11: 要做到这一点,你需要利用C ++ 11:

connect(view, &QWebView::loadFinished, [=,this](){
  this->ui->label_3->setText(taxt);
});

The lambda syntax translates into a functor class instance with copies of taxt and this as members. lambda语法转换为带有taxt副本的taxt函数类实例,并将this作为成员。 The compiler essentially creates the following, on the fly: 编译器实际上是在运行中创建以下内容:

class Functor_1 {
  MainWindow * _this;
  QString taxt;
public:
  MyFunctor_1(MainWindow * a1, const QString & a2) : _this(a1), taxt(a2) {}
  void operator() {
    _this->ui->label_3->setText(taxt);
  }
}

...
connect(view, &QWebView::loadFinished, Functor_1(this, taxt));

Of course this means that if you want to use Qt 4 signals and slots, you need to add the taxt member to your MainWindow class, and create a slot to do what the functor does. 当然这意味着如果你想使用Qt 4信号和插槽,你需要将taxt成员添加到你的MainWindow类,并创建一个插槽来执行functor所做的事情。 So, for Qt 4: 所以,对于Qt 4:

class MainWindow : public QMainWindow {
  Q_OBJECT
  QString m_taxt;
  Q_SLOT void loadFinished() {
    ui->label_3->setText(m_taxt);
  }
  ...
  Q_SLOT void on_pushButton_clicked() {
    QString webAdress = ui->lineEdit->text();

    QWebView *view = ui->webView;
    view->load(QUrl(webAdress));
    m_taxt = view->title();

    connect(view, SIGNAL(loadFinished(bool)), SLOT(loadFinished());
    ...
  }
};

Note that you shouldn't be connecting repeatedly. 请注意,您不应重复连接。 For Qt 4 style connection, move the connect to MainWindow's constructor. 对于Qt 4样式连接,将连接移动到MainWindow的构造函数。 For Qt 5 style connection, you need to break the connection once it fires. 对于Qt 5样式连接,您需要在触发后断开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM