November 12, 2010

torsten torsten
Lab Rat
17 posts

QGraphicsScene with QGLWidget viewport is still active after closing window

 

Hello,

I have a problem with a QGraphicsScene, i want to use it in fullscreen mode. But if i use an QGLWidget as viewport, the screen stays black, even if the parent window is already closed. If i don’t use the fullscreen, it works. I’ve wrote a little demoproject (source below) you can download here [projects.ideaplexus.com] if you want to try it. I don’t get the problem.

main.cpp

  1. #include "demodialog.h"
  2.  
  3. #include <QtGui/QApplication>
  4. #include <QWidget>
  5.  
  6. int main( int argc, char* argv[] ) {
  7.     QApplication app( argc, argv );
  8.     DemoDialog *demoDialog = new DemoDialog();
  9.     demoDialog->show();
  10.     return app.exec();
  11. }

demodialog.h

  1. #ifndef DEMODIALOG_H
  2. #define DEMODIALOG_H
  3.  
  4. #include <QDialog>
  5. #include "graphicswidgetdialog.h"
  6.  
  7. class DemoDialog : public QDialog
  8. {
  9.     Q_OBJECT
  10. public:
  11.     explicit DemoDialog(QWidget *parent = 0);
  12.     ~DemoDialog();
  13.  
  14. private:
  15.     GraphicsWidgetDialog *gwd;
  16.  
  17. private slots:
  18.     void normalDialog();
  19.     void fullDialog();
  20.     void closeDialog();
  21. };
  22.  
  23. #endif // DEMODIALOG_H

demodialog.cpp

  1. #include "demodialog.h"
  2. #include <QHBoxLayout>
  3. #include <QPushButton>
  4.  
  5. DemoDialog::DemoDialog(QWidget *parent) :
  6.     QDialog(parent)
  7. {
  8.     gwd = new GraphicsWidgetDialog;
  9.     QPushButton *normal = new QPushButton(tr("normal"));
  10.     QPushButton *full = new QPushButton(tr("fullscreen"));
  11.     QHBoxLayout *layout = new QHBoxLayout;
  12.     layout->addWidget(normal);
  13.     layout->addWidget(full);
  14.     setLayout(layout);
  15.     connect(normal,SIGNAL(clicked()),this,SLOT(normalDialog()));
  16.     connect(full,SIGNAL(clicked()),this,SLOT(fullDialog()));
  17.     connect(gwd,SIGNAL(closeDialog()),this,SLOT(closeDialog()));
  18. }
  19.  
  20. DemoDialog::~DemoDialog() {
  21. }
  22.  
  23. void DemoDialog::normalDialog() {
  24.     gwd->showNormal();
  25. }
  26.  
  27. void DemoDialog::fullDialog() {
  28.     gwd->showFullScreen();
  29. }
  30.  
  31. void DemoDialog::closeDialog() {
  32.     gwd->close();
  33. }

graphicswidgetdialog.h

  1. #ifndef GRAPHICSWIDGETDIALOG_H
  2. #define GRAPHICSWIDGETDIALOG_H
  3.  
  4. #include <QDialog>
  5. #include "graphicswidget.h"
  6.  
  7. class GraphicsWidgetDialog : public QDialog
  8. {
  9.     Q_OBJECT
  10. public:
  11.     explicit GraphicsWidgetDialog(QWidget *parent = 0);
  12.     ~GraphicsWidgetDialog();
  13.  
  14. private:
  15.     GraphicsWidget *graphicsWidget;
  16.  
  17. signals:
  18.     void closeDialog();
  19.  
  20. private slots:
  21.     void keyPressEventSuccess();
  22.  
  23. };
  24.  
  25. #endif // GRAPHICSWIDGETDIALOG_H

