简体   繁体   English

QStateMachine如何在不同的QState中显示和隐藏QGraphicsView和QObject

[英]QStateMachine how to show and hide QGraphicsView and QObject in different QState

this is my simple code: 这是我的简单代码:

I've created a new scene , view and QPixmapItem 我创建了一个新的sceneviewQPixmapItem

QGraphicsScene *scena = new QGraphicsScene();
QGraphicsPixmapItem *object1= new QGraphicsPixmapItem();
object1->setPixmap(QPixmap(":/prova/prova.png"));

QGraphicsView *view = new QGraphicsView();

view->setScene(scena);
scena->addItem(object1);

view->show();

and next I've created a new QStateMachine with two QState 和明年我创建了一个新的QStateMachine有两个QState

QStateMachine *machine = new QStateMachine();
QState *s1 = new QState();
QState *s2 = new QState();

machine -> addState(s1);
machine -> addState(s2);

//mouse click in a void mousePressEvent
s1 -> addTransition(this,SIGNAL(mouseclick()),s2);

machine -> start();
  1. I want to show the view in s1 and set object1 visible. 我想在s1显示view并将object1设置为可见。

  2. With a mouse click on the scene I've added a transition to s2 . 用鼠标点击场景我添加了转换到s2

  3. In s2 I want to hide only object1 . s2我想隐藏object1

How can I do this? 我怎样才能做到这一点? Someone can help me with a small tutorial? 有人可以帮我一个小教程吗?

I'm using Qt 5.6.0 with MinGW 4.9.2 32bit. 我正在使用Qt 5.6.0和MinGW 4.9.2 32bit。

Each QState has entered and exited signals that you can connect functors to. 每个QStateenteredexited可以连接QState函数的信号。 This is a common idiom in modern Qt 5 code. 这是现代Qt 5代码中常见的习语。 The functors can be concisely given using lambda expressions, where you can invoke arbitrary operations on non-objects, such as on the QPixmapItem . 可以使用lambda表达式简明地给出QPixmapItem函数,您可以在其中调用非对象上的任意操作,例如在QPixmapItem If QPixmapItem derived from QGraphicsObject , you could use QState::assignProperty to assign the desired visibility state instead of calling show() and hide() . 如果QPixmapItem派生自QGraphicsObject ,则可以使用QState::assignProperty分配所需的可见性状态,而不是调用show()hide()

Below is a complete example. 以下是一个完整的例子。

// https://github.com/KubaO/stackoverflown/tree/master/questions/scenestate-37684315
#include <QtWidgets>

void addTransition(QState * src, QObject * eventSource, QEvent::Type type, QAbstractState * dst)
{
   auto transition = new QEventTransition(eventSource, type);
   transition->setTargetState(dst);
   src->addTransition(transition);
}

struct Window : public QWidget {
   QHBoxLayout m_layout{this};
   QGraphicsScene m_scene;
   QGraphicsPixmapItem m_item;
   QGraphicsView m_view{&m_scene};

   QStateMachine m_mach;
   QState s1{&m_mach};
   QState s2{&m_mach};
   Window() {
      m_layout.addWidget(&m_view);
      QPixmap pix{128, 128};
      QPainter p{&pix};
      p.setBrush(Qt::white);
      p.drawRect(pix.rect().adjusted(0,0,-1,-1));
      p.drawText(pix.rect(), "Hello");
      m_item.setPixmap(pix);
      m_scene.addItem(&m_item);

      // I want to show the view in s1...
      s1.assignProperty(&m_view, "visible", true);
      // and set object1 visible.
      s1.connect(&s1, &QState::entered, [&]{ m_item.show(); });
      // With a mouse click on the scene I've added a transition to s2.
      addTransition(&s1, &m_view, QEvent::MouseButtonPress, &s2);
      // In s2 I want to hide only object1.
      s2.connect(&s2, &QState::entered, [&]{ m_item.hide(); });
      m_mach.setInitialState(&s1);
      m_mach.start();
   }
};

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   Window w;
   w.show();
   return app.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 QStateMachine它是如何工作的 - QStateMachine how it works QStateMachine时序(通过QState enter()信号显示非阻塞对话框) - QStateMachine timing (showing a non-blocking dialog via QState entered() signal) 为什么在QState的子类内嵌套QStateMachine并在构造函数中使用此指针导致外部状态机无法进行转换? - Why a nested QStateMachine inside subclass of QState with this pointer in constructor causes that an outer state machine can't make a transition? QT 和 OpenCV 问题查看 Opencv Mat 上的 QGraphicsView 在不同的线程:: QObject::killTimer: 计时器不能从另一个线程停止 - QT and OpenCV problem viewing Opencv Mat on QGraphicsView in different thread :: QObject::killTimer: Timers cannot be stopped from another thread 如何使用QStatemachine影响ListView? - How to use QStatemachine to influence a ListView? 如何刷新QGraphicsView以显示QGraphicsScene的背景更改 - How to refresh QGraphicsView to show changes in the QGraphicsScene's background QStateMachine如何执行状态转换动作? QSignalMapper? - How to implement QStateMachine state transition actions? QSignalMapper? 如何设置Qt QStateMachine动画持续时间 - How to set Qt QStateMachine animation duration 如何正确调整QGraphicsView的大小? - How to resize QGraphicsView properly? 如何在QGraphicsView上添加效果? - How to add an effect on QGraphicsView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM