Пример #1
0
/*!
  Adds a transition associated with the given \a signal of the given \a sender
  object, and returns the new QSignalTransition object. The transition has
  this state as the source, and the given \a target as the target state.
*/
QSignalTransition *QState::addTransition(QObject *sender, const char *signal,
                                         QAbstractState *target)
{
    if (!sender) {
        qWarning("QState::addTransition: sender cannot be null");
        return 0;
    }
    if (!signal) {
        qWarning("QState::addTransition: signal cannot be null");
        return 0;
    }
    if (!target) {
        qWarning("QState::addTransition: cannot add transition to null state");
        return 0;
    }
    int offset = (*signal == '0'+QSIGNAL_CODE) ? 1 : 0;
    const QMetaObject *meta = sender->metaObject();
    if (meta->indexOfSignal(signal+offset) == -1) {
        if (meta->indexOfSignal(QMetaObject::normalizedSignature(signal+offset)) == -1) {
            qWarning("QState::addTransition: no such signal %s::%s",
                     meta->className(), signal+offset);
            return 0;
        }
    }
    QSignalTransition *trans = new QSignalTransition(sender, signal);
    trans->setTargetState(target);
    addTransition(trans);
    return trans;
}
Пример #2
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
// Загружаем интерфейс пользователя из формы и устанавливаем действия в меню
    ui->setupUi(this);
    connect(ui->action_start, SIGNAL(triggered()), ui->startButton, SLOT(click()));
    connect(ui->action_exit, SIGNAL(triggered()), this, SLOT(close()));
    connect(ui->action_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    connect(ui->action_help, SIGNAL(triggered()), this, SLOT(showHelp()));
    connect(ui->action_about, SIGNAL(triggered()), this, SLOT(showAbout()));
    connect(ui->action_tech, SIGNAL(triggered()), this, SLOT(showTz()));
// Заводим машину состояний
    QStateMachine *animation = new QStateMachine(this);
    QState *idle = new QState();
    QState *animating = new QState();
    animating->assignProperty(ui->startButton,"text", tr("&Стоп"));
    animating->assignProperty(ui->startButton,"icon", QIcon(":/icons/control-stop-square.png"));
    animating->assignProperty(ui->action_start,"text",tr("О&становить анимацию"));
    animating->assignProperty(ui->action_start,"icon", QIcon(":/icons/control-stop-square.png"));
    idle->assignProperty(ui->startButton,"text", tr("Пу&ск"));
    idle->assignProperty(ui->startButton,"icon", QIcon(":/icons/control.png"));
    idle->assignProperty(ui->action_start,"text",tr("Запу&стить анимацию"));
    idle->assignProperty(ui->action_start,"icon", QIcon(":/icons/control.png"));
    QSignalTransition *startTransition = new QSignalTransition(ui->startButton, SIGNAL(clicked()), idle);
    startTransition->setTargetState(animating);
    QSignalTransition *stopTransition = new QSignalTransition(ui->startButton, SIGNAL(clicked()), animating);
    stopTransition->setTargetState(idle);
    QSignalTransition *doneTransition = new QSignalTransition(ui->widget, SIGNAL(animationStopped()), animating);
    doneTransition->setTargetState(idle);
    connect(startTransition, SIGNAL(triggered()), ui->widget, SLOT(startAnimation()));
    connect(stopTransition, SIGNAL(triggered()), ui->widget, SLOT(stopAnimation()));
    idle->addTransition(startTransition);
    animating->addTransition(stopTransition);
    animating->addTransition(doneTransition);
    animation->addState(idle);
    animation->addState(animating);
    animation->setInitialState(idle);
    animation->start();
 // В Linux мячик иногда сразу не отображается...
    ui->widget->updateGL();
}