R
R
RST0012021-07-21 21:01:31
QML
RST001, 2021-07-21 21:01:31

How to change the color of the rectangle on each click of the mouse?

You need to change the color of the rectangle every time you click the mouse. A set of two to four colors. Here's my failed attempt:

import QtQml 2.3
import QtQml.Models 2.3
import QtQuick.Templates 2.2
import QtQuick 2.15
import QtQuick.Controls 2.15
import matrixfield 1.0

Rectangle {
    width: Constants.width
    height: Constants.height

    color: Constants.backgroundColor

    Grid {
        columns: 2;
        rows: 4;
        spacing: 2;
        Rectangle {
            color: "cyan";
            width: 15;
            height: 15;
            MouseArea {
                id: mouseArea1
                anchors.fill: parent
                onClicked: parent.color = 'red'

            }
            MouseArea {
                id: mouseArea2
                anchors.fill: parent
                onClicked: parent.color = 'cyan'

            }


        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2021-07-22
@RST001

Before you start writing code, try to understand why each element is needed separately, how it works, experiment with it, read the documentation. With such an approach as now, you will develop very slowly.
If the case, then you need a click counter and an array of colors, and there should be only one click area.
For the future, here is another good series of articles: https://habr.com/en/post/195706/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question