Evgenii Legotckoi
Oct. 13, 2015, 9:37 p.m.

QML - Lesson 003. Custom Dialog in QML Android

Once we made customized buttons in the previous lesson , it is time to make the Custom Dialog, which will look more natively for Android devices, and can even look like the design on the IOS device. In any case, you will be able to more fully come to the realization of these devices guides.

To create a dialog object to be used Dialog of QtQuick.Dialog library. Nuance of A when working with custom dialog for Android is that the Standard Buttons that adequately look at the development of a desktop, for they look bad for Android, and more difficult to customize them for a beginner developer. The easiest way to implement your own buttons with their own stylization.

Development of Custom Dialog

Development of customized dialogue will continue on the basis of the project from the previous lesson. There we have two Custom Button have been created that we have a little tweak in color, so they looked concisely in relation to the dialogue. Also, using the technique in their styling, Styling dialog box button, which will be two buttons: "OK" and "Cancel". These buttons will close the dialog.

Dialog buttons will need to arrive at the bottom of the dialog, and the rest give a message "Hello, World !!!", as well as the section's button gray line with each other, and the same line will separate the buttons on the message. It will seem a bit on the IOS dialogue. As the line will be the Rectangle rectangle, the same technique is applied in the development of Android on Java, but instead of QML uses XML layout.

In order to make a customized normal contents of the dialog box, you must specify its parameter contentItem the object that will replace the contents. The most convenient is the Rectangle object, as early as it will place all other objects. Naturally no standard buttons there already and will not be in sight, so you can forget about them, but it is not a great loss indeed.


main.qml

