QVariant bug/documentation issue

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

QVariant bug/documentation issue

Post by seasoned_geek »

All,

The QVariant documentation has an enum to indicate a QFont is contained.
https://www.copperspice.com/docs/cs_api/class_qvariant.html

What it doesn't have is a QVariant( QFont) or toFont().

If we have to serialize QFont ourselves via toString()
https://www.copperspice.com/docs/cs_api/class_qfont.html#a5ab046d742a8538c2e2e50c2e03733ea

How can QVariant possibly know it has a font when it contains a string?

If we have to "just know" a string is a font, what good is the enum?

The doc seems to be ahead of the actual functionality.
ansel
Posts: 152
Joined: Fri Apr 10 2015 8:23 am

Re: QVariant bug/documentation issue

Post by ansel »

There are around 50 different constructors in QVariant to support the most common data types. To create a QVariant using a type which was not provided, such as QFont, you need to use the QVariant::fromValue or QVariant::setValue template methods.

The value can then be retrieved out of the variant using QVariant::value(), QVariant::getData(), or QVariant::getDataOr().

Code: Select all

// construct QFont
QFont font_in = QFont("Times", 10, QFont::Bold);                   

// construct QVariant
QVariant myVariant = QVariant::fromValue(font_in);

// retrieve value from QVariant
QFont font_out = myVariant.value<QFont>();
Ansel Sermersheim
CopperSpice Cofounder
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

Re: QVariant bug/documentation issue

Post by seasoned_geek »

Ansel,

Thank you for the reply, but you completely missed what was being reported.

QVariant has an enum to identify it contains a QFont.

What it doesn't have is a QVariant( QFont) and QVariant::toQFont()

Yes, I can wedge a string in there, and have done it. What was being reported is that the documentation for the enum indicates QVariant supported types that don't also have documented in/to methods.

Given what is in the documentation the enum is out of sync with the class definition.
ansel
Posts: 152
Joined: Fri Apr 10 2015 8:23 am

Re: QVariant bug/documentation issue

Post by ansel »

Classes such as QColor, QBrush, and QFont are part of CsGui. We can not declare a constructor which accepts a QFont or a toFont() method since QVariant is in the CsCore library. This is why we provided template methods such as fromValue() and value().
Ansel Sermersheim
CopperSpice Cofounder
seasoned_geek
Posts: 253
Joined: Thu Jun 11 2020 12:18 pm

Re: QVariant bug/documentation issue

Post by seasoned_geek »

Ansel,

Thank you for your reply.

Once again you have completely missed the point.

QVariant has an enum to identify it contains a QFont.

You (Copperspice) have enumerated support for a data type QVariant does not in fact support. There are no toQFont() or fromQFont() methods. The reason for them not being there doesn't matter. What matters is the class itself has en enum stating they should be.
barbara
Posts: 446
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QVariant bug/documentation issue

Post by barbara »

The CopperSpice team has not missed your question and we will try to explain in more detail.

Constructing a QVariant which contains a QFont can be done, however you must use the code we provided earlier in this thread. This is the QVariant::fromValue<T>() and QVariant::value<T>(). This is actually the preferred way to construct or access a QVariant. The overloaded QVariant non-template constructors may require an implicit conversion.

The enum value for QFont must be present since there needs to be a test to determine if a QVariant containing a QFont is convertible to a QString. Having the enum value present does not imply a non-template constructor will exist for that type.

The precise reason it is not possible to add a QVariant constructor for QFont (or QColor or QBrush) as you are requesting, is that there is no way to link the CsCore library with the CsGui library. This would be a circular dependency. If we added such a constructor the CopperSpice build would fail.
Post Reply