Tutorial · Chapter 2

First Project — Logging from a Button Click

Create a new project and complete the simplest possible example: place one button on the screen, and when it is pressed, print a log 10 times using a for loop. By following this process you will learn the core development flow of QMachineStudio all at once — lay out the screen → connect the event to a function → write code in the function → run → check the log.

Note QMachineStudio does not work by editing text files directly; you add functions and steps in the Solution Explorer tree and edit only the code of the selected item. If this concept is unfamiliar, read the "Development Approach" section of Getting Started first.

1. Create the Project

  1. Choose File → New, or press New Project on the Welcome page.
  2. In the NEW PROJECT DIALOG, enter FirstProject in Project Name and the folder to hold the project (e.g. D:\RUN) in Project Path, then press OK. You can also pick the folder with the ... button.
  3. A FirstProject folder is created under Project Path, filled from the default template, and the new project opens right away. If another project was open, it is closed first and you are asked whether to save it.

The Solution Explorer of a new project contains the modules below. There is no run module (sequence) yet.

GroupModuleRole
System ModulesMon · Event · Data · InitMonitoring · common event functions · variables · machine initialization
View ModulesViewMainMain window of the machine screen (MainView of the project options)
View ModulesViewRunRun page shown in BasePanel of the main window when the machine screen opens (START · STOP · RESET buttons)

In this example you place the button on ViewRun, the screen you see as soon as the machine screen opens.

2. Add a Button to the Screen

  1. In the Solution Explorer, double-click View Modules → ViewRun → Design to open the screen designer (View Module).
  2. Drag Button from the Button group of the ToolBox at the bottom left of the designer onto the screen. You can find the available control types in the UI Controls Reference.
  3. With the button selected, enter the display text (e.g. Run Log) in Caption and the name (e.g. btnRunLog) in Name of the Property Editor at the top left.
Screen designer — Property Editor at the top left, ToolBox at the bottom left (ViewMain of SequenceSample shown)
Screen designer — Property Editor at the top left, ToolBox at the bottom left (ViewMain of SequenceSample shown)

3. Connect the Button Click to a Function

The action to run when the button is pressed is written as a function, and that function is connected to the button's click event (OnClickEvent). For a button click to call a function, the button's Action property must be RunEvent.

  1. Double-click the button in the designer.
  2. If Action is the default None or RunEvent, QMachineStudio creates a click event function under Functions of ViewRun, puts its name into OnClickEvent and changes Action to RunEvent. The function's editing tab opens right away.
  3. When OnClickEvent is empty, the function is named ButtonName_OnClickEvent. In this example btnRunLog_OnClickEvent is created. To choose the name yourself, enter it (e.g. OnRunLogClick) in the OnClickEvent property before double-clicking.
  4. The function is added as a node under ViewRun → Functions in the Solution Explorer. Later you can right-click the node to Rename Function · Delete Function. When you rename it, update the button's OnClickEvent as well.

Caution If Action has another value such as ShowPage, the click is handled as that action and the OnClickEvent function is not called. In that case double-clicking does not change Action and only shows a notice (RunFunction · RunManual open the specified function in the editor). Screen events can call only functions of the same view module, so the event function must be in ViewRun.

When called, the event function receives string sender (control name), int tag (the control's TagIndex) and array params. This example does not use the arguments.

4. Write the Log with a for Loop

In the open function tab, add code above return true;. Declare a variable i and use for to repeat from 1 to 10, leaving one log line each time. The finished function looks like this.

xscript
FUNCTION bool btnRunLog_OnClickEvent(string sender, int tag , array params)
{
   // ==========================================================================
   // Module : 'ViewRun'
   // ( header comment inserted automatically )
   // ==========================================================================
 
   int i;
 
   for(i, 1, 10)
   {
      Log("count = {0}", i);
   }
 
   return true;
}
  • for(variable, start, end) is a range-based loop that includes the end value. The code above runs 10 times with i going from 1 to 10. See Control Flow for the full syntax.
  • Log("...{0}...", value) outputs a log, and the trailing value is inserted at {0}. The interpolated form Log($"count = {i}") works the same. Here 10 lines are output, from count = 1 to count = 10.
  • New functions are created with a bool return type, so keep the final return true;.

5. Build and Run

  1. Save and build with Build → Save All & Make (F6). If there are errors, the Error List tab at the bottom opens automatically, and double-clicking an item jumps to its location. (For the bottom pane, see Output · Error · Search.)
  2. Build → Run (F5) saves, builds and switches to the machine screen. The ViewRun screen appears inside the main window (ViewMain).
  3. Press the Run Log button on the screen.

To return from the machine screen to the development screen, log in as Developer and press Ctrl+F5. The machine screen and simulation mode are described in Run Modes.

Note When testing on a PC without motor or IO boards, turn on Simulation mode on the Options tab of the project options; IO and motors then run as virtual devices.

6. Check the Log with Log Manager

Pressing the button runs Log("count = {0}", i) and leaves 10 log lines. QMachineStudio sends runtime logs to the companion tool ICT Log Manager, so Log Manager must be running before you press the button. Logs produced while Log Manager is not running are not kept.

  1. Start Log Manager. On the machine screen of a new project, the LOG button (Action ShowLogManager) brings the running Log Manager window to the front. (For details, see Log Manager Overview and Live Logs · Opening Files.)
  2. If you see 10 lines in order from count = 1 to count = 10 in the Live tab, you have succeeded.
  3. When there are many lines, search for count with Log Manager's Find · Bookmarks · Filter Bar.

What You Learned

  • How to create a new project and what the default modules (System Modules · ViewMain · ViewRun) do
  • How to place a control on a view module and create and connect a click event function by double-clicking the button (Action = RunEvent)
  • The QMachineStudio approach of managing functions as nodes in the tree and editing only that node's code
  • The range-based for(i, 1, 10) loop and Log("...{0}...", value) output
  • How to check runtime logs with Log Manager

Next Steps

Subscribe