All work will be carried out only in main.qml file, so just bring it. The rest of the information can see in the previous article .

  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Controls.Styles 1.4
  4. import QtQuick.Dialogs 1.2
  5. import QtQuick.Layouts 1.1
  6.  
  7. ApplicationWindow {
  8. visible: true
  9. width: 640
  10. height: 480
  11. title: qsTr("Hello World")
  12. color: "white"
  13.  
  14. MainForm {
  15. // Stretch the object of the main window over the parent element
  16. anchors.fill: parent
  17.  
  18. // Styling the first button
  19. button1.style: ButtonStyle {
  20. // Styling the appearance of button
  21. background: Rectangle {
  22. /* If the button is pressed, it will be red
  23.   * with a black bezel with rounded edges,
  24.   * otherwise it will be black with a red rim
  25. */
  26. color: control.pressed ? "#d7d7d7" : "#f7f7f7"
  27. border.color: "#d7d7d7"
  28. border.width: 2
  29. radius: 5
  30. }
  31.  
  32. // Styling the button text color
  33. label: Text {
  34. /* If the button is pressed, the color will be black
  35.                  * Otherwise red
  36. */
  37. text: qsTr("Press Me 1")
  38. color: "black"
  39. }
  40. }
  41.  
  42. // Styling second button
  43. button2.style: ButtonStyle {
  44. // Styling the appearance of buttons
  45. background: Rectangle {
  46. /* similarly as for the first button,
  47.                  * But in the reverse order
  48. */
  49. color: control.pressed ? "#d7d7d7" : "#f7f7f7"
  50. border.color: "#d7d7d7"
  51. border.width: 2
  52. radius: 5
  53. }
  54. // Styling button color
  55. label: Text {
  56. /* similarly as for the first button,
  57.                  * But in the reverse order
  58. */
  59. text: qsTr("Press Me 2")
  60. color: "black"
  61. }
  62. }
  63.  
  64. // Start a dialogue by pressing any of the buttons in the main window
  65. button1.onClicked: dialogAndroid.open();
  66. button2.onClicked: dialogAndroid.open();
  67.  
  68. // Create Object dialog box
  69. Dialog {
  70. id: dialogAndroid
  71. /* When deploite for Android-devices,
  72.              * obligatory comment out these two lines,
  73.              * another word glitches in the operation of the device
  74. */
  75. width: 600 // Set the width of the dialog, which works on the desktop, but it does not work on Android
  76. height: 500 // Set the height of the dialog, which works on the dekstop, but does not work on Android
  77.  
  78. // Create the contents of the dialog box
  79. contentItem: Rectangle {
  80. width: 600 // Set the width, necessary for Android-devices
  81. height: 500 // Set the height, necessary for Android-devices
  82. color: "#f7f7f7" // Set the color
  83.  
  84. // The area for the dialog box message
  85. Rectangle {
  86. anchors.left: parent.left
  87. anchors.right: parent.right
  88. anchors.top: parent.top
  89. anchors.bottom: dividerHorizontal.top
  90. color: "#f7f7f7" // Задаём цвет области
  91.  
  92. Label {
  93. id: textLabel
  94. text: qsTr("Hello, World!!!")
  95. color: "#34aadc"
  96. anchors.centerIn: parent
  97. }
  98. }
  99.  
  100. // Create a horizontal divider with the Rectangle
  101. Rectangle {
  102. id: dividerHorizontal
  103. color: "#d7d7d7"
  104. height: 2
  105. anchors.left: parent.left
  106. anchors.right: parent.right
  107. anchors.bottom: row.top
  108. }
  109.  
  110. /* Create a support for an object in the form of rows of buttons
  111.                  * this facility for children objects do not work some parameters of
  112.                  * anchors, except anchors.top parameters and anchors.bottom
  113. */
  114. Row {
  115. id: row
  116. height: 100 // Set height
  117. anchors.bottom: parent.bottom
  118. anchors.left: parent.left
  119. anchors.right: parent.right
  120.  
  121. Button {
  122. id: dialogButtonCancel
  123. anchors.top: parent.top
  124. anchors.bottom: parent.bottom
  125. // Set width button halfway line minus 1 pixel
  126. width: parent.width / 2 - 1
  127.  
  128. style: ButtonStyle {
  129. background: Rectangle {
  130. color: control.pressed ? "#d7d7d7" : "#f7f7f7"
  131. border.width: 0
  132. }
  133.  
  134. label: Text {
  135. text: qsTr("Cancel")
  136. color: "#34aadc"
  137. verticalAlignment: Text.AlignVCenter
  138. horizontalAlignment: Text.AlignHCenter
  139. }
  140. }
  141.  
  142. onClicked: dialogAndroid.close()
  143. }
  144.  
  145. Rectangle {
  146. id: dividerVertical
  147. width: 2
  148. anchors.top: parent.top
  149. anchors.bottom: parent.bottom
  150. color: "#d7d7d7" // Задаём цвет разделителя
  151. }
  152.  
  153. Button {
  154. id: dialogButtonOk
  155. anchors.top: parent.top
  156. anchors.bottom: parent.bottom
  157. width: parent.width / 2 - 1
  158.  
  159. // Стилизуем кнопку
  160. style: ButtonStyle {
  161. background: Rectangle {
  162. color: control.pressed ? "#d7d7d7" : "#f7f7f7"
  163. border.width: 0
  164. }
  165.  
  166. label: Text {
  167. text: qsTr("Ok")
  168. color: "#34aadc"
  169. verticalAlignment: Text.AlignVCenter
  170. horizontalAlignment: Text.AlignHCenter
  171. }
  172. }
  173. onClicked: dialogAndroid.close()
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }

Result

As a result, you get Custom Dialog, which already will look decent on your Android device. Of course, under the dekstopov it will look awful, at least in this tutorial, but it is a question of the correct scaling size and configuration elements that are the issue of a separate article.

And here is the result of dialogue on my smartphone and the desktop you can see in the screenshots and in the video tutorial.

QML Custom Dialog Desctop

QML Custom Dialog Android

QML Custom Dialog Android

Video Lesson

M
  • Oct. 4, 2017, 3:17 p.m.

Немного не понял
Заполнение Row происходит слава направо?
Сначала устанавливаем ButtonCancel потом Вертикальный разделитель и следом ButtonOk

Evgenii Legotckoi
  • Oct. 4, 2017, 4:42 p.m.

Ну да. Вас смущает последовательность? Можете и наоборот заполнить.
Это из разряда какой вариант юзабилити может быть удобнее. Начитался всяких гайдлайнов от Google да Apple, вот так и написал. А так дело вашего вкуса.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html