Can't convert const *char to QChar32

Post Reply
opaque
Posts: 19
Joined: Sat Mar 28 2020 11:53 am

Can't convert const *char to QChar32

Post by opaque »

It's a g++ error message that I vaguely recall. I put it here because of plain curiosity of how to proper adapt it to Cs. I think I will revisit it in the future but now I'm about to cancel my plan to adapt Lazarus' Qt5 binding to Cs as I think I have more to learn about Object Pascal.

The original code is here: https://github.com/graemeg/lazarus/blob/upstream/lcl/interfaces/qt5/cbindings/src/pascalbind.h

The problematic code:

Code: Select all

inline void copyQStringToPWideString(const QString &qs, PWideString ps)
{
if (qs!=0 && ps) copyUnicodeToPWideString(qs.unicode(), ps, qs.length());
}


inline void copyPWideStringToQString(PWideString ps, QString &qs)
{
  qs.setUtf16((ushort *)unicodeOfPWideString(ps),
    lengthOfPWideString(ps));
}
I translated these line according to the migration guide (https://www.copperspice.com/docs/cs_api_1.6/class_qstring8.html#migration-qstring) into:

Code: Select all

inline void copyQStringToPWideString(const QString &qs, PWideString ps)
{
if (qs!=0 && ps) copyUnicodeToPWideString(qs.constData(), ps, qs.length());
}


inline void copyPWideStringToQString(PWideString ps, QString &qs)
{
  qs.fromUtf16((ushort *)unicodeOfPWideString(ps),
    lengthOfPWideString(ps));
}
And g++ give this error message.

seasoned_geek
Posts: 123
Joined: Thu Jun 11 2020 12:18 pm

Re: Can't convert const *char to QChar32

Post by seasoned_geek »

I know nothing about Object Pascal. What I can tell you about is C++, at least a bit.

Code: Select all

inline void copyPWideStringToQString(PWideString ps, QString &qs)
{
  qs.setUtf16((ushort *)unicodeOfPWideString(ps),
    lengthOfPWideString(ps));
}
The first thing you have to do is look at the doc.

https://www.copperspice.com/docs/cs_api/class_qstring8.html#add2174ee7d5d91fdcba0bc4ea4d63552

Code: Select all

QString8 QString8::fromUtf16 	( 	const char16_t *  	str,
		size_type  	numOfChars = -1 
	) 	
The method wants a const char16_t * but the previous code is doing a brutal cast to ushort *. You are attempting to cast away const-i-ness. In the C++ world the politically correct cast would be

static_cast<const char16_t *>(unicodeOfPWideString(ps))

If this is completely wrong, I'm sorry. I just saw you hadn't had a response in quite some time.

ansel
Posts: 116
Joined: Fri Apr 10 2015 8:23 am

Re: Can't convert const *char to QChar32

Post by ansel »

seasoned_geek wrote:
Wed Dec 16 2020 2:33 pm
If this is completely wrong, I'm sorry. I just saw you hadn't had a response in quite some time.
Your response is correct. Thanks for taking the time to post a reply.
Ansel Sermersheim
CopperSpice Cofounder

Post Reply