Advertisement
Guest User

Captcha Dialog QML

a guest
Feb 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Window 2.0
  4.  
  5. Window {
  6.   id: captchaDialog
  7.   title: qsTr("Captcha")
  8.   minimumWidth: 220
  9.   minimumHeight: 160
  10.   maximumWidth: minimumWidth
  11.   maximumHeight: minimumHeight
  12.   flags: Qt.Dialog
  13.   color: "white"
  14.   property alias captchaUrl: captchaImage.source
  15.   property int paddings: 10
  16.   signal captchaSubmitted(string code)
  17.   signal closed()
  18.  
  19.   Rectangle {
  20.     id: container
  21.     anchors.fill: parent
  22.     anchors.margins: captchaDialog.paddings
  23.     color: "transparent"
  24.  
  25.     Rectangle {
  26.       id: content
  27.       anchors.top: parent.top
  28.       color: "transparent"
  29.       width: parent.width
  30.       height: parent.height - buttonContainer.height
  31.  
  32.       Column {
  33.         anchors.centerIn: parent
  34.         spacing: captchaDialog.paddings
  35.  
  36.         Image {
  37.           id: captchaImage
  38.           cache: false
  39.           width: 130
  40.           height: 50
  41.  
  42.           function reload() {
  43.             var s = source.toString().replace(/&v=.*/, '');
  44.             source = s + "&v=" + Date.now();
  45.             captchaCode.text = "";
  46.             captchaCode.focus = true;
  47.           }
  48.  
  49.           // Контекстное меню самому нужно реализовывать. Код ниже нерабочий:
  50.           // Menu {
  51.           //   MenuItem {
  52.           //     text: qsTr("Reload")
  53.           //     onTriggered: captchaImage.reload()
  54.           //   }
  55.           // }
  56.  
  57.           MouseArea {
  58.             anchors.fill: parent
  59.             onClicked: parent.reload()
  60.           }
  61.         }
  62.  
  63.         TextField {
  64.           id: captchaCode
  65.           horizontalAlignment: TextInput.AlignHCenter
  66.           placeholderText: qsTr("Enter captcha code")
  67.           width: 130
  68.           // Вручную нужно делать
  69.           onAccepted: sendButton.clicked()
  70.         }
  71.       }
  72.     }
  73.  
  74.     Rectangle {
  75.       id: buttonContainer
  76.       anchors.bottom: parent.bottom
  77.       color: "transparent"
  78.       width: parent.width
  79.       height: childrenRect.height
  80.  
  81.       Button {
  82.         id: sendButton
  83.         anchors.right: parent.right
  84.         text: qsTr("Send")
  85.         onClicked: captchaDialog.captchaSubmitted(captchaCode.text)
  86.       }
  87.     }
  88.   }
  89.  
  90.   onCaptchaSubmitted: {
  91.     close()
  92.   }
  93.  
  94.   onVisibleChanged: {
  95.     if (visible == Window.Hidden) {
  96.       closed()
  97.     }
  98.   }
  99.  
  100.   onClosed: {
  101.     captchaCode.text = ""
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement