【问题标题】:How to use focusInEvent and focusOutEvent [duplicate]如何使用 focusInEvent 和 focusOutEvent [重复]
【发布时间】:2014-01-16 06:53:37
【问题描述】:

我正在实现一个应用程序,其中我有 3 个QToolButton,当焦点位于任何QToolButton 上时,它应该是resize。 我的一位朋友给了我答案,但我无法弄清楚,因为我在 mainWindow 中也继承了QMainWindow 类。他说也要继承QToolButton。但是会出现多重继承问题。那么具体如何使用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
}

我必须合并的朋友的代码:

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);
}

【问题讨论】:

    标签: c++ qt qmainwindow qevent


    【解决方案1】:

    你应该这样做

    class YourButton : public QToolButton
    {
        Q_OBJECT
    
        protected:
    
        void focusInEvent(QFocusEvent* e);
        void focusOutEvent(QFocusEvent* e);
    };
    

    在 .cpp 文件中

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

    然后在主窗口中使用 yourButton 类。

    也(另一种选择)您可以在 mainWindow 中使用http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter

    【讨论】:

    • 它给出了这个错误:'YourButton' has not been declared
    • 您必须创建一个继承 QToolButton 的新类(例如,我将 YourButton 指定为 YourButton.h 和 YourButton.cpp)。然后在 .h 文件中覆盖受保护的: foucusInEvent() 并在 .cpp 文件中实现 YourButton 的大小调整。请参阅此链接答案stackoverflow.com/questions/2804115/qlineedit-focus-event。那么你必须在你的 mywindow 类中使用这个 YourButton。
    • 我正在为您展示如何创建自定义小部件,但无法找到合适的示例。您可以遵循 qt 示例中的模拟时钟、形状时钟,但相关性不大。如果这个论坛中的任何人可以帮助他..请做..
    • 我已经写了所有东西,但它不起作用。没有错误发生
    【解决方案2】:

    @Wagmare 的解决方案仅适用于布局之外的按钮。 要使其在布局内工作,它应该如下所示:

    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;
        }
    };
    

    如果有适当的尺寸政策,它也可以在没有布局的情况下工作。


    另一个没有子类的很酷的解决方案是样式表:
    QPushButton:focus {
        min-height: 40px
        min-width:  72px
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2018-05-22
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多