QPointer compilation error...



  • If I declare a QPointer in a header, like so:
    @
    private:
    QPointer<QWidgetDerived> tp;
    @
    and then in the constructor, I create an instance and try to assign it, like thus:
    @
    SomeOtherClass::function()
    {
    tp = new QWidgetDerived();
    }
    @
    I get the following compilation error:
    @
    passing 'const QPointer<QWidgetDerived>' as 'this' argument of
    'QPointer<T>& QPointer<T>::operator==(T*)
    [with T = QWidgetDerived]' discards qualifiers
    @
    Interestingly enough, if I create a local pointer and assign it, like so:
    @
    SomeOtherClass::function()
    {
    QPointer<QWidgetDerived> op = new QWidgetDerived();
    }
    @
    it works fine, but if I then try to assign that to the member, like so:
    @
    SomeOtherClass::function()
    {
    QPointer<QWidgetDerived> op = new QWidgetDerived();
    tp = op;
    }
    @
    I get the same error.

    If I try:
    @
    SomeOtherClass::function()
    {
    QWidgetDerived *op = new QWidgetDerived();
    tp = op;
    }
    @
    I still get the same error.

    Can someone tell me what's going on? The docs indicate that QPointer is virtually interchangeable with c++ pointers, but I keep running into this compilation issue when I declare a QPointer in a header, and it's driving me crazy. I'm not declaring anything with 'const' qualifiers, but the compiler seems to think a 'const' is specified or implied somewhere....

    This is on Mac with the gnu compiler, BTW.

    Any thoughts appreciated,
    rickb



  • On the first sight, it seems ok. A short test with some of my classes works (also on a Mac). Can you please create a small yet complete test case (including all relevant source files) that demonstrates the error.



  • Unh.... I think I see the problem...

    The calling routine is declared like this:

    @
    WidgetDerived *OtherClass::gimmeAWidget(const QWidget *parent) const
    {
    }
    @

    The trailing 'const' spec in the call is what's messing me up, I think... I don't have any control over that...

    Boogers.

    [EDIT: code formatting, Volker]



  • Yes, that's the show stopper here. You can do some dirty hack to cast a away the constness:

    @
    WidgetDerived *OtherClass::gimmeAWidget(const QWidget *parent) const
    {
    OtherClass *that = const_cast<OtherClass *>(this);
    that->tp = new QWidgetDerived();
    return tp;
    }
    @

    Another option would be to declare the pointer mutable:

    @
    private:
    mutable QPointer<QWidgetDerived> tp;
    @



  • Both interesting workarounds. The 'const' construct in all its flavors drives me crazy...

    Grumble.

    Anyway, the way I got around it was to define a couple of more signals and slots and connect them up to the created object, rather than rely on keeping a pointer around. That satisfies the issue of not accessing something if it's deleted (which was the intent of using QPointer in the first place), and 'connect' does not appear to violate the 'const' qualification.

    Thanks for the pointers (ar, ar). Keep them in mind for future necessities.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.