2

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);
}
Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
Suresh
  • 745
  • 4
  • 11
  • 25

2 Answers2

3

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 .

Wagmare
  • 1,354
  • 1
  • 24
  • 58
  • It is giving this error: ‘YourButtton’ has not been declared – Suresh Jan 16 '14 at 07:16
  • you have to create a new class inheriting QToolButton(example i gave YourButton as YourButton.h and YourButton.cpp). then in .h file you override the protected : foucusInEvent() and in .cpp file you can implement resizing of the YourButton . see this link answer http://stackoverflow.com/questions/2804115/qlineedit-focus-event. then you have to use this YourButton in your mywindow class. – Wagmare Jan 16 '14 at 07:24
  • i am serching for you to show how to create a custom widget but couldnt able to find a proper example. you can follow the analogclock, shapedClock in qt examples but not that much relevant. if any one in this forum can help him .. please do it .. – Wagmare Jan 16 '14 at 09:10
  • http://qt-project.org/doc/qt-4.8/widgets-analogclock.html and http://books.google.co.in/books?id=tSCR_4LH2KsC&pg=PA101&lpg=PA101&dq=how+to+create+custom+widgets+%2B+qt&source=bl&ots=E703pV27Fc&sig=Y2ip2QNzjM7kaoDImMsCfIq1HNw&hl=en&sa=X&ei=p6LXUuCZAcH_rAexroCwDg&ved=0CGwQ6AEwCA#v=onepage&q=how%20to%20create%20custom%20widgets%20%2B%20qt&f=false can help you – Wagmare Jan 16 '14 at 09:14
  • I have written everything but it is not working. No error is coming – Suresh Jan 16 '14 at 09:15
  • The idea it self is good, but the way size is updated is incorrect, see my answer. – Marek R Jan 16 '14 at 10:28
1

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
}
Marek R
  • 32,568
  • 6
  • 55
  • 140