Namespaces
Variants
Actions
Revision as of 04:14, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:How to create a 2-state button using the Qt State Machine Framework

Jump to: navigation, search
Archived.png
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

This example shows how to use the Qt State Machine Framework to implement a simple state machine that toggles the current state when a button is clicked.

Article Metadata

Article
Created: chintandave_er (21 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Implementation

  1. construct a button and a state machine
    QPushButton button;
    QStateMachine machine;
  2. Create two states: on and off. assign Property and ObjectName of the each state
     QState *off = new QState();
    off->assignProperty(&button, "text", "Off");
    off->setObjectName("off");
     
    QState *on = new QState();
    on->setObjectName("on");
    on->assignProperty(&button, "text", "On");
  3. When the state machine is in the off state and the button is clicked, it will transition to the on state; when the state machine is in the on state and the button is clicked, it will transition to the off state.
    off->addTransition(&button, SIGNAL(clicked()), on);
    on->addTransition(&button, SIGNAL(clicked()), off);
  4. add states to the state machine
    machine.addState(off);
    machine.addState(on);
  5. set the Initial State
    machine.setInitialState(off);
    machine.start();
  6. show the button
     button.resize(100, 50);
    button.show();

Related articles

89 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved