문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top