gpt4 book ai didi

c++ - Qt5/C++ 在 mousePressEvent 期间释放鼠标

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:40 24 4
gpt4 key购买 nike

我有一个 QGridLayout,其中每个单元格都包含我的自定义小部件 QFrameExtended,定义如下:

在 .h 中:

#ifndef QFRAMEEXTENDED_H
#define QFRAMEEXTENDED_H

#include <QObject>
#include <QFrame>

class QFrameExtended : public QFrame
{
Q_OBJECT

private:

public:
int id;
explicit QFrameExtended(QWidget *parent = 0);

signals:
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
void pressed(QFrameExtended *);
void released(QFrameExtended *);

public slots:
void on_mouse_press();
void on_mouse_release();
};

#endif // QFRAMEEXTENDED_H

在 .cpp 中:

#include "qframe_extended.h"

QFrameExtended::QFrameExtended(QWidget *parent) : QFrame(parent)
{
this->id = /* Imagine here there is a function to generate an id */ ;
connect( this, SIGNAL( mousePressEvent(QMouseEvent*) ), this, SLOT( on_mouse_press() ) );
connect( this, SIGNAL( mouseReleaseEvent(QMouseEvent*) ), this, SLOT( on_mouse_release() ) );
}

void QFrameExtended::on_mouse_press() {
emit pressed(this);
}

void QFrameExtended::on_mouse_release() {
emit released(this);
}

我的表单使用 QFrameExtended 小部件创建了 QGridLayout 并为它们中的每一个定义了一个事件处理程序:

/* ... */
/* This snippet of code is inside a loop that is creating frame objects */
connect(frame, &QFrameExtended::pressed, this, &MyForm::on_mouse_press);
connect(frame, &QFrameExtended::released, this, &MyForm::on_mouse_release);
/* ... */

最后是事件处理程序:

void MyForm::on_mouse_press(QFrameExtended *frame) {
qDebug() << "Pressed here: " << frame->id << endl;
}

void MyForm::on_mouse_release(QFrameExtended *frame) {
qDebug() << "Released here: " << frame->id << endl;
}

当我在没有释放按钮的情况下单击一个单元格(即 QFrameExtended 小部件)时,我会在控制台上看到单元格的 ID。在我将鼠标移到另一个单元格上后,当我释放按钮时,我会看到打印的第二个 ID。

一个例子是这样的输出:

在这里按下:1

在这里发布:3

但实际情况是,当我在 QFrameExtended 上按下鼠标按钮时,他开始抓取所有鼠标事件,直到我松开按钮。这是预期的行为:

Qt automatically grabs the mouse when a mouse button is pressed inside a widget; the widget will continue to receive mouse events until the last mouse button is released.

发件人:http://doc.qt.io/qt-4.8/qmouseevent.html

我怎样才能改变这种行为?如果你也能给我一个例子,我将不胜感激

最佳答案

好的,我按照peppe的建议解决了。我尝试扩展 QGridLayout 但布局不支持鼠标事件,因为不继承自 QWidget。因此,我扩展了包含布局的 QWidget

在 .h 中:

#ifndef QWIDGETEXTENDED_H
#define QWIDGETEXTENDED_H

#include <QWidget>
#include <QString>
#include <QMouseEvent>
#include "qframe_extended.h"

class QWidgetExtended : public QWidget
{
Q_OBJECT
public:
explicit QWidgetExtended(QWidget *parent = 0);

protected:
virtual void mousePressEvent(QMouseEvent *);
virtual void mouseReleaseEvent(QMouseEvent *);

signals:
void pressed(QFrameExtended *);
void released(QFrameExtended *);

};

#endif

在 .cpp 中:

#include "qwidget_extended.h"
#include "qframe_extended.h"

QWidgetExtended::QWidgetExtended(QWidget *parent) : QWidget(parent)
{

}

void QWidgetExtended::mousePressEvent(QMouseEvent *event) {
QFrameExtended frame;
QWidget *widget = this->childAt(event->pos());
if (widget != NULL) {
QString widgetClassName(widget->metaObject()->className());
//I don't use explicitly the string because if one day someone changes the name of the class, the compiler will output an error
QString className(frame.metaObject()->className());
if (widgetClassName == className) {
emit pressed(dynamic_cast<QFrameExtended*> (widget));
}
}
}

void QWidgetExtended::mouseReleaseEvent(QMouseEvent *event) {
QFrameExtended frame;
QWidget *widget = this->childAt(event->pos());
if (widget != NULL) {
QString widgetClassName(widget->metaObject()->className());
//I don't use explicitly the string because if one day someone changes the name of the class, the compiler will output an error
QString className(frame.metaObject()->className());
if (widgetClassName == className) {
emit released(dynamic_cast<QFrameExtended*> (widget));
}
}
}

关于c++ - Qt5/C++ 在 mousePressEvent 期间释放鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com