Link to home
Start Free TrialLog in
Avatar of Ingo Foerster
Ingo Foerster

asked on

QString to TCHAR in QT

Hello,
how I can copy a QString to a TCHAR value of a componet (Dll)?
In VC++ I used:
_tcsncpy(info.DeveloperID, m_strDeveloperID, 24*sizeof(TCHAR));
//info.DeveloperID = TCHAR

Open in new window


How it look in QT if m_strDeveloperID is a QString?
Avatar of jkr
jkr
Flag of Germany image

This is quite similar to your 'TCHAT' issue, with the problem being that QString has 'std::string       toStdString() const' and 'std::wstring       toStdWString() const' as its members for this task. So, you either you need to provide cenvenience wrappers similar to 'tchat.h' or compile it conditionally like

#ifdef _UNICODE
_tcsncpy(info.DeveloperID, m_strDeveloperID.toStdWString().c_str(), 24*sizeof(TCHAR));
#else
_tcsncpy(info.DeveloperID, m_strDeveloperID.toStdString().c_str(), 24*sizeof(TCHAR));
#endif

Open in new window

Ooops, alsmost forgot - the convenience wrapper could just be

TCHAR* QStringToTCHAR(const QString& qs) {
#ifdef _UNICODE
  return qs.toStdWString().c_str();
#else
  return qs.toStdString().c_str();
#endif
}

// ...

_tcsncpy(info.DeveloperID, QStringToTCHAR(m_strDeveloperID), 24*sizeof(TCHAR));

Open in new window

And, last but not least: see the docs at http://doc.qt.io/qt-5/qstring.html
Avatar of Ingo Foerster
Ingo Foerster

ASKER

so _tcsncpy is allowed in QT and works on all platforms like Linux, Mac and Windows?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great and high professional help. Many thanks to jkr.