Qt Events - why doesn't this work?



  • I have an event handler that handles key presses and when the spacebar key is pressed, then a mouse is clicked, then the application would create another widget.

    However, the application never handles the mouse click.

    keypress.cpp:

    @
    #include "keypress.h"
    #include "ui_keypress.h"
    #include "mousehandle.h"

    #include <QKeyEvent>

    KeyPress::KeyPress(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::KeyPress)
    {
    ui->setupUi(this);
    }

    KeyPress::~KeyPress()
    {
    delete ui;
    }

    void KeyPress::keyPressEvent(QKeyEvent * event)
    {
    if (event->key() == Qt::Key_Space)
    {
    ui->KeyTracker->setText("Spacebar is pressed!");
    MouseHandle mouseHandler;
    }
    }

    void KeyPress::keyReleaseEvent(QKeyEvent * event)
    {
    if (event->key() == Qt::Key_Space)
    {
    ui->KeyTracker->setText("Spacebar is released!");
    }
    }
    @

    mousehandle.cpp:

    @
    #include "mousehandle.h"
    #include "ui_keypress.h"

    MouseHandle::MouseHandle(QWidget *parent) :
    QWidget(parent)
    {
    }

    void MouseHandle::mousePressEvent(QMouseEvent event)
    {
    QLabel
    mouseLabel = new QLabel;
    mouseLabel->setText("Mouse pressed!");
    }
    @



  • Are you sure that it never handles the click? Put a breakpoint into mousePressEvent and see what happens. You create a label without a parent and you never call show() to actually show the label.

    //you also don't show MouseHandle mouseHandler... so where exactly your application should handle the click?



  • I updated my code to show the label, and it is as following:

    mousehandler.cpp:

    @
    #include "mousehandle.h"
    #include "ui_keypress.h"

    #include <QDebug>

    MouseHandle::MouseHandle(QWidget *parent) :
    QWidget(parent)
    {
    qDebug() << "mouse handler created";
    }

    void MouseHandle::mousePressEvent(QMouseEvent event)
    {
    QLabel
    mouseLabel = new QLabel;
    mouseLabel->setText("Mouse pressed!");
    mouseLabel->show();
    }
    @

    As you can see, in the constructor, I use qDebug() to notify me when a MouseHandle is created. Indeed, the MouseHandle is created when I press the spacebar.

    P.S. Look at the keypress.cpp's keyPressEvent, it creates the MouseHandle.



  • MouseHandle is created, i see that (i said it is not showed), how you want to click it if it isn't showed.
    Also you may want to create that on heap else even if you call show it will immediately disappear because the object goes out of scope.
    Maybe you should tell us what are you trying to achieve.



  • -I am just playing around with Qt events. In this application, I want to press spacebar, and when I press the spacebar and then click, a new widget appears.-

    -So the click event only registers when you click the widget? I thought it registers whenever and wherever you click..-

    -I think the problem with my code is that I have to click while the keyPressEvent is executing in order for the widget to show.-

    Never mind, I understand what you mean. The click must be within the QWidget's window (thus must be visibled to be clicked) to be registered.

    How would I go about doing this when the spacebar is pressed? Should I allocate the widget somewhere else and then wait for the spacebar to be pressed then show it? This is inefficient though, because the spacebar may never be pressed.

    Or should I create a pointer in the KeyPress class to a new MouseHandle and then allocate memory for it when the spacebar is pressed? That seems quite favorable..



  • You didn't tell us what are you trying to achieve: do you really want to show some widget when space is pressed and user should click on that or do you want the combination of space pressed + click on the mainwindow? If later you can make a simple bool member that is true while space is pressed (set true on press and false on release) and handle the click in the same class (mousePressEvent must be overriden in the same class KeyPress in your case and i here check the boolean and do the space + click combination if that is true).

    Anyway right now you create a new widget (that you don't show) and only this one react to click, so please describe what are you trying to do.



  • Thanks, for the help, I got what I wanted to work.

    -I am just wondering. Do all mouse events only work when you click on the exact widget that has the mouseEvents implemented? For example, if you have two widgets, one with mousePressEvent implementation and one without, does the mouse event only work with the one with the implementation?-

    -Is this the same with key events, or do they work no matter what widget?-

    I should rephrase my question. Do events only register when the widget has the respective event implemented and the widget is the current window/is selected?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.