본문 바로가기
Python

[PyQt6] Action in QML

by llHoYall 2022. 11. 6.

이번에는 다양한 활용이 가능한 Action에 대해서 다뤄보겠습니다.

어떤 동작을 수행시킬 때 사용할 수 있어요.

따라서, 메뉴나 툴바 등에서 활용하기 좋은 기능입니다.

Application 만드는 골격은 이전 포스팅을 참고해주세요.

여기에서는 QML을 중심으로 다루겠습니다.

2022.11.03 - [Python] - [PyQt6] Getting Started with QtQuick

Example with Action

// main.qml

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Controls.Material

ApplicationWindow {
  visible: true
  width: 640
  height: 480
  title: "Example App"

  Material.theme: Material.Dark
  Material.accent: Material.Orange

  Item {
    id: idItem
    focus: true

    property int count
  }

  RowLayout {
    anchors.centerIn: parent
    spacing: 20

    Button {
      id: idBtnDec
      action: idActDec
    }

    Label {
      id: idLabel
      text: idItem.count
      font.pixelSize: 24
    }

    Button {
      id: idBtnInc
      action: idActInc
    }
  }

  Action {
    id: idActDec
    text: qsTr("&Decrease")
    shortcut: "Ctrl+D"
    onTriggered: idItem.count--
  }

  Action {
    id: idActInc
    text: qsTr("&Increase")
    shortcut: "Ctrl+I"
    onTriggered: idItem.count++
  }
}

count 변수를 위한 Item을 하나 만들고, 1행의 layout을 위해 RowLayout을 놓았어요.

그 안에 감소 Button, Label, 증가 Button을 배치하였습니다.

그 후에 드디어 하이라이트인 Action이 나옵니다.

shortcut property를 사용하여 직접 실행을 할 수도 있고, Button등에 action property로 연동을 해서 사용을 할 수도 있습니다.

Button을 클릭하거나 단축키를 눌러서 동작시켜보세요.

Wrap Up

이번에는 간단하게 동작을 정의해서 사용할 수 있는 Action에 대해 알아보았습니다.

쉽게 사용할 수 있겠죠?

'Python' 카테고리의 다른 글

[PyQt6] Menu in QML  (0) 2022.11.10
[PyQt6] QML에서 Tool Bar 만들기  (0) 2022.11.06
[PyQt6] Shortcut in QML  (0) 2022.11.05
[PyQt6] Data 전달 with QML  (0) 2022.11.04
[PyQt6] Getting Started with QtQuick  (0) 2022.11.03

댓글