Insert QWidget Object in Qt Quick 2

8 months ago there was such a question - how to embed a qwidget-based object in QML, http://doc.qt.digia.com/4.7/declarative-cppextensions-qwidgets.html Qt5. Insert a QWidget object in QML . Has the situation changed? Or for some complex applications, using our own overridden paintEvent, can we use only classic Qt?

+4
source share
1 answer

QQuickPaintedItem can be used to draw using the QPainter API.

In the code below, I tried to wrap a QCalendarWidget in a QQuickPaintedItem . It correctly displays, but does not handle input events:

.hour:

 class CalendarControl : public QQuickPaintedItem { Q_OBJECT public: explicit CalendarControl(QQuickItem *parent = 0); virtual ~CalendarControl(); void paint(QPainter *painter); … protected: QCalendarWidget *calendar_; } 

.cpp:

 CalendarControl::CalendarControl(QQuickItem *parent) : QQuickPaintedItem(parent) , calendar_(NULL) { setOpaquePainting(true); setAcceptHoverEvents(true); setAcceptedMouseButtons(Qt::AllButtons); calendar_ = new QCalendarWidget; // Calendar will draw partially if update is called right here QTimer::singleShot(0, this, SLOT(update())); } void CalendarControl::paint(QPainter *painter) { calendar_->render(painter, QPoint(), QRegion(), QCalendarWidget::DrawWindowBackground | QCalendarWidget::DrawChildren); } 

To catch mouse events, override

 void hoverEnterEvent(QHoverEvent *event); void hoverLeaveEvent(QHoverEvent *event); void hoverMoveEvent(QHoverEvent *event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); 

I could not pass them to QCalendarWidget although it ignores them. But when you create a wrapper for a custom QWidget you can probably pass these events directly to it.

+5
source

Source: https://habr.com/ru/post/1491636/


All Articles