Evgenii Legotckoi
Evgenii LegotckoiMarch 1, 2018, 2:58 a.m.

Android. Java vs Qt QML - Tutorial 003. Console messages and Toast pop-up messages

In the previous article , two buttons were created, which were responsible for changing the text in the text field. And now let's consider the option when we need to output information to the console about the button presses, or to display a pop-up message. That is, we modify the previous lesson.

In fact, this will be the very moment when there will be almost no additional differences in the code. In both cases, there are corresponding Classes / Types that are responsible for all this functionality.

Output to the console

In the case of Java, the Log class serves for this, and in the case of QML, the console functional is used for this, which is familiar to Web programmers.

Console messages have different levels of importance:

  • ERROR
  • WARN
  • NFO
  • DEBUG
  • VERBOSE

In the case of Java, this will be the following static methods of the Log class:

  • Log.v()
  • Log.d()
  • Log.i()
  • Log.w()
  • Log.e()

In the case of Qt QML, these are the console methods:

  • console.log
  • console.debug
  • console.info
  • console.warn
  • console.error

The console uses C ++ qCDebug, qCWarning, and so on. That is, within QML, these functions coming from JavaScript are translated into C ++ methods, which already serve to output information to the console.

Java

To output to the console in Java, you need to import the corresponding package with the Log class into the java file

import android.util.Log;

And call the necessary methods of this class

View.OnClickListener onClickListenerOkBtn = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "Button ОК"); 
    }
};

QML

To output to the console from QML code, you do not need to connect or import anything. You just need to call the necessary function in the right place of the code.

onClicked: {
    console.debug("Button Ok")
}

Pop-up messages

In Java under Android there is a special class for calling pop-up messages, which is called Toast. And only for this purpose it serves. While in QML for these purposes you can use the Type ToolTip, which can be used as well as the usual tooltip for interface elements. ToolTip can be instantiated and attached to various QML objects, also many QML graphic objects already have a ToolTip object, you just need to configure its behavior.

But in this lesson we will try to implement the standard Toast, which is present in Java Android.

This Toast in Java Android looked like this

While Toast in QML looked like this

The behavior and appearance of these pop-up messages are similar, and therefore we can now look at the program code.

Java

For Java, you need to import the Toast class

import android.widget.Toast;

and call a pop-up message in the correct part of the code

Toast.makeText(getApplicationContext(), "OK Button is pressed", Toast.LENGTH_LONG).show();

Little code has been obtained, and in this lesson Java somewhat wins by the amount of written code, because in QML we will try to bring the appearance and behavior of ToolTip QML to the appearance and behavior of Toast in Java, and this will require some customization.

QML

To use the ToolTip type, the QtQuick.Controls 2 module is imported

import QtQuick.Controls 2.3

Next, you need to add the ToolTip to the main application window, give it an id, and customize it.

ApplicationWindow {
    visible: true
    width: 360
    height: 520
    title: qsTr("QML Buttons")

    ToolTip {
        id: toast
        delay: 500
        timeout: 5000
        x: (parent.width - width) / 2
        y: (parent.height - 100)

        background: Rectangle {
            color: "gray"
            radius: 15
        }
    }

    // another code
}

And then already set the text to it and set the visible property to true.

onClicked: {
    toast.text = qsTr("OK Button is pressed")
    toast.visible = true
}

Conclusion

The output to the console is equivalent to Java and Qt QML.

Pop-up messages in Java Android by default win on ease of embedding in pop-up messages in Qt QML, but do not forget that QML ToolTip is a tooltip that has more advanced functionality than Toast in Java. For example, ToolTip can be used at the Slider object and move behind the slider, which is not provided by Toast. Also ToolTip is easier to customize.

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 14, 2024, 12:07 p.m.
Circuit switching and packet data transmission networks Angioedema 1 priligy dapoxetine
i
innorwallNov. 14, 2024, 11:42 a.m.
How to Copy Files in Linux If only females relatives with DZ offspring were considered these percentages were 23 order priligy online uk
i
innorwallNov. 14, 2024, 9:09 a.m.
Qt/C++ - Tutorial 068. Hello World using the CMAKE build system in CLion ditropan pristiq dosing With the Yankees leading, 4 3, Rivera jogged in from the bullpen to a standing ovation as he prepared for his final appearance in Chicago buy priligy pakistan
i
innorwallNov. 14, 2024, 4:05 a.m.
EVILEG-CORE. Using Google reCAPTCHA 2001; 98 29 34 priligy buy
i
innorwallNov. 14, 2024, 4 a.m.
PyQt5 - Lesson 007. Works with QML QtQuick (Signals and slots) priligy 30mg Am J Obstet Gynecol 171 1488 505
Now discuss on the forum
i
innorwallNov. 14, 2024, 3:39 a.m.
добавить qlineseries в функции priligy amazon canada 93 GREB1 protein GREB1 AB011147 6
i
innorwallNov. 11, 2024, 10:55 a.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 9:10 a.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks