Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The GdiPlus API show different behaves in QT



  • Here is a demo which shows different behaves using GDIPlus API in QT.Pay attention to the Note section

    #include <QtWidgets/qapplication.h>
    #include <iostream>
    #include <comdef.h>
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
    using namespace std;
    int main(int args, char** argv)
    {
        QApplication app(args, argv);	
        ULONG_PTR gdiplusToken;
        GdiplusStartupInput gdiplusStartupInput;
        GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL);
        Bitmap mBitMap(506, 283);
        Graphics* mGr = Graphics::FromImage(&mBitMap);
        mGr->SetPageUnit(Gdiplus::Unit::UnitPixel);
        mGr->SetPageScale(1.0f);
        Gdiplus::Font font((L"Aharoni"), 14.0f,  (Gdiplus::Unit)1);
        StringFormat* stringFormat = new StringFormat();
        stringFormat->SetAlignment(StringAlignmentCenter);
        stringFormat->SetLineAlignment(StringAlignmentCenter);
        RectF rect(0, 0, 409.05f, 141.0f);
        RectF boundingBox;
        INT codepointsFitted = 0;
        INT linesFilled = 0;
    	  /*
          **Note :When you remove "QApplication app(args, argv);", boundingBox will show different values,please debug and check.**
    	  */
        Gdiplus::Status status = mGr->MeasureString((L"Chart Title"), 7 , &font, rect, 
        stringFormat,&boundingBox,&codepointsFitted,&linesFilled);
        delete stringFormat;
        GdiplusShutdown(gdiplusToken);
        return 0;
    }
    

    Not only this GDI+ API but others also show different value,Who can tell me why and how to fix it to get coincident value by Gdiplus API ? Thanks!


  • Lifetime Qt Champion

    Hi,

    Which version of Qt ?
    Which version of Windows ?
    What are these differences ?
    What happens if you create your QApplication object after initializing GdiPlus ?



  • Hi,
    A1: Qt version is qt-opensource-windows-x86-5.12.2
    A2: My windows is win10
    A3: If I don't remove the code “QApplication app(args, argv);” the value of the variable
    boundingBox as follows (Value A):

    • boundingBox class Gdiplus::RectF
      Height 25.96516609
      Width 86.74423218
      X 161.1528778
      Y 57.51741409

    if I remove the code “QApplication app(args, argv);” the value of the variable
    boundingBox as follows (Value B):

    • boundingBox class Gdiplus::RectF
      Height 20.77213097
      Width 69.39538574
      X 169.827301
      Y 60.11393356

    A4: If I create my QApplication object after initializing GdiPlus like this:

    ...
    GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL);
    QApplication app(args, argv);
    ...
    

    the value of boundingBox is the same to Value B
    Thanks!


  • Lifetime Qt Champion

    So it seems there might be some initialisation done by Qt that interacts with GdiPlus.

    What is your use case by the way ?



  • First of all, thanks for your reply!
    Actually we integrated a third part library in QT(5.12) which drawing by Gdiplus but we found the drawing result is different compare to running the third part lib in Visual Studio.
    So I give you a simple demo above which has nothing to do with the third part library to explain the different behavior using GDIPlus in QT.
    I created the demo in QTCreate 4.8.2, by Application-> QT Console Application by a CMakeLists.txt.

    cmake_minimum_required(VERSION 3.1.0)
    
    project(qtdemo VERSION 1.0.0 LANGUAGES CXX)
    
    get_filename_component(PARENT_DIR ./ ABSOLUTE)
    message(PARENT_DIR:${PARENT_DIR})
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    set(CMAKE_AUTOMOC ON)
    
    if(CMAKE_VERSION VERSION_LESS "3.7.0")
        set(CMAKE_INCLUDE_CURRENT_DIR ON)
    endif()
    
    find_package(Qt5 COMPONENTS Widgets REQUIRED)
    
    add_executable(qtdemo main.cpp)
    
    target_link_libraries(qtdemo Qt5::Widgets )
    

  • Lifetime Qt Champion

    Maybe this stack overflow thread might help.


Log in to reply