QGraphicsProxyWidget does not get focus, when using setFocus()
-
Hello.
When i use setFocus() in my program(keyPressEvent), the QGraphicsProxyWidget not gain the Focus.
The titlebar is not active and also hasFocus() return false.What i am doing wrong?
Thank you.
I have a mini testcode here:
test.pro
@QT += core gui openglHEADERS +=
scene.hSOURCES +=
main.cpp
scene.cpp@main.cpp
@#include "scene.h"int main(int argc, char *argv[])
{
QApplication a( argc, argv );
Scene *scene = new Scene(800, 600);QGraphicsView view; view.setStyleSheet( "QGraphicsView { border-style: none; }" ); QGLFormat glFormat; view.setViewport(new QGLWidget(glFormat)); view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate); view.setScene(scene); view.resize(800, 600); view.setGeometry( QStyle::alignedRect( Qt::LeftToRight, Qt::AlignCenter, view.size(), a.desktop()->availableGeometry() )); view.show(); return a.exec();
}@
scene.h
@#ifndef SCENE_H
#define SCENE_H#include <QtGui>
#include <QtOpenGL>class Scene : public QGraphicsScene
{
Q_OBJECT
public:
Scene(int width, int height);
~Scene();
virtual void drawBackground(QPainter painter, const QRectF &rect);
public slots:
void animate();
void startApp();
protected:
void keyPressEvent(QKeyEvent event);
public:
void initUI();
QGraphicsProxyWidget *proxyDialog, *proxyStartMenu;
QLineEdit *line;
};#endif // SCENE_H@
scene.cpp
@#include "scene.h"Scene::Scene(int width, int height)
: QGraphicsScene() {
setSceneRect(0, 0, width, height);
QMetaObject::invokeMethod(this, "startApp", Qt::QueuedConnection);
}Scene::~Scene(){}
void Scene::startApp() {
initUI();
QTimer::singleShot(0, this, SLOT(animate()));
}void Scene::initUI() {
proxyDialog = new QGraphicsProxyWidget;
proxyDialog->setWidget(new QWidget());
proxyDialog->setWindowFlags(Qt::Dialog);
proxyDialog->setWindowTitle(QString("Dialog"));
proxyDialog->setVisible(true);
this->addItem(proxyDialog);proxyStartMenu = new QGraphicsProxyWidget; proxyStartMenu->setWidget(new QWidget()); proxyStartMenu->setWindowFlags(Qt::Dialog); proxyStartMenu->setWindowTitle(QString("Menu")); proxyStartMenu->setVisible(true); line = new QLineEdit(proxyStartMenu->widget()); line->setGeometry(10,10,100,25); line->setVisible(true); this->addItem(proxyStartMenu); proxyDialog->setGeometry(QRect(40,140,200,100)); proxyStartMenu->setGeometry(QRect(140,40,200,100)); proxyStartMenu->setFocus(Qt::MouseFocusReason);
}
void Scene::animate() {
update();
QTimer::singleShot(10, this, SLOT(animate()));
}void Scene::drawBackground(QPainter *painter, const QRectF &) {
painter->beginNativePainting();
glClearColor(0.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
painter->endNativePainting();
}void Scene::keyPressEvent(QKeyEvent* event) {
if(event->isAutoRepeat()) {
QGraphicsScene::keyPressEvent(event);
} else {
if (event->key() == Qt::Key_F1) {
proxyStartMenu->setFocus();
qDebug() << "F1 pressed, hasFocus: " << proxyStartMenu->hasFocus();
} else {
QGraphicsScene::keyPressEvent(event);
}
}
}@