Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Porting CString formatted value to QString issue
Qt 6.11 is out! See what's new in the release blog

Porting CString formatted value to QString issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to port CString formatted value to QString:

    DWORD dwLangCode = 0;
    CString strSubBlock = "";
    strSubBlock.Format(_T("\\StringFileInfo\\%04X%04X\\"), dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);
    
    //QString example
    QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFFF).arg((dwLangCode & 0xFFFF0000) >> 16);
    

    I check the result with this code:

    if (VerQueryValue(lpData, (LPTSTR)(LPCTSTR)(strSubBlock.append("CompanyName").toStdWString().c_str()), &lpInfo, &unInfoLen)) {
       std::string companyName = std::wstring(reinterpret_cast<LPCTSTR>(lpInfo));
        qDebug() << QString::fromStdWString(companyName.c_str()); //returns ""
    }
    

    So it returns the empty data, but when using CString it returns the actual data. Any ideas what is wrong? Thanks.

    After come checking I think my QString formatting is wrong. I get 10331200 value, but from CString I get 040904B0 value.

    Cobra91151C 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      Hi! I want to port CString formatted value to QString:

      DWORD dwLangCode = 0;
      CString strSubBlock = "";
      strSubBlock.Format(_T("\\StringFileInfo\\%04X%04X\\"), dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);
      
      //QString example
      QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFFF).arg((dwLangCode & 0xFFFF0000) >> 16);
      

      I check the result with this code:

      if (VerQueryValue(lpData, (LPTSTR)(LPCTSTR)(strSubBlock.append("CompanyName").toStdWString().c_str()), &lpInfo, &unInfoLen)) {
         std::string companyName = std::wstring(reinterpret_cast<LPCTSTR>(lpInfo));
          qDebug() << QString::fromStdWString(companyName.c_str()); //returns ""
      }
      

      So it returns the empty data, but when using CString it returns the actual data. Any ideas what is wrong? Thanks.

      After come checking I think my QString formatting is wrong. I get 10331200 value, but from CString I get 040904B0 value.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by
      #2

      So, one option is to use: QString strSubBlock = QString().sprintf("\\StringFileInfo\\%04X%04X\\", dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);

      But Qt docs does not recommend to use it in the new code:
      Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream.

      So how to make it work with .arg() then? Thanks.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        If you want exactly 4 digits you should take a look in the docs - fieldWidth and fillChar are your friends.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Cobra91151C 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          If you want exactly 4 digits you should take a look in the docs - fieldWidth and fillChar are your friends.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #4

          @Christian-Ehrlicher

          Hi! I have tried: QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFF, 4, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, QChar('0')); but it returns: \\StringFileInfo\\000\t000°\\
          Can you provide an example how to properly use it?

          Ok. I fixed it like this: QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFF, 4, 16, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, 16, QChar('0'));

          1 Reply Last reply
          0
          • Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            By the way, to make it uppercase:

            Code:

            QString strHex = QString("%1%2").arg(dwLangCode & 0x0000FFF, 4, 16, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, 16, QChar('0')).toUpper();
            QString strSubBlock = QString("\\StringFileInfo\\%1\\").arg(strHex);
            

            And now it displays value uppercase: 040904B0.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved