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
Forum Updated to NodeBB v4.3 + New Features

Porting CString formatted value to QString issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 659 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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 26 May 2019, 16:17 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.

    C 1 Reply Last reply 26 May 2019, 16:34
    0
    • C Cobra91151
      26 May 2019, 16:17

      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.

      C Offline
      C Offline
      Cobra91151
      wrote on 26 May 2019, 16:34 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
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 26 May 2019, 16:40 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

        C 1 Reply Last reply 26 May 2019, 17:22
        3
        • C Christian Ehrlicher
          26 May 2019, 16:40

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

          C Offline
          C Offline
          Cobra91151
          wrote on 26 May 2019, 17:22 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
          • C Offline
            C Offline
            Cobra91151
            wrote on 26 May 2019, 17:44 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

            1/5

            26 May 2019, 16:17

            • Login

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