×
Namespaces

Variants
Actions
(Difference between revisions)

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

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
Line 28: Line 28:
  
 
# construct a button and a state machine
 
# construct a button and a state machine
#:<code cpp>
+
#:<code cpp-qt>
 
QPushButton button;
 
QPushButton button;
 
QStateMachine machine;
 
QStateMachine machine;
 
</code>
 
</code>
 
# Create two states: on and off. assign Property and ObjectName of the each state
 
# Create two states: on and off. assign Property and ObjectName of the each state
#:<code cpp>
+
#:<code cpp-qt>
 
  QState *off = new QState();
 
  QState *off = new QState();
 
  off->assignProperty(&button, "text", "Off");
 
  off->assignProperty(&button, "text", "Off");
Line 43: Line 43:
 
</code>
 
</code>
 
# 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.
 
# 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.
#: <code cpp>
+
#: <code cpp-qt>
 
off->addTransition(&button, SIGNAL(clicked()), on);
 
off->addTransition(&button, SIGNAL(clicked()), on);
 
on->addTransition(&button, SIGNAL(clicked()), off);
 
on->addTransition(&button, SIGNAL(clicked()), off);
 
</code>
 
</code>
 
# add states to the state machine
 
# add states to the state machine
#: <code cpp>
+
#: <code cpp-qt>
 
machine.addState(off);
 
machine.addState(off);
 
machine.addState(on);
 
machine.addState(on);
 
</code>
 
</code>
 
# set the Initial State  
 
# set the Initial State  
#: <code cpp>
+
#: <code cpp-qt>
 
machine.setInitialState(off);
 
machine.setInitialState(off);
 
machine.start();
 
machine.start();
 
</code>
 
</code>
 
# show the button
 
# show the button
#:<code cpp>
+
#:<code cpp-qt>
 
  button.resize(100, 50);
 
  button.resize(100, 50);
 
  button.show();
 
  button.show();

Latest revision as of 04:14, 11 October 2012

Archived.pngArchived: 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
Compatibility
Platform(s):
Symbian
Article
Created: chintandave_er (21 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

[edit] 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();

[edit] Related articles

This page was last modified on 11 October 2012, at 04:14.
102 page views in the last 30 days.
×