How to use focusInEvent and focusOutEvent

14,507

Solution 1

you should do something like this

class YourButton : public QToolButton
{
    Q_OBJECT

    protected:

    void focusInEvent(QFocusEvent* e);
    void focusOutEvent(QFocusEvent* e);
};

in .cpp file

void YourButton::focusInEvent(QFocusEvent* e)
{
    if (e->reason() == Qt::MouseFocusReason)
    {
      // Resize the geometry -> resize(bigWidth,bigHeight); 
    }


    QToolButton::focusInEvent(e);
}

then use the yourButton class in your mainWindow.

also (another option) you can use http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter in your mainWindow .

Solution 2

The solution from @Wagmare will work only for buttons outside a layouts. To make it work inside of layout it should look like this:

class YourButton : public QToolButton
{
    Q_OBJECT
    // proper constructor and other standard stuff 
    // ..

protected:
    void focusInEvent(QFocusEvent* e) {
        QToolButton::focusInEvent(e);
        updateGeometry();
    }

    void focusOutEvent(QFocusEvent* e) {
        QToolButton::focusOutEvent(e);
        updateGeometry();
    }


public:
    QSize sizeHint() const {
        QSize result = QToolButton::sizeHint();
        if (hasFocuc()) {
            result += QSize(20,20);
        }
        return result;
    }
};

With proper size policy it will also work without a layout.


Another cool solution without subclassing is a style sheet:
QPushButton:focus {
    min-height: 40px
    min-width:  72px
}
Share:
14,507
Suresh
Author by

Suresh

I am working as a intern in ByDesign.

Updated on June 17, 2022

Comments

  • Suresh
    Suresh almost 2 years

    I am implementing an application in which i am having 3 QToolButton and when the focus is coming on any QToolButton it should resize. One of my friend has given me answer but i am unable to figure it out as i am inheriting QMainWindow class as well in my mainWindow. He is telling to inherit QToolButton too. But multiple inheritance problem will occur. So how exactly to use focusInEvent().

    MyCode:
    mywindow.h :
    
    class mywindow : public QMainWindow
    {
        Q_OBJECT
    public:
        mywindow() ;
    
    protected:
        void keyReleaseEvent(QKeyEvent *event); 
        void focusInEvent(QFocusEvent *event);
        void focusOutEvent(QFocusEvent *event);
    
    private:
        QWidget *widget;
        QStackedWidget *stack1;
        QToolBar *tool;
        QListWidget *list1;
        QListWidget *list2;
        QVBoxLayout *vertical;
        QToolButton *button1;
        QToolButton *button2;
        QToolButton *button3;
    
    public slots:
        void fileNew();
        void file();
        bool eventFilter(QObject *object, QEvent *event);
    
    };
    

    mywindow.cpp :

    mywindow::mywindow() : QMainWindow()
    {   
      //some code
    }
    

    My friend's code which i have to merge :

    class mywindow : public QToolButton
    {
        private:
             int originalWidth, originalHeight;
             int bigWidth, bigHeight;
    };
    
    void focusInEvent ( QFocusEvent * event ) { 
                       resize(bigWidth,bigHeight); 
                       QToolButton::focusInEvent(event); 
    }
    
    void focusOutEvent ( QFocusEvent * event ) { 
                       resize(originalWidth,originalHeight); 
                       QToolButton::focusOutEvent(event);
    }