Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Print character
QtWS25 Last Chance

Print character

Scheduled Pinned Locked Moved Qt Creator and other tools
5 Posts 3 Posters 307 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.
  • N Offline
    N Offline
    Nauvisse
    wrote on 12 Aug 2024, 08:27 last edited by
    #1

    Hi,

    I've already tried to print character such as beta or gamma and it doesn't work. I work on Qt Creator (Windows 7, 64 bits) and I've tried to change "Encodage par défaut" UTF-8, but it should not be here that I have to change choice.

    I also want to cast wchar_t to char without doing something.

    I had given up.

    Do you know how I can do please ?
    Encodage.jpg

    J 1 Reply Last reply 12 Aug 2024, 08:47
    0
    • N Nauvisse
      12 Aug 2024, 08:27

      Hi,

      I've already tried to print character such as beta or gamma and it doesn't work. I work on Qt Creator (Windows 7, 64 bits) and I've tried to change "Encodage par défaut" UTF-8, but it should not be here that I have to change choice.

      I also want to cast wchar_t to char without doing something.

      I had given up.

      Do you know how I can do please ?
      Encodage.jpg

      J Offline
      J Offline
      JonB
      wrote on 12 Aug 2024, 08:47 last edited by JonB 8 Dec 2024, 08:47
      #2

      @Nauvisse
      Are you talking about "printing" a character from your Qt program or entering it into Creator text editor?

      I also want to cast wchar_t to char without doing something.

      Again, are you talking about at editing time (in which case I don't know what wchar_t or casting has to do with it) or in your code?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nauvisse
        wrote on 14 Aug 2024, 08:14 last edited by Nauvisse
        #3

        Thank you for your reply. I had assumed I will get an email telling me a reply was sent on this forum. Sorry for my delay.

        Printing character

        Printing on the stdout, for example:

        printf("\beta");
        

        wchar_t

        Here is my code. I was said on this forum https://learn.microsoft.com/en-us/answers/questions/1856800/incorrect-file-creation-date?page=1&orderby=Helpful&comment=answer-1644021#newest-answer-comment (clik comments to see the response) that "the project will build as unicode or non-unicode based on the project property settings"

        void SetFileToCurrentTime(const char* pathDirFile)
        {
          HANDLE        hFile;
          FILETIME      ft;
          SYSTEMTIME    st;
          size_t        origsize = strlen(pathDirFile) + 1;
          const size_t  newsize  = mbstowcs(NULL,pathDirFile,origsize) + 1;
          wchar_t       wPathDirFile[newsize];
        
          mbstowcs(wPathDirFile,pathDirFile,origsize);
          hFile = CreateFile(wPathDirFile,FILE_WRITE_ATTRIBUTES,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
          if (hFile          == INVALID_HANDLE_VALUE) {printf("Cannot create times of the file, error code is %lud\n",GetLastError());   return;}
          if (GetLastError() == ERROR_FILE_NOT_FOUND) {printf("Cannot create times of the file because ERROR_FILE_NOT_FOUND.\n");        return;}
          GetSystemTime(&st);
          if (!SystemTimeToFileTime(&st,&ft))         {printf("Cannot create times of the file because SystemTimeToFileTime failed.\n"); return;}
          if (!SetFileTime(hFile,&ft,&ft,&ft))        {printf("Cannot create times of the file because SetFileTime failed.\n");          return;}
          if (!CloseHandle(hFile))                    {printf("Cannot create times of the file because CloseHandle failed.\n");          return;}
        }
        

        Thank you

        1 Reply Last reply
        0
        • N Nauvisse marked this topic as a regular topic on 14 Aug 2024, 08:21
        • J Offline
          J Offline
          JonB
          wrote on 14 Aug 2024, 08:43 last edited by JonB
          #4

          Your code is 100% Windows-specific and has nothing Qt in it. I don't know why you would try to use Qt with it.

          Your screenshot shows settings for the Text Editor inside Qt Creator. Settings there (including encoding or UTF-8 or whatever) are for when you enter source code text into the editor. That has no relationship to wchar_t or similar which you use at runtime in your code. You ask about printf("\beta");. But you don't use anything like that in your SetFileToCurrentTime() implementation so I have no idea what the connection is. Incidentally "\beta" is incorrect as a C++ literal string in source code, so that's not great. You say "I've already tried to print character such as beta or gamma and it doesn't work. ", but we don't know what "doesn't work" means. If you literally have printf("\beta"); then (a) that might be the reason for your "doesn't work" and (b) even if you correct to good C++ printf() is not going to output some "beta" character from that.

          Whatever your problem is about "printing characters" you do not say where you run your program, e.g. inside Creator versus outside, whether you use Creator's inbuilt terminal when you run it, etc.

          All in all I don't know what your question or problem is, what it has to do with Qt/Creator, or why you are even using Qt/Creator.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SimonSchroeder
            wrote on 16 Aug 2024, 07:28 last edited by
            #5

            We call setlocale(LC_ALL, ".UTF8"); at the beginning in main(). This says that every Windows function treats char* as UTF-8 strings. There is a compiler switch that tells if functions like CreateFile get replaced by either CreateFileA or CreateFileW. If you choose the A variant with this trick you can now provide UTF-8 file paths. Then you don't need any conversions anymore. Another good thing is that the code is then portable between Windows and all the other platforms (which already all use UTF-8).

            If you want to show Unicode characters on the console (e.g. by using printf) you need to make sure that the correct code page is set. Either your standard code page already supports Greek letters or you might want to switch to the UTF-8 code page. However, there is no way in C++ that you can write \beta or \gamma. You have to provide the character itself inside a u8"..." string or use Unicode escape codes.

            PS: This gives me the idea that we could actually try to overload the string operator to substitute \beta with the correct UTF-8 character. Has anyone ever tried this? Something like "\beta"_latex...

            1 Reply Last reply
            0

            2/5

            12 Aug 2024, 08:47

            • Login

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