Re: [Interest] Migrating QWeakPointer to Qt5

2016-05-06 Thread Nye
On Sat, May 7, 2016 at 12:54 AM, Thiago Macieira 
wrote:

>
> > Yes, by actually using a QSharedPointer for the constructor (as
> > QWeakPointer is a weak reference to a shared pointer).
>
> That's not the same thing. For a while, we had deprecated QPointer in
> favour
> of QWeakPointer for tracking QObjects you did not own. But we came to our
> senses before Qt 5.0 was launched and un-deprecated QPointer for that
> functionality.
>

That's what I meant indeed, I suppose I should've been more descriptive. I
think QPointer is actually to be used here, as Bo Thorsen had noted.
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Migrating QWeakPointer to Qt5

2016-05-06 Thread Thiago Macieira
On sexta-feira, 6 de maio de 2016 12:14:14 PDT Nye wrote:
> On Fri, May 6, 2016 at 8:35 AM, Tom Isaacson 
> 
> wrote:
> > I'm moving some Qt4.8.2 code which uses QWeakPointer to Qt5.6.
> > Simplifying a lot it looks like this:
> > [snippet ..]
> > but when I compile under Visual Studio 2013 I get this error:
> > error C2664: 'QWeakPointer::QWeakPointer(const QSharedPointer
> > &)' : cannot convert argument 1 from 'MySettings *' to 'const
> > QWeakPointer &'
> 
> Is there a recommended method for migrating the use of QWeakPointer into
> 
> > Qt5?
> 
> Yes, by actually using a QSharedPointer for the constructor (as
> QWeakPointer is a weak reference to a shared pointer).

That's not the same thing. For a while, we had deprecated QPointer in favour 
of QWeakPointer for tracking QObjects you did not own. But we came to our 
senses before Qt 5.0 was launched and un-deprecated QPointer for that 
functionality.

QWeakPointer in Qt 5 is as you said: a weak reference to a shared pointer.

> > I tried changing the QWeakPointer to a QPointer but now I'm getting:
> > error C2440: 'static_cast' : cannot convert from 'QObject *' to MyClass *'
> > (GeneratedFiles\Debug\moc_MyCode.cpp)

Looks like a missing #include.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Migrating QWeakPointer to Qt5

2016-05-06 Thread Nye
>
> Well you can't have implicit upcasting ...
>

That's a typo, I meant downcasting.

>
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Migrating QWeakPointer to Qt5

2016-05-06 Thread Nye
On Fri, May 6, 2016 at 8:35 AM, Tom Isaacson 
wrote:

> I'm moving some Qt4.8.2 code which uses QWeakPointer to Qt5.6.
> Simplifying a lot it looks like this:
> [snippet ..]
> but when I compile under Visual Studio 2013 I get this error:
> error C2664: 'QWeakPointer::QWeakPointer(const QSharedPointer
> &)' : cannot convert argument 1 from 'MySettings *' to 'const
> QWeakPointer &'

Is there a recommended method for migrating the use of QWeakPointer into
> Qt5?
>

Yes, by actually using a QSharedPointer for the constructor (as
QWeakPointer is a weak reference to a shared pointer).


> I tried changing the QWeakPointer to a QPointer but now I'm getting:
> error C2440: 'static_cast' : cannot convert from 'QObject *' to MyClass *'
> (GeneratedFiles\Debug\moc_MyCode.cpp)
>

Well you can't have implicit upcasting, so you should manually qobject_cast
to the derivied class, but this error doesn't really correspond to your
snippet. The pointer you have is referencing MySettings, not MyClass.

Kind regards,
Konstantin.
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] Migrating QWeakPointer to Qt5

2016-05-06 Thread Bo Thorsen

Den 06-05-2016 kl. 07:35 skrev Tom Isaacson:

I'm moving some Qt4.8.2 code which uses QWeakPointer to Qt5.6.  Simplifying a 
lot it looks like this:

class MySettings : public QObject
{
Q_OBJECT
}

class MyClass : public QObject
{
Q_OBJECT

private:
QWeakPointer m_xSettings;
}

MyClass::MyClass(MySettings* pSettings, QObject* pParent)
: QObject(pParent)
, m_xSettings(pSettings)
{
}

but when I compile under Visual Studio 2013 I get this error:
error C2664: 'QWeakPointer::QWeakPointer(const QSharedPointer &)' : cannot 
convert argument 1 from 'MySettings *' to 'const QWeakPointer &'

I tried changing the QWeakPointer to a QPointer but now I'm getting:
error C2440: 'static_cast' : cannot convert from 'QObject *' to MyClass *' 
(GeneratedFiles\Debug\moc_MyCode.cpp)

Is there a recommended method for migrating the use of QWeakPointer into Qt5?


With this code, you need QPointer.

Bo Thorsen,
Director, Viking Software.

--
Viking Software
Qt and C++ developers for hire
http://www.vikingsoft.eu
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest