How to steal focus on simple mouse click at an empty space of the window?



  • So i have a spinbox which is connected with editingfinished..
    so i want the void editing finished to be called if you click with the mouse at an empty space at that window.. How to accomplish that?



  • If I understand you correctly, you can inherit from QWidget and override the "event":http://qt-project.org/doc/qwidget.html#mousePressEvent :

    @
    class MyClass: public QWidget {
    protected:
    void mousePressEvent(QMouseEvent *event) {
    // some code
    }
    }
    @



  • In addition to the above code you need to emit a signal inside mousePressEvent() like

    @void MyClass::mousePressEvent(QMouseEvent *event)
    {

    if (event->button() == Qt::LeftButton)
        emit clicked();
    

    }@

    where clicked() is a signal declared in .h file eg

    @class MyClass : public QWidget
    {
    Q_OBJECT
    public:
    //some declaration ... ...

    signals:
    void clicked();

    private:
    Ui::MyClass *ui;

    };@

    and you need to connect signal & slots like

    @connect(this,SIGNAL(clicked()),SLOT(editingfinished()));@


Log in to reply
 

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