简体   繁体   中英

Qt C++ Custom Slot

I'm having trouble with making custom slots in Qt. Code:

class.h file:

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

main.cpp file:

#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:

#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:

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

However, your class is derived from QMainWindow :

class webview : public QMainWindow

Both the base class, and your derived class indeed do not have any load() method. You should derive your webview class from QWebView instead of QMainWindow . In this case, the base class' load() method will be called, and it will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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