[英]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
我创建了一个新的
scene
, view
和QPixmapItem
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();
I want to show the view
in s1
and set object1
visible. 我想在
s1
显示view
并将object1
设置为可见。
With a mouse click on the scene I've added a transition to s2
. 用鼠标点击场景我添加了转换到
s2
。
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. 每个
QState
都entered
和exited
可以连接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.