C++ (Qt)#include <QtWidgets>#include <QGLWidget> #define PARENT_CLASS QGLWidget class MyWidget : public PARENT_CLASS {public: MyWidget( QWidget * parent = 0 ) : PARENT_CLASS(parent) { } void paintGL( void ) { qDebug() << "paintGL"; } void mousePressEvent( QMouseEvent * ) { update(); } void paintEvent( QPaintEvent * ) { static int theCount = 0; qDebug() << "paintEvent" << ++theCount; QPainter painter(this); painter.fillRect(rect(), Qt::gray); QColor color[] = { Qt::red, Qt::green, Qt::blue }; int width = rect().width(); int height = rect().height(); QRect R(0, 0, width / 5, height / 5); for (int i = 0; i < 10; ++i) painter.fillRect(R.translated(qrand() % width, qrand() % height), color[qrand() % 3]); }}; int main( int argc, char **argv ) { QApplication app(argc, argv); QDialog * dlg = new QDialog; dlg->setGeometry(100, 100, 400, 400); MyWidget * mw = new MyWidget(dlg); mw->setGeometry(0, 0, 400, 400); QPushButton * btn = new QPushButton("Button", dlg); btn->setGeometry(10, 10, 60, 20); dlg->show(); return app.exec();}
#include <QtWidgets>#include <QGLWidget>#include <QHBoxLayout>class MyWidget : public QGLWidget {public: MyWidget( QWidget * parent = 0 ) : QGLWidget(parent) { QHBoxLayout* lay = new QHBoxLayout(this); QPushButton * btn = new QPushButton("Button", this); lay->addWidget(btn); } void paintGL( void ) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); }};int main( int argc, char **argv ){ QApplication app(argc, argv); QDialog * dlg = new QDialog; dlg->setGeometry(100, 100, 400, 400); MyWidget * mw = new MyWidget(dlg); QHBoxLayout lay; lay.addWidget(mw); dlg->setLayout(&lay); dlg->show(); return app.exec();}
int main( int argc, char **argv ){ QApplication app(argc, argv); QDialog * dlg = new QDialog; dlg->setGeometry(100, 100, 400, 400); MyWidget * mw = new MyWidget(0);// QHBoxLayout lay;// lay.addWidget(mw);// dlg->setLayout(&lay); mw->show(); return app.exec();}
C++ (Qt)setAttribute(Qt::WA_PaintOnScreen, false);