0

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
Connor M
  • 331
  • 3
  • 5
  • 18
  • 1
    That doesn't look right. What class is your slot defined in? – Mat Aug 23 '13 at 04:58
  • if this is all your code then your cpp file is missing method definitions and if thats your entire .h file you dont have a class at all – mdoran3844 Aug 23 '13 at 04:59
  • Thats not my entire code, I can paste the rest if you need it – Connor M Aug 23 '13 at 05:02
  • Can you post the exact error message please& – SingerOfTheFall Aug 23 '13 at 05:07
  • Exact error message: 'class webview' has no member named 'load' – Connor M Aug 23 '13 at 05:08
  • It doesn't have a member named `load`. Look at that class. Do you see a definition for `load` anywhere? Furthermore, [`QMainWindow` has no `load` member either](http://qt-project.org/doc/qt-5.0/qtwidgets/qmainwindow-members.html). Perhaps you're trying to use a `QWebView`? It has a `load` member, at least. But you're not using a `QWebView` anywhere (despite including its header). – Cornstalks Aug 23 '13 at 05:14

1 Answers1

0

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • When I changed QMainWindow to QWebView in that spot, I got a couple new errors: 'QMainWindow' is not a direct base of 'webview' and no matching function for call to 'Ui::webview::setupUi(webview* const) – Connor M Aug 23 '13 at 05:18
  • @COnnorM, you also need to change the constructor (to call the constructor of a proper base class). Also get rid of the `ui`, you will not need it (and I'm not even sure the `QWebView` has it). – SingerOfTheFall Aug 23 '13 at 05:23