Alatriste
Alatriste

Reputation: 567

Qt Directx rendering

I'm rendering in Qt widget by DirectX. I disabled qt paint engine

QPaintEngine *paintEngine() const { return NULL; }

but sometimes I want to enable qt paint and disable directX rendering. I'm disabling DirectX rendering, but how can I enable Qt paintEngine?

Upvotes: 1

Views: 1963

Answers (2)

Antony Riakiotakis
Antony Riakiotakis

Reputation: 121

Use

QWidget::setAttribute(Qt::WA_OpaquePaintEvent, false);

to reenable Qt background drawing on your widget.

Upvotes: 0

The possible solutions include:

  1. Use a QWindow, and then switch rendering between [raster][0] and DirectX.

  2. Use a DirectX-rendered QWindow or QWidget, and render the optional content to a QImage. You can use that image as a texture and easily overlay it over the DirectX-rendered content.

  3. A quick workaround with little code changes would be to re-create the widget each time the rendering mode changes. This could be implemented very cleanly by having the state of the widget reside in a PIMPL. See the example below.

class MyWidgetPrivate {
public:
  bool qtRendering;
  // your data members etc.
  MyWidgetPrivate(bool qtRendering) :
    qtRendering(qtRendering)
  {}
};

class MyWidget : public QWidget {
  Q_OBJECT
  Q_DECLARE_PRIVATE(MyWidget)
  QScopedPointer<MyWidgetPrivate> const d_ptr;

  QPaintEngine *paintEngine() const {
    Q_D(const MyWidget);
    return d->qtRendering ? QWidget::paintEngine() : nullptr;
  }

  MyWidget(QScopedPointer<MyWidgetPrivate> & data, QWidget * parent, bool qtRendering) :
    QWidget(parent),
    d_ptr(data.take())
  {
     d_ptr->qtRendering = qtRendering;
  }
public:
  MyWidget(QWidget * parent, bool qtRendering) :
    QWidget(parent),
    d_ptr(new MyWidgetPrivate(qtRendering))
  {}

  void setQtRendering(bool qtRendering) {
    if (qtRendering == d_ptr->qtRendering) return;
    auto geom = geometry();
    auto parent = this->parentWidget();
    auto & d = const_cast<QScopedPointer<MyWidgetPrivate>&>(d_ptr);
    QScopedPointer<MyWidgetPrivate> pimpl(d.take());
    this->~MyWidget(); // destroy in place
    new (this) MyWidget(pimpl, parent, qtRendering); // reconstruct in place
    setGeometry(geom);
  }
};

Upvotes: 1

Related Questions