鼠标按下事件和 qtabwidget

发布于 2024-11-02 13:57:33 字数 40 浏览 1 评论 0原文

需要当鼠标(带有按钮)位于选项卡顶部时,它会被激活。 怎么可能呢。

need that when a mouse ( with press button ) is on top of tab, it gets activated.
How is it possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-11-09 13:57:33

您可以拦截来自 QTabBar 的 mouseMoveEvent 事件。

编辑:如何

要拦截它,Qt 为您提供了强大的功能。您可以在创建的小部件上安装事件过滤器,以便它将能够获取其子小部件的过滤事件。请参阅有关 installEventFilter 函数的文档。

You can intercept the mouseMoveEvent event from the QTabBar.

Edit: How To

To intercept it Qt offers you a great functionalities. You can install an event filter on a widget you have created so it will be able to get filtered event of its child widgets. See documentation about the installEventFilter function.

第几種人 2024-11-09 13:57:33

只需创建您自己的事件过滤器即可。

// TabBarEventHandler
class TabBarEventHandler : public QObject
{
public:
    explicit TabBarEventHandler( QTabWidget * tabWidget );
    virtual ~TabBarEventHandler();

    void Initialize();

protected:
    QTabWidget  * mTabWidget;

    virtual bool eventFilter( QObject* obj, QEvent* event ) override;
};


// TabBarEventHandler
TabBarEventHandler::TabBarEventHandler( TabBar * tabWidget ) 
    : mTabWidget( tabWidget )
{
}

TabBarEventHandler::~TabBarEventHandler()
{
    mTabWidget->tabBar()->removeEventFilter( this );
}

void TabBarEventHandler::Initialize()
{
    mTabWidget->tabBar()->installEventFilter( this );
}

bool TabBarEventHandler::eventFilter(QObject* obj, QEvent* event)
{
    qDebug() << event->type();

    return QObject::eventFilter( obj, event );
}

void TabBarEventHandler::timerEvent(QTimerEvent* event)
{
}

在你的小部件中:

MyTabWidget::MyTabWidget( QWidget * parent ) : QTabWidget( parent )
{
    mEventHandler = new TabBarEventHandler( this );
    mEventHandler->Initialize();
}

MyTabWidget::~MyTabWidget()
{
    delete mEventHandler;
}

调试输出:

QEvent::Type(HoverMove) QEvent::Type(HoverMove)
QEvent::Type(MouseButtonPress) QEvent::Type(InputMethodQuery)
QEvent::Type(InputMethodQuery) QEvent::Type(InputMethodQuery)
QEvent::类型(绘画) QEvent::类型(绘画)
QEvent::Type(MouseButtonRelease) QEvent::Type(HoverMove)
QEvent::Type(HoverMove) QEvent::Type(Leave)

Just create your own EventFilter.

// TabBarEventHandler
class TabBarEventHandler : public QObject
{
public:
    explicit TabBarEventHandler( QTabWidget * tabWidget );
    virtual ~TabBarEventHandler();

    void Initialize();

protected:
    QTabWidget  * mTabWidget;

    virtual bool eventFilter( QObject* obj, QEvent* event ) override;
};


// TabBarEventHandler
TabBarEventHandler::TabBarEventHandler( TabBar * tabWidget ) 
    : mTabWidget( tabWidget )
{
}

TabBarEventHandler::~TabBarEventHandler()
{
    mTabWidget->tabBar()->removeEventFilter( this );
}

void TabBarEventHandler::Initialize()
{
    mTabWidget->tabBar()->installEventFilter( this );
}

bool TabBarEventHandler::eventFilter(QObject* obj, QEvent* event)
{
    qDebug() << event->type();

    return QObject::eventFilter( obj, event );
}

void TabBarEventHandler::timerEvent(QTimerEvent* event)
{
}

And in your widget:

MyTabWidget::MyTabWidget( QWidget * parent ) : QTabWidget( parent )
{
    mEventHandler = new TabBarEventHandler( this );
    mEventHandler->Initialize();
}

MyTabWidget::~MyTabWidget()
{
    delete mEventHandler;
}

The debug output:

QEvent::Type(HoverMove) QEvent::Type(HoverMove)
QEvent::Type(MouseButtonPress) QEvent::Type(InputMethodQuery)
QEvent::Type(InputMethodQuery) QEvent::Type(InputMethodQuery)
QEvent::Type(Paint) QEvent::Type(Paint)
QEvent::Type(MouseButtonRelease) QEvent::Type(HoverMove)
QEvent::Type(HoverMove) QEvent::Type(Leave)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文