#include #include #include #include #include #include #include class Item : public QGraphicsRectItem { public: Item() : QGraphicsRectItem(0,0,100,100) { setAcceptHoverEvents(true); setPen(Qt::NoPen); setBrush(Qt::green); } protected: void hoverEnterEvent(QGraphicsSceneHoverEvent* e) { setBrush(Qt::red); } void hoverLeaveEvent(QGraphicsSceneHoverEvent* e) { setBrush(Qt::green); } }; class View : public QGraphicsView { public: View(QWidget *parent = {}): QGraphicsView(parent) { setFrameStyle(QFrame::NoFrame); setBackgroundBrush(Qt::black); setViewport(new QOpenGLWidget); //< this causes problems setScene(new QGraphicsScene); scene()->addItem(new Item); } void resizeEvent(QResizeEvent* e) { scene()->setSceneRect({{0,0}, e->size()}); } void mouseDoubleClickEvent(QMouseEvent* e) { if(windowFlags() & Qt::FramelessWindowHint) { setWindowFlags(windowFlags() & ~Qt::FramelessWindowHint); } else { setWindowFlags(windowFlags() | Qt::FramelessWindowHint); } show(); } }; #include #include using OpenGLClass = QOpenGLWidget; //< QOpenGLWindow or QOpenGLWidget class Widget : public OpenGLClass { using Base = OpenGLClass; public: Widget() { // QOpenGLWidget case setMouseTracking(true); } protected: void mouseMoveEvent(QMouseEvent * e) { _itemBrush = (_itemRect.contains(e->pos()) ? Qt::red : Qt::green); update(); } bool event(QEvent *e) { switch(e->type()) { case QEvent::Leave: _itemBrush = Qt::green; update(); break; default: break; } return Base::event(e); } void mouseDoubleClickEvent(QMouseEvent* e) { // QOpenGLWidget case if(windowFlags() & Qt::FramelessWindowHint) { setWindowFlags(windowFlags() & ~Qt::FramelessWindowHint); } else { setWindowFlags(windowFlags() | Qt::FramelessWindowHint); } // QOpenGLWindow case // if(flags() & Qt::FramelessWindowHint) // { // setFlags(flags() & ~Qt::FramelessWindowHint); // } // else // { // setFlags(flags() | Qt::FramelessWindowHint); // } show(); } void paintGL() { QPainter p(this); p.setPen(Qt::NoPen); p.setBrush(_itemBrush); p.drawRect(_itemRect); } private: QRectF _itemRect {0.,0.,100.,100.}; QBrush _itemBrush {Qt::green}; }; int main(int argc, char ** argv) { QApplication app(argc, argv); // View w; //< scene view case Widget w; w.resize(200,200); w.show(); return app.exec(); }