graphicswidgetdialog.cpp

  1. #include "graphicswidgetdialog.h"
  2. #include <QHBoxLayout>
  3.  
  4. GraphicsWidgetDialog::GraphicsWidgetDialog(QWidget *parent) :
  5.     QDialog(parent)
  6. {
  7.     graphicsWidget = new GraphicsWidget(this);
  8.     QHBoxLayout *layout = new QHBoxLayout;
  9.     layout->addWidget(graphicsWidget);
  10.     layout->setContentsMargins(0,0,0,0);
  11.     setLayout(layout);
  12.     connect(graphicsWidget,SIGNAL(keyPressEventSuccess()),this,SLOT(keyPressEventSuccess()));
  13. }
  14.  
  15. GraphicsWidgetDialog::~GraphicsWidgetDialog() {
  16. }
  17.  
  18. void GraphicsWidgetDialog::keyPressEventSuccess() {
  19.     emit closeDialog();
  20. }

graphicswidget.h

  1. #ifndef GRAPHICSWIDGET_H
  2. #define GRAPHICSWIDGET_H
  3.  
  4. #include <QWidget>
  5. #include <QGraphicsScene>
  6. #include <QGraphicsView>
  7. #include <QKeyEvent>
  8.  
  9. class GraphicsWidget : public QWidget
  10. {
  11.     Q_OBJECT
  12. public:
  13.     explicit GraphicsWidget(QWidget *parent = 0);
  14.     ~GraphicsWidget();
  15.  
  16. private:
  17.     QGraphicsScene *scene;
  18.     QGraphicsView *view;
  19.     bool listenEvents;
  20.  
  21. protected:
  22.     void keyPressEvent(QKeyEvent *event);
  23.  
  24. signals:
  25.     void keyPressEventSuccess();
  26. };
  27.  
  28. #endif // GRAPHICSWIDGET_H

graphicswidget.cpp

  1. #include "graphicswidget.h"
  2. #include <QGLWidget>
  3. #include <QHBoxLayout>
  4.  
  5. GraphicsWidget::GraphicsWidget(QWidget *parent) :
  6.     QWidget(parent)
  7. {
  8.     scene = new QGraphicsScene;
  9.     view = new QGraphicsView(scene);
  10.     view->setViewport(new QGLWidget(QGLFormat(QGL::DirectRendering)));
  11.     view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  12.     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13.     view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  14.     view->setFrameStyle(QFrame::NoFrame);
  15.     view->setBackgroundBrush(Qt::black);
  16.     view->setRenderHints(QPainter::Antialiasing);
  17.     QHBoxLayout *layout = new QHBoxLayout;
  18.     layout->addWidget(view);
  19.     layout->setContentsMargins(0,0,0,0);
  20.     setLayout(layout);
  21.     listenEvents=true;
  22.     grabKeyboard();
  23. }
  24.  
  25. GraphicsWidget::~GraphicsWidget() {
  26. }
  27.  
  28. void GraphicsWidget::keyPressEvent(QKeyEvent *event) {
  29.     if (listenEvents) {
  30.         switch (event->key()) {
  31.         case Qt::Key_F11:
  32.             emit keyPressEventSuccess();
  33.             break;
  34.         default:
  35.             QWidget::keyPressEvent(event);
  36.         }
  37.     } else {
  38.         QWidget::keyPressEvent(event);
  39.     }
  40. }

3 replies

November 12, 2010

iunknwn iunknwn
Lab Rat
33 posts

Memory leak at demoDialog.cpp:8: gwd = new GraphicsWidgetDialog;
Change to: gwd = new GraphicsWidgetDialog(this);

Problem solved.

 Signature 

Vista x64 with Qt 4.8.2 and VS2010

November 12, 2010

torsten torsten
Lab Rat
17 posts

Dammit, you don’t believe how long i’m seaching for this… tanks

November 13, 2010

torsten torsten
Lab Rat
17 posts

mmh, yesterday it works, but now it seems there is another problem. I’ve added some debug output and i found out, that GraphicsWidget in showFullScreen don’t have focus/isactivewindow. so the problem exists

 
  ‹‹ Is there a lightweight browser for me to display web contents?      QTableWidget - how to delete a row? ››

You must log in to post a reply. Not a member yet? Register here!