c++qtclassslot

Qt4 using a Class to create a SLOT to control a QWebView's setTextSizeMultiplier using CONNECT with a QSLIDER


Using Qt4, how do I create a Class to create a SLOT to control a QWebView's setTextSizeMultiplier using CONNECT with a QSLIDER.

My code: (thesliderbar is QSlider and vweb is QWebView)

class webextras
{
    Q_OBJECT::Ui_ywr *pui;

public slots:
    void wtresize(int wtr)
    {
        pui->vweb->setTextSizeMultiplier(wtr);
    }
};

connect(thesliderbar,SIGNAL(valueChanged(int)),webextras,SLOT(wtresize(int)));

I'm getting errors for the connect();.

Errors:

ywr.cpp:31: error: expected primary-expression before ‘,’ token

ywr.cpp:-1: In constructor ‘ywr::ywr(QWidget*)’:


Solution

  • First in order your class to support signals/slots it must inherit from QObject.

    Second you should use the macro Q_OBJECT which is needed from the moc tool:

    The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.

    The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

    So your class should be:

    class webextras : public QObject
    {
        Q_OBJECT;
    
    public slots:
        void wtresize(int wtr)
        {
            pui->vweb->setTextSizeMultiplier(wtr);
        }
    };
    

    What is the Ui_ywr *pui? Q_OBJECT is a macro, you cannot declare it like the way you did.