How to use CreateFont API function in Qt5



  • Hi. Beginner's question. I try to port my application to Qt. I need call to Windows' CreateFont function. Header files are ok, compiler is happy. But linker complains : "undefined reference to '_imp__CreateFontW". Code is not changed and used to work.
    I found info that I need "" so I added @win32:LIBS += c:/Windows/System32/Kernel32
    C:/Windows/System32/Gdi32@
    Same error. Any idea what else I can try?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It would rather be LIBS += -lGdi32.

    Out of curiosity, why not use Qt's QFont class ?

    [edit: Fixed casing SGaist]



  • The program was written using APIs originally. I like to compile and link it before I start to make changes.

    I have tried your solution. Same error.


  • Qt Champions 2016

    @Uncle-Kris
    Do you have the windows SDKs installed? Did you make sure that the -L switch points to the correct path containing the import library files (.lib) and that you have -lgdi32 in your pro (that's a typo on part of @SGaist as the library is specified with a lowercase letter)?



  • I see your point. No, SDK is not installed. I used to work in BCB 4 and all basic libraries were part of installation. I will look for them now.



  • I have installed SDK, updated path in .pro file. Same error.


  • Qt Champions 2016

    @Uncle-Kris
    Okay, what libraries does the SDK provide (for which compilers), do they match the compiler you're using? Can we see the line from the project where you set the link flags (i.e. LIBS += ...)?



  • The line is:
    LIBS += -L"H:\Windows Kits\8.1\Lib\winv6.3\um\x86/gdi32"\

    There is no info on compiler dependency. Just folder full of libraries. How I can check?


  • Qt Champions 2016

    @Uncle-Kris
    LIBS += -L"H:\Windows Kits\8.1\Lib\winv6.3\um\x86/gdi32"\

    This is incomplete. The backslash at the end means it continues on to the next line. Also, when giving paths, use the forward slash instead of the a backwards. It is recognized on windows and you'll work around a ton of problems with escaping in the long run.

    There is no info on compiler dependency. Just folder full of libraries. How I can check?

    It should be noted somewhere on the MS's site, however I have no windows on hand to check. What compiler are you using currently? The 8.1 SDK should work with MSVC 2012/2013 if I recall correctly.


  • Lifetime Qt Champion

    @kshegunov Not a typo ;) See here


  • Qt Champions 2016

    @SGaist said:

    It would rather be LIBS += -LGdi32.

    I know, I know. Still looks like you typed -L instead of -l ;)


  • Lifetime Qt Champion

    Haaaaaaa that typo ! Good catch ! Then indeed, there was ! :)



  • @kshegunov There was a next line. I have removed it for test time. Also changes to shash/backslash do not make any difference for now. Linker is not happy.



  • @SGaist What typo? Can you be more specific, please? Compiler has no problem with that line. And it used to work in another IDE.

    Also: "It would rather be LIBS += -LGdi32." No. Without full path there is a message that file is not found. -L and -l makes no difference so far.


  • Qt Champions 2016

    @Uncle-Kris
    A library is passed to qmake (respectively make) with the following syntax:

    LIBS += -L/path/to/library -llibraryname
    

    The typo was that @SGaist used the switch for library path -L to specify the library name. It should've been -lgdi32. So in your case it'd look something like this:

    LIBS += -L"H:/Windows Kits/8.1/Lib/winv6.3/um/x86" -lgdi32
    

  • Lifetime Qt Champion

    Yes the typo was the L vs l but still the library name is Gdi32


  • Qt Champions 2016

    @SGaist said:

    but still the library name is Gdi32

    The windows linker isn't case sensitive. :)


  • Lifetime Qt Champion

    @kshegunov said:

    @SGaist said:

    but still the library name is Gdi32

    The windows linker isn't case sensitive. :)

    Indeed, but I wouldn't be surprised if that changed at some point :D


  • Qt Champions 2016

    @SGaist said:

    Indeed, but I wouldn't be surprised if that changed at some point :D

    Perhaps, perhaps. Although, judging from the time it took MS to introduce the x64 compiler, if it were to change, it'd take decades. And since it hasn't changed for over 30 years, I really doubt that it ever will. :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.