I created a simple project to show the problem that I have in a larger application.
So, I create a mainwindow with 2 buttons. I am creating a class that inherits from a QWidget with 2 buttons and a QGL view.
The problem is that, apparently, and somehow I canβt find it, creating this QGL view blocks some view events, especially mouse events. In the following event code of the mouse, hover over the buttons and the main widget of class 1 will not be detected (the cursor should change from an arrow to a manual one and wait accordingly), while the mouse cursor changes well on the buttons of the main window (but in my real application, this is more more serious than a simple hover event).
I understand that this problem only occurs on Mac 10.6 and higher, and not on Windows. I am using Qt 5.1.
Here is the code:
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class Class1; class PanoramaGLView; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; Class1 *c1; PanoramaGLView *gl; }; #endif // MAINWINDOW_H
mainwindoww.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> using namespace std; #include "class1.h" #include "panoramaglview.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); c1=new Class1(0); ui->centralLayout->addWidget(c1); //if created here, no problem with mouse cursor on mainwindow buttons but not what I want //gl=new PanoramaGLView(0); //ui->centralLayout->addWidget(gl); } MainWindow::~MainWindow() { delete ui; }
myglview.h:
#ifndef MYGLVIEW_H #define MYGLVIEW_H #include <QGraphicsView> #include <QtOpenGL/QGLWidget> class MyGLView : public QGraphicsView { Q_OBJECT public: MyGLView(QWidget* pParent = 0); virtual ~MyGLView(); protected: QGLWidget* m_pGLWidget; }; #endif // MYGLVIEW_H
myglview.cpp:
#include "myglview.h" MyGLView::MyGLView(QWidget* pParent) : QGraphicsView(pParent) { m_pGLWidget = new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::AlphaChannel)); m_pGLWidget->makeCurrent(); setViewport(m_pGLWidget); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); setViewportUpdateMode(QGraphicsView::FullViewportUpdate); } MyGLView::~MyGLView() { delete m_pGLWidget; }
class1.h:
#ifndef CLASS1_H #define CLASS1_H #include <QWidget> class MyGLView; namespace Ui { class Class1; } class Class1 : public QWidget { Q_OBJECT public: explicit Class1(QWidget *parent = 0); ~Class1(); private: Ui::Class1 *ui; public slots: void click1(); }; #endif // CLASS1_H
class1.cpp:
#include "class1.h" #include "ui_class1.h" #include "myglview.h" #include <iostream> using namespace std; Class1::Class1(QWidget *parent) : QWidget(parent), ui(new Ui::Class1) { ui->setupUi(this); MyGLView *glv=new MyGLView(0); ui->verticalLayout->addWidget(glv); connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(click1())); } Class1::~Class1() { delete ui; } void Class1::click1(){ cout<<"Class1::click1()"<<endl; }
mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QVBoxLayout" name="centralLayout"> <item> <widget class="QWidget" name="widget" native="true"> <property name="styleSheet"> <string notr="true">background-color: rgb(255, 54, 76);</string> </property> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QPushButton" name="pushButton"> <property name="cursor"> <cursorShape>PointingHandCursor</cursorShape> </property> <property name="text"> <string>PushButton</string> </property> </widget> </item> </layout> </widget> </item> </layout> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
class1.ui:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Class1</class> <widget class="QWidget" name="Class1"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="cursor"> <cursorShape>WaitCursor</cursorShape> </property> <property name="windowTitle"> <string>Form</string> </property> <property name="styleSheet"> <string notr="true"/> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QPushButton" name="pushButton"> <property name="cursor"> <cursorShape>PointingHandCursor</cursorShape> </property> <property name="text"> <string>1</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_2"> <property name="cursor"> <cursorShape>PointingHandCursor</cursorShape> </property> <property name="text"> <string>2</string> </property> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
If I do not create a GL view, there is no problem, of course. And if I instantiate myGLView directly in MainWindow, without class 1, it works too. But I need to create different views, and not everything in the mainwindow (usually how ui are created).
In my opinion, this is a problem with the way I create the view, its parent or something like that, since the event is being passed from parent to child, so I think the problem is related to this. If it works well, the mouse cursor should change when the mouse moves over buttons 1 and 2 (the manual cursor) and just moves the main widget of class1 (the waiting cursor).
Any ideas?