0
votes

Im a complete c++ noob. I'm trying to setup a Redis connection and be able to set keys into redis using a c++ method. The c++ code is long so Im only including the relevant part : webpage.h

#ifndef WEBPAGE_H
#define WEBPAGE_H

#include 
#include 
#include 
#include 

#include "cookiejar.h"
#include "redispp.h"

class Config;
class CustomPage;
class WebpageCallbacks;
class NetworkAccessManager;
class QWebInspector;

class WebPage : public QObject, public QWebFrame::PrintCallback
{
    Q_OBJECT
    Q_PROPERTY(QString title READ title)
    Q_PROPERTY(QString frameTitle READ frameTitle)
    Q_PROPERTY(QString content READ content WRITE setContent)
    Q_PROPERTY(QString frameContent READ frameContent WRITE setFrameContent)
    Q_PROPERTY(QString url READ url)
    Q_PROPERTY(QString frameUrl READ frameUrl)
    Q_PROPERTY(bool loading READ loading)
    Q_PROPERTY(int loadingProgress READ loadingProgress)
    Q_PROPERTY(bool canGoBack READ canGoBack)
    Q_PROPERTY(bool canGoForward READ canGoForward)
    Q_PROPERTY(QString plainText READ plainText)
    Q_PROPERTY(QString framePlainText READ framePlainText)
    Q_PROPERTY(QString libraryPath READ libraryPath WRITE setLibraryPath)
    Q_PROPERTY(QString offlineStoragePath READ offlineStoragePath)
    Q_PROPERTY(int offlineStorageQuota READ offlineStorageQuota)
    Q_PROPERTY(QVariantMap viewportSize READ viewportSize WRITE setViewportSize)
    Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
    Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
    Q_PROPERTY(QVariantMap scrollPosition READ scrollPosition WRITE setScrollPosition)
    Q_PROPERTY(bool navigationLocked READ navigationLocked WRITE setNavigationLocked)
    Q_PROPERTY(QVariantMap customHeaders READ customHeaders WRITE setCustomHeaders)
    Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
    Q_PROPERTY(QVariantList cookies READ cookies WRITE setCookies)
    Q_PROPERTY(QString windowName READ windowName)
    Q_PROPERTY(QObjectList pages READ pages)
    Q_PROPERTY(QStringList pagesWindowName READ pagesWindowName)
    Q_PROPERTY(bool ownsPages READ ownsPages WRITE setOwnsPages)
    Q_PROPERTY(QStringList framesName READ framesName)
    Q_PROPERTY(QString frameName READ frameName)
    Q_PROPERTY(int framesCount READ framesCount)
    Q_PROPERTY(QString focusedFrameName READ focusedFrameName)
    Q_PROPERTY(QObject* cookieJar READ cookieJar WRITE setCookieJarFromQObject)
    Q_PROPERTY(QStringList captureContent READ captureContent WRITE setCaptureContent)

public:
    WebPage(QObject* parent, const QUrl& baseUrl = QUrl());
    virtual ~WebPage();

    QWebFrame* mainFrame();

    QString content() const;
    QString frameContent() const;
    void setContent(const QString& content);
    void setFrameContent(const QString& content);

    QString title() const;
    QString frameTitle() const;

    QString url() const;
    QString frameUrl() const;

    bool loading() const;
    int loadingProgress() const;

    QString plainText() const;
    QString framePlainText() const;

    QString libraryPath() const;
    void setLibraryPath(const QString& dirPath);

    QString offlineStoragePath() const;

    int offlineStorageQuota() const;

    void setViewportSize(const QVariantMap& size);
    QVariantMap viewportSize() const;

    void setClipRect(const QVariantMap& size);
    QVariantMap clipRect() const;

    void setScrollPosition(const QVariantMap& size);
    QVariantMap scrollPosition() const;

    void setPaperSize(const QVariantMap& size);
    QVariantMap paperSize() const;

    void setNavigationLocked(bool lock);
    bool navigationLocked();

    void setCustomHeaders(const QVariantMap& headers);
    QVariantMap customHeaders() const;
}

webpage.cpp

# include "webpage.h"
# include "networkaccessmanager.h"
# include "utils.h"
# include "config.h"
# include "consts.h"
# include "callback.h"
# include "cookiejar.h"
# include "system.h"
# include "redispp.h"
# ifdef Q_OS_WIN32
# define STDOUT_FILENAME "/dev/stdout"
# define STDERR_FILENAME "/dev/stderr"


/**
 * @class CustomPage
 */
class CustomPage: public QWebPage {
    Q_OBJECT


void WebPage::connectRedis()
{
    this->redis = redispp::Connection conn("127.0.0.1", "6379", "password", false);
}

 void WebPage::redisSet(const QString& key,const QString& value)
{
this->redis.set(key, value);
}





int WebPage::offlineStorageQuota() const
{
    return m_customWebPage->settings()->offlineStorageDefaultQuota();
}

void WebPage::showInspector(const int port)
{
    m_customWebPage->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    m_inspector = new QWebInspector;
    m_inspector->setPage(m_customWebPage);

    if (port == -1) {
        m_inspector->setVisible(true);
    } else {
        m_customWebPage->setProperty("_q_webInspectorServerPort", port);
    }
}

void WebPage::applySettings(const QVariantMap& def)
{
    QWebSettings* opt = m_customWebPage->settings();

    opt->setAttribute(QWebSettings::AutoLoadImages, def[PAGE_SETTINGS_LOAD_IMAGES].toBool());
    opt->setAttribute(QWebSettings::JavascriptEnabled, def[PAGE_SETTINGS_JS_ENABLED].toBool());
    opt->setAttribute(QWebSettings::XSSAuditingEnabled, def[PAGE_SETTINGS_XSS_AUDITING].toBool());
    opt->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, def[PAGE_SETTINGS_LOCAL_ACCESS_REMOTE].toBool());
    opt->setAttribute(QWebSettings::WebSecurityEnabled, def[PAGE_SETTINGS_WEB_SECURITY_ENABLED].toBool());
    opt->setAttribute(QWebSettings::JavascriptCanOpenWindows, def[PAGE_SETTINGS_JS_CAN_OPEN_WINDOWS].toBool());
    opt->setAttribute(QWebSettings::JavascriptCanCloseWindows, def[PAGE_SETTINGS_JS_CAN_CLOSE_WINDOWS].toBool());

    if (def.contains(PAGE_SETTINGS_USER_AGENT)) {
        m_customWebPage->m_userAgent = def[PAGE_SETTINGS_USER_AGENT].toString();
    }

    if (def.contains(PAGE_SETTINGS_USERNAME)) {
        m_networkAccessManager->setUserName(def[PAGE_SETTINGS_USERNAME].toString());
    }

    if (def.contains(PAGE_SETTINGS_PASSWORD)) {
        m_networkAccessManager->setPassword(def[PAGE_SETTINGS_PASSWORD].toString());
    }

    if (def.contains(PAGE_SETTINGS_MAX_AUTH_ATTEMPTS)) {
        m_networkAccessManager->setMaxAuthAttempts(def[PAGE_SETTINGS_MAX_AUTH_ATTEMPTS].toInt());
    }

    if (def.contains(PAGE_SETTINGS_RESOURCE_TIMEOUT)) {
        m_networkAccessManager->setResourceTimeout(def[PAGE_SETTINGS_RESOURCE_TIMEOUT].toInt());
    }

}

QString WebPage::userAgent() const
{
    return m_customWebPage->m_userAgent;
}

void WebPage::setNavigationLocked(bool lock)
{
    m_navigationLocked = lock;
}

bool WebPage::navigationLocked()
{
    return m_navigationLocked;
}

void WebPage::setViewportSize(const QVariantMap& size)
{
    int w = size.value("width").toInt();
    int h = size.value("height").toInt();
    if (w > 0 && h > 0) {
        m_customWebPage->setViewportSize(QSize(w, h));
    }
}

QVariantMap WebPage::viewportSize() const
{
    QVariantMap result;
    QSize size = m_customWebPage->viewportSize();
    result["width"] = size.width();
    result["height"] = size.height();
    return result;
}



}

Compilation Errors


webpage.cpp:950:11: error: no member named 'redis' in 'WebPage'
    this->redis = redispp::Connection conn("127.0.0.1", "6379", "password", false);
    ~~~~  ^
webpage.cpp:950:39: error: expected '(' for function-style cast or type construction
    this->redis = redispp::Connection conn("127.0.0.1", "6379", "password", false);
                  ~~~~~~~~~~~~~~~~~~~ ^
webpage.cpp:955:7: error: no member named 'redis' in 'WebPage'
this->redis.set(key, value);
~~~~  ^

This c++ Redis library Im using is https://github.com/brianwatling/redispp

Note** *I tried using Redispp namespace such as in teh example but the connection class would conflict with other classes. https://github.com/brianwatling/redispp/blob/master/test/multi.cpp *

The whole webpage.cpp file is in the pastebin. The file goes over StackOverflow character limits http://pastebin.com/fUhRwy0R
Any help would be appreciated.

1
What's with all the empty #include statements?Colin Basnett
Stackoverflow wouldn't render it correctly so I removed them. The includes were irrelevant. The script is working fine, I just cant seem to globalize the connection.mbejda

1 Answers

0
votes

Your class is called CustomPage but you're implementing functions in a WebPage for some reason.

Change your function definitions to this:

void connectRedis()

And...

void redisSet(const QString & key, const QString & value)

Additionally, since it seems you might be coming from Java or C#, the standard way to do things in C++ is to separate class definition from implementations (into a .h/.hpp and .cpp file), see here for more information on this.