Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsProxyWidget does not get focus, when using setFocus()
Qt 6.11 is out! See what's new in the release blog

QGraphicsProxyWidget does not get focus, when using setFocus()

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bugtracker
    wrote on last edited by
    #1

    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 opengl

    HEADERS +=
    scene.h

    SOURCES +=
    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);
    }
    }
    }@

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved