gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
QGraphicsScene的上下左右键会触发场景移动,如何屏蔽
- User
QGraphicsScene的上下左右键会触发场景移动,如何屏蔽
- Assistant
可以通过重写QGraphicsView中的keyPressEvent()函数来屏蔽上下左右键触发场景移动的功能。具体步骤如下:
- 创建一个自定义QGraphicsView类,继承于QGraphicsView。
- 在自定义的QGraphicsView类中重新实现keyPressEvent()函数。
- 在keyPressEvent()中判断按下的键是否是上下左右键,如果是则不调用基类的keyPressEvent()函数,从而屏蔽场景移动的功能。
下面给出一个示例代码:
class MyGraphicsView : public QGraphicsView { public: MyGraphicsView(QWidget* parent = nullptr) : QGraphicsView(parent) {} protected: void keyPressEvent(QKeyEvent* event) override { if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down || event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) { event->ignore(); } else { QGraphicsView::keyPressEvent(event); } } };其中,event->ignore()表示忽略该事件,不做处理。将该自定义QGraphicsView类应用到QGraphicsScene中即可。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.