简体   繁体   English

Qt C ++自定义插槽

[英]Qt C++ Custom Slot

I'm having trouble with making custom slots in Qt. 我在Qt中制作自定义插槽时遇到了麻烦。 Code: 码:

class.h file: class.h文件:

public slots:
    void resetUrl(){
        this->load(QUrl("http://www.google.com"));
}

main.cpp file: main.cpp文件:

#include <QWebView>
#include <QPushButton>

QWebView *web = new QWebView(mainwindow);
QPushButton *button = new QPushButton(mainwindow);

web->load(QUrl("http://www.yahoo.com"));
button->setText("Google");

QObject::connect(button, SIGNAL(clicked()), web, SLOT(resetUrl()));

It gives me a message saying load is not a recognized member. 它给我一条消息,说负载不是公认的成员。 What do I need to change? 我需要更改什么?

Edit: Heres the full webview.h file: 编辑:这是完整的webview.h文件:

#ifndef WEBVIEW_H
#define WEBVIEW_H

#include <QMainWindow>
#include <QWebView>


namespace Ui {
class webview;
}

class webview : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void resetUrl(){
        this->load(QUrl("http://www.google.com"));
    }

private:
    Ui::webview *ui;
};

#endif // WEBVIEW_H

You are trying to call a load() method of your webview class here: 您正在尝试在此处调用webview类的load()方法:

void resetUrl(){
    this->load(QUrl("http://www.google.com"));
}

However, your class is derived from QMainWindow : 但是,您的类是从QMainWindow派生的:

class webview : public QMainWindow

Both the base class, and your derived class indeed do not have any load() method. 基类和派生类实际上都没有任何load()方法。 You should derive your webview class from QWebView instead of QMainWindow . 您应该从QWebView而不是QMainWindow派生webview类。 In this case, the base class' load() method will be called, and it will work fine. 在这种情况下,将调用基类的load()方法,并且可以正常工作。

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

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