Qt:QPainter + GDI在同一个小部件中吗? [英] Qt: QPainter + GDI in the same widget?

查看:129
本文介绍了Qt:QPainter + GDI在同一个小部件中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处描述的方法在同一小部件​​上使用QPainter和GDI调用.
不幸的是,本教程似乎是在早期版本的Qt上编写的,现在它不起作用.

I'm trying to use the method described here to use a QPainter and GDI calls on the same widget.
Unfortunately this tutorial seem to have been written on an earlier version of Qt and now it does not work.

我设置了WA_PaintOnScreen标志并重新实现paintEngine()以返回NULL.

I set the WA_PaintOnScreen flag and reimplement paintEngine() to return NULL.

然后在paintEvent()上创建一个QPainter,使用它,然后使用一些GDI调用绘制位图. GDI调用工作正常,但QPainter却不执行任何操作.我在控制台上收到以下错误:

Then on the paintEvent() I create a QPainter, use it and then use some GDI calls to paint a bitmap. The GDI calls work fine but the QPainter does nothing. I get the following error on the console:

QPainter::begin: Paint device returned engine == 0, type: 1

这不再受支持了吗?我该怎么办?

Is this simply not supported anymore? how can I do it?

我也尝试过在GDI绘画小部件之上创建其他小部件,但是效果不佳,因为顶部小部件显示为黑色并阻塞了GDI小部件.

I've also tried creating an additional widget on top of the GDI-painting widget but that didn't go well as well since the top widget appears black and blocks the GDI widget.

推荐答案

我在QT 4.7-beta 2中按如下方式进行了工作

I got this working in QT 4.7-beta 2 as follows

  1. 在构造函数中调用setAttribute(Qt :: WA_PaintOnScreen,true);
  2. 请勿重新实现paintEngine()以返回NULL;
  3. 在paintEvent()中使用以下代码;

  1. In the constructor call setAttribute(Qt::WA_PaintOnScreen,true);
  2. Do NOT reimplement paintEngine() to return NULL;
  3. Use the following code in the paintEvent();

QPainter painter(this);
HDC hdc = painter.paintEngine()->getDC();   // THIS IS THE CRITICAL STEP! 
HWND hwnd = winID();

   // From this point on it is all regular GDI 
QString text("Test GDI Paint");
RECT rect;
GetClientRect(hwnd, &rect);

HBRUSH hbrRed = CreateSolidBrush(RGB(255,0,0));
FillRect(hdc, &rect, hbrRed);
HBRUSH hbrBlue = CreateSolidBrush(RGB(40,40,255));
HPEN bpenGreen = CreatePen(PS_SOLID, 4, RGB(0,255,0));
SelectObject(hdc,bpenGreen);
SelectObject(hdc,hbrBlue);

Ellipse(hdc,10,10,rect.right-20,rect.bottom-20);
SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
TextOutW(hdc, width() / 2, height() / 2, text.utf16(), text.size());
ReleaseDC(hwnd, hdc);

这篇关于Qt:QPainter + GDI在同一个小部件中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