- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用来自 here 的 SizeGripItem
(添加信号的最小修改)在 QGraphicsRectItem
的子类上定义如下:
//! RectItem that sends a boxChanged signal whenever it is moved or resized via the handles.
class QSignalingBoxItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
private:
void setSelected_(bool selected);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
public:
QSignalingBoxItem(QRectF rect, QGraphicsItem * parent = nullptr);
~QSignalingBoxItem();
signals:
void boxChanged(QRectF newBox);
};
namespace
{
class BoxResizer : public SizeGripItem::Resizer
{
public:
virtual void operator()(QGraphicsItem* item, const QRectF& rect)
{
QSignalingBoxItem* rectItem =
dynamic_cast<QSignalingBoxItem*>(item);
if (rectItem)
{
rectItem->setRect(rect);
}
}
};
}
QSignalingBoxItem::QSignalingBoxItem(QRectF rect, QGraphicsItem * parent) : QGraphicsRectItem(parent)
{
setFlags(QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemSendsScenePositionChanges);
}
QVariant QSignalingBoxItem::itemChange(GraphicsItemChange change, const QVariant & value)
{
switch (change)
{
case QGraphicsItem::ItemSelectedHasChanged:
setSelected_(value.toBool());
break;
case QGraphicsItem::ItemScenePositionHasChanged:
emit boxChanged(rect());
break;
default:
break;
}
return value;
}
void QSignalingBoxItem::setSelected_(bool selected)
{
//TODO: Test that it works as expected
if (selected)
{
auto sz = new SizeGripItem(new BoxResizer, this);
connect(sz, &SizeGripItem::resized, [&]() {emit boxChanged(rect()); });
}
else
{
// Get the child
// If it's a SizeGripItem, delete it.
if (childItems().length() > 0)
{
foreach(QGraphicsItem* item, childItems())
{
auto sz = dynamic_cast<SizeGripItem*>(item);
if (sz)
{
delete sz;
break;
}
}
}
}
}
如果我在构造函数中应用 SizeGripItem
,该类将按预期工作。但是,我需要 handle 可见并且仅在选择项目时才起作用。但是,当我运行上面的代码时, handle 会显示出来,但只要单击 handle ,就会移动整个框(就像我在框中间单击一样),而不是调整它的大小。
使用调试器运行时,我发现 itemChange
甚至没有被调用。为什么会这样?选择项目时,我需要如何修改类才能使其正常工作?
这就是类的使用方式。我有一个 QGraphicsView
的扩展及其私有(private) m_scene
,我在其中覆盖 mouse{Press,Move,Release}Event
以生成一个框“单击并拖动”。撇开类的其余部分不谈,因为涉及 QSignalingBoxItem
的唯一部分是这三个函数,所以我添加了它们。如果需要更多,我会根据需要添加。
void QSampleEditor::mousePressEvent(QMouseEvent * mouseEvent)
{
QImageView::mousePressEvent(mouseEvent);
if (mouseEvent->buttons() == Qt::RightButton && m_currentSample && m_activePixmap && m_activePixmap->isUnderMouse())
{
m_dragStart = mapToScene(mouseEvent->pos());
m_currentBox = new QSignalingBoxItem({ m_dragStart, QSize(0,0) });
m_scene.addItem(m_currentBox);
mouseEvent->accept();
}
}
void QSampleEditor::mouseMoveEvent(QMouseEvent * mouseEvent)
{
QImageView::mouseMoveEvent(mouseEvent);
if (m_currentBox)
{
m_currentBox->setRect(rectFromTwoPoints(m_dragStart, mapToScene(mouseEvent->pos())));
mouseEvent->accept();
}
}
void QSampleEditor::mouseReleaseEvent(QMouseEvent * mouseEvent)
{
//TODO: Add checks that the sample is still there.
QImageView::mouseReleaseEvent(mouseEvent);
if (m_currentBox)
{
// Add to the StringSample
auto new_char = new CharacterSample;
connect(m_currentBox, &QSignalingBoxItem::boxChanged, new_char, &CharacterSample::boxChanged);
new_char->boxChanged(m_currentBox->rect());
m_currentSample->addCharacter(new_char);
m_currentBox = nullptr;
mouseEvent->accept();
}
}
最佳答案
当给定项目选择了其任何可移动祖先项目时,选定的最年长的祖先将接收事件 - 即使该项目的位置与子项目重叠。这在 QGraphicsItem::mouseMoveEvent
中处理- 如果存在可移动的祖先,则不处理移动事件。最年长的可移动选定祖先接收事件并使用它移动自身,但后代项目忽略它(这里 - 句柄!)。
关于代码(您的和您正在重用的代码)的一般说明:
以Q
开头的类型名是为Qt保留的。除非您已将 Qt 放入 namespace ,否则不应使用此类名称。
SizeGripItem
应该被标记为没有内容 - 因为它的 paint
方法是空操作。
通过 const 引用传递非数字、非指针方法参数除非该方法需要一个拷贝以在内部进行修改。
SizeGripItem
需要有一个 Resizer
或发出一个信号,而不是两者 - 这两个选项是互斥的。
事实上,Resizer
是 Qt 4 的遗迹,其中的插槽非常冗长,并且无法连接到 lambda。在 Qt 5 中完全没有必要,其中信号可以连接到任何仿函数,包括 Resizer
类型的仿函数 - 从而使 QObject::connect
隐式向后 -与显式使用 Resizer
(!) 兼容。
可以提出以下解决方案:
SizeGripItem
本身可移动。 SignalingBoxItem
不能移动。SizeGripItem::HandleItem
的 mouseMoveEvent
以接受相关事件并对它们使用react。 SignalingBoxItem
保持可移动。SizeGripItem
成为其 HandleItem
的事件过滤器,并按照解决方案 #2 处理相关事件。 SignalingBoxItem
保持可移动。SizeGripItem
成为 SignalingBoxItem
的兄弟项。 SignalingBoxItem
然后可以移动,而不会影响使用 SizeGripItem
的句柄。当然,那时 SignalingBoxItem
的祖先都不能移动。以下是大约 240 行的完整示例,实现了解决方案 1-3。每个解决方案都在一个条件 block 中描述,并且该示例在启用它们的任何子集的情况下进行编译。没有选择解决方案,原来的问题仍然存在。可以在运行时选择启用的解决方案。
首先,让我们从 SizeGripItem
开始:
// https://github.com/KubaO/stackoverflown/tree/master/questions/graphicsscene-children-51596611
#include <QtWidgets>
#include <array>
#define SOLUTION(s) ((!!(s)) << (s))
#define HAS_SOLUTION(s) (!!(SOLUTIONS & SOLUTION(s)))
#define SOLUTIONS (SOLUTION(1) | SOLUTION(2) | SOLUTION(3))
class SizeGripItem : public QGraphicsObject {
Q_OBJECT
enum { kMoveInHandle, kInitialPos, kPressPos };
struct HandleItem : QGraphicsRectItem {
HandleItem() : QGraphicsRectItem(-4, -4, 8, 8) {
setBrush(Qt::lightGray);
setFlags(ItemIsMovable | ItemSendsGeometryChanges);
}
SizeGripItem *parent() const { return static_cast<SizeGripItem *>(parentItem()); }
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == ItemPositionHasChanged) parent()->handleMoved(this);
return value;
}
#if HAS_SOLUTION(2)
bool sceneEvent(QEvent *event) override {
return (data(kMoveInHandle).toBool() && hasSelectedMovableAncestor(this) &&
processMove(this, event)) ||
QGraphicsRectItem::sceneEvent(event);
}
#endif
};
#if HAS_SOLUTION(2) || HAS_SOLUTION(3)
static bool processMove(QGraphicsItem *item, QEvent *ev) {
auto mev = static_cast<QGraphicsSceneMouseEvent *>(ev);
if (ev->type() == QEvent::GraphicsSceneMousePress &&
mev->button() == Qt::LeftButton) {
item->setData(kInitialPos, item->pos());
item->setData(kPressPos, item->mapToParent(mev->pos()));
return true;
} else if (ev->type() == QEvent::GraphicsSceneMouseMove &&
mev->buttons() == Qt::LeftButton) {
auto delta = item->mapToParent(mev->pos()) - item->data(kPressPos).toPointF();
item->setPos(item->data(kInitialPos).toPointF() + delta);
return true;
}
return false;
}
static bool hasSelectedMovableAncestor(const QGraphicsItem *item) {
auto *p = item->parentItem();
return p && ((p->isSelected() && (p->flags() & QGraphicsItem::ItemIsMovable)) ||
hasSelectedMovableAncestor(p));
}
#endif
std::array<HandleItem, 4> handles_;
QRectF rect_;
void updateHandleItemPositions() {
static auto get = {&QRectF::topLeft, &QRectF::topRight, &QRectF::bottomLeft,
&QRectF::bottomRight};
for (auto &h : handles_) h.setPos((rect_.*get.begin()[index(&h)])());
}
int index(HandleItem *handle) const { return handle - &handles_[0]; }
void handleMoved(HandleItem *handle) {
static auto set = {&QRectF::setTopLeft, &QRectF::setTopRight,
&QRectF::setBottomLeft, &QRectF::setBottomRight};
auto rect = rect_;
(rect.*set.begin()[index(handle)])(handle->pos());
setRect(mapRectToParent(rect.normalized()));
}
public:
SizeGripItem(QGraphicsItem *parent = {}) : QGraphicsObject(parent) {
for (auto &h : handles_) h.setParentItem(this);
setFlags(ItemHasNoContents);
}
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == QGraphicsItem::ItemPositionHasChanged) resize();
return value;
}
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override {}
QRectF boundingRect() const override { return rect_; }
void setRect(const QRectF &rect) {
rect_ = mapRectFromParent(rect);
resize();
updateHandleItemPositions();
}
void resize() { emit rectChanged(mapRectToParent(rect_), parentItem()); }
Q_SIGNAL void rectChanged(const QRectF &, QGraphicsItem *);
#if SOLUTIONS
void selectSolution(int i) {
#if HAS_SOLUTION(1)
setFlag(ItemIsMovable, i == 1);
setFlag(ItemSendsGeometryChanges, i == 1);
if (i != 1) {
auto rect = mapRectToParent(rect_);
setPos({}); // reset position if we're leaving the movable mode
setRect(rect);
}
i--;
#endif
for (auto &h : handles_) {
int ii = i;
#if HAS_SOLUTION(2)
h.setData(kMoveInHandle, ii-- == 1);
#endif
#if HAS_SOLUTION(3)
if (ii == 1)
h.installSceneEventFilter(this);
else
h.removeSceneEventFilter(this);
#endif
}
}
#endif
#if HAS_SOLUTION(3)
bool sceneEventFilter(QGraphicsItem *item, QEvent *ev) override {
if (hasSelectedMovableAncestor(item)) return processMove(item, ev);
return false;
}
#endif
};
然后,SignalingBoxItem
:
class SignalingBoxItem : public QObject, public QGraphicsRectItem {
Q_OBJECT
SizeGripItem m_sizeGrip{this};
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override {
if (change == QGraphicsItem::ItemSelectedHasChanged)
m_sizeGrip.setVisible(value.toBool());
else if (change == QGraphicsItem::ItemScenePositionHasChanged)
emitRectChanged();
return value;
}
void emitRectChanged() { emit rectChanged(mapRectToScene(rect())); }
void setRectImpl(const QRectF &rect) {
QGraphicsRectItem::setRect(rect);
emitRectChanged();
}
public:
SignalingBoxItem(const QRectF &rect = {}, QGraphicsItem *parent = {})
: QGraphicsRectItem(rect, parent) {
setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsScenePositionChanges);
m_sizeGrip.hide();
connect(&m_sizeGrip, &SizeGripItem::rectChanged, this,
&SignalingBoxItem::setRectImpl);
}
void setRect(const QRectF &rect) {
setSelected(false);
m_sizeGrip.setRect(rect);
setRectImpl(rect);
}
Q_SIGNAL void rectChanged(const QRectF &); // Rectangle in scene coordinates
#if SOLUTIONS
void selectSolution(int index) {
setFlag(ItemIsMovable, !HAS_SOLUTION(1) || index != 1);
m_sizeGrip.selectSolution(index);
}
#endif
};
SampleEditor
:
class SampleEditor : public QGraphicsView {
Q_OBJECT
bool m_activeDrag = false;
SignalingBoxItem m_box;
QPointF m_dragStart;
public:
SampleEditor(QGraphicsScene *scene) : QGraphicsView(scene) {
scene->addItem(&m_box);
connect(&m_box, &SignalingBoxItem::rectChanged, this, &SampleEditor::rectChanged);
}
Q_SIGNAL void rectChanged(const QRectF &);
void mousePressEvent(QMouseEvent *event) override {
QGraphicsView::mousePressEvent(event);
if (event->button() == Qt::RightButton) {
m_dragStart = m_box.mapFromScene(mapToScene(event->pos()));
m_activeDrag = true;
m_box.show();
m_box.setRect({m_dragStart, m_dragStart});
event->accept();
}
}
void mouseMoveEvent(QMouseEvent *event) override {
QGraphicsView::mouseMoveEvent(event);
if (m_activeDrag) {
m_box.setRect({m_dragStart, m_box.mapFromScene(mapToScene(event->pos()))});
event->accept();
}
}
void mouseReleaseEvent(QMouseEvent *event) override {
QGraphicsView::mouseReleaseEvent(event);
if (m_activeDrag && event->button() == Qt::RightButton) {
event->accept();
m_activeDrag = false;
}
}
void resizeEvent(QResizeEvent *event) override {
QGraphicsView::resizeEvent(event);
scene()->setSceneRect(contentsRect());
}
#if SOLUTIONS
void selectSolution(int index) { m_box.selectSolution(index); }
#endif
};
最后,演示代码:
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QWidget ui;
QGridLayout layout{&ui};
QGraphicsScene scene;
SampleEditor editor(&scene);
QComboBox sel;
QLabel status;
layout.addWidget(&editor, 0, 0, 1, 2);
layout.addWidget(&sel, 1, 0);
layout.addWidget(&status, 1, 1);
sel.addItems({
"Original (Movable SignalingBoxItem)",
#if HAS_SOLUTION(1)
"Movable SizeGripItem",
#endif
#if HAS_SOLUTION(2)
"Reimplemented HandleItem",
#endif
#if HAS_SOLUTION(3)
"Filtering SizeGripItem",
#endif
});
sel.setCurrentIndex(-1);
#if SOLUTIONS
QObject::connect(&sel, QOverload<int>::of(&QComboBox::currentIndexChanged),
[&](int index) { editor.selectSolution(index); });
#endif
QObject::connect(&editor, &SampleEditor::rectChanged, &status,
[&](const QRectF &rect) {
QString s;
QDebug(&s) << rect;
status.setText(s);
});
sel.setCurrentIndex((sel.count() > 1) ? 1 : 0);
ui.setMinimumSize(640, 480);
ui.show();
return a.exec();
}
#include "main.moc"
关于c++ - 如果选择了项目,为什么 QGraphicsItem 子级不再单击鼠标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596611/
我在选项卡中有我的网络应用程序的选项。 aaa bbb ccc ddd eee 当用户单击任何选项卡(在同一窗口中)时,我会使用此代码获得淡出效果,然后自动
如何获得一个简单的调用单击来实现如下功能:http://jsfiddle.net/ftwPS/6/我显然错过了一些基本的东西,因为当您单击“CLICK”时这不起作用。 $('tr').click(fu
我有一个列表,在左侧显示一个插入符号图标,单击它时应该更改图标。每当我单击一个图标时,所有图标都会更改。 detailedInfo = []; subordinateInfo; openSu
我有一个 div,我想对其进行设置,以便当我单击其他内容时,它会隐藏该 div。 我也是这样 $('body').click(function(){ if(loginOpened) {
我有很多装有不同内容的盒子。 如果单击第一个框,您可以看到一个包含更多详细信息的弹出框。如果单击第二个、第三个等框,您必须查看这些框的详细信息。 我的问题:如果我点击框号。 2 我看到了盒子号的详细信
当我点击 .add-btn 时,我将 .add-btn 的样式更改为 background:#cccccc 并将 val() 更改为“-”。 现在当我用 tr td .list 删除添加的文本时,它是
感谢 SO 的出色贡献者!当您开始了解 jQuery 时,它会变得更酷。 :) 所以我有一个 LI,单击时会显示/隐藏子 UL。我想做的是能够单击 LI 内的链接,打开一个空白窗口,但也不会关闭子 U
我有这份文档,它使我能够获得一份带有点击进度的多项选择选择调查问卷。 如何用图像代替此处的文字? (并且仍然有这样的机制,一旦单击[图像],就会提出下一个问题) 我已经尝试使用 UL/H1 设置背景图
我想在 JQGrid 的刷新按钮单击上编写代码。有什么事件吗? 最佳答案 如果您需要在开始刷新之前执行一些操作,您应该使用 beforeRefresh打回来: $("#grid_id").jqGrid
问题是将对象或多个参数从模板传递到组件,并使用它们将数据添加到 API。 任务.service.ts addTasks(task: Task): Observable{ let headers =
我有一个像这样的primefaces选项卡 View : This tab has static content. this t
我在 jquery 中有一个有效的 a.click() 函数...但是如果我单击一个 anchor ,我会打开一个新窗口...但是我怎样才能阻止浏览器本身打开一个新窗口? 示例: $('a')
有没有简单的方法来创建代码:如果 URL 更改或单击链接显示 div(例如加载 gif 3 秒),则显示页面?有点像空白的白色页面,加载 gif 旋转 3 秒然后显示页面? 谢谢! 最佳答案 给定 G
我需要知道此时按钮的状态是否被点击? 谢谢 最佳答案 if (myButton.state & UIControlStateHighlighted) { // Do your stuff the
我正在 NSImageView 上绘制一条 NSBezierPath 线。我正在创建 NSBezierPath 对象,设置 moveToPoint,设置 lineToPoint,设置 setLineW
我的 Selenium 代码存在问题,无法正确执行按键 + 单击操作。 测试应打开 jqueryui.com 链接并选择页面上的前 2 个 li 元素。 我正在使用 Selenium 2.23 和 F
单击时我将更改字符串一部分的样式。例如“TEXT”,然后单击“T”,之后它会将样式从黑色更改为红色,仅 T在我的代码中,当我单击文本时,我拆分文本并保留在“split”数组中,它将调用handleCl
我在网站上有一个 anchor 。当有人点击它时,我在 jquery 中执行一些操作并更改名称,但是当再次单击它时,事件被触发,尽管我已经更改了它的名称。代码在这里: $(".like_cont a[
我有一个下载链接Download我希望每次当有人点击“下载”时,我都可以将其插入数据库total_downloads+1。为了插入数据库,我通常使用 然后 if (isset($_POST['d
我是一名优秀的程序员,十分优秀!