Important: Please read the Qt Code of Conduct -

Problem Using QLabel::paintEvent to Draw an Image

  • I need to draw an image that is formatted as a "TCL" file and must be displayed using a function in a TCL library named TCL_Display. TCL_Display requires an HDC object to be provided, and it draws the image onto that device context.

    In order to accomplish this in a QLabel window, I am overriding the paintEvent function as shown below (my NavChartImgWnd class is derived from QLabel). Note that mChartId will be zero until an image has been loaded (by another function in the TCL library), so the paintEvent function will not do anything until an image has been loaded and mChartId has been set to a valid chart ID that TCL_Display can operate on.

    void NavChartImgWnd::paintEvent(QPaintEvent * event)
    if( mChartId == 0) return;

    HDC      lWndDC = getDC();
    TCL_Display( mChartId, hDC, 1.0, 1.0, NULL, NULL, 0);


    The result I get when I execute this code is that the image appears momentarily in the window and then disappears. I can get the image to appear and persist by moving the scroll bar (the NavChartImgWnd object is placed inside a QScrollWindow object using setWidget), but it disappears whenever focus is shifted to another window and does not reappear until I once again move the scroll bar. By setting a break point after the call to TCL_Display, I can verify that paintEvent is being called when focus is returned to the window, and the image appears on the screen immediately after the call to TCL_Display, but it is erased after leaving paintEvent.

    Can anyone tell me what I'm doing wrong? I am new to Qt, so I'm probably making some very fundamental errors.

Log in to reply