在QML webview中使用alert及confirm Dialogs

185 篇文章 6 订阅
9 篇文章 0 订阅

我们可以在QML应用中直接使用QML WebView来装载我们的HTML应用,但是如果我们的HTML文件中使用到alert及confirm,我们的应用可能并不能弹出我们需要的alert及confirm。这时,我们需要对WebView中的alertDialog及confirmDialog进行设计才可以起到作用。


我们在下面来展示如何去做:


index.html


<html>
   <head>
   
      <script type="text/javascript">
         <!--
            function getConfirmation(){
               var retVal = confirm("Do you want to continue ?");
               if( retVal == true ){
                  return true;
               }
               else{
                  return false;
               }
            }
         //-->

         <!--
            function getAlert(){
               alert("This is cool!");
            }
         //-->

        <!--
            function getPrompt(){
               var retVal = prompt("Enter your name : ", "your name here");
               // console.log("retVal: + " + retVal);
            }
         //-->
      </script>
      
   </head>
   <body>
      <p>Click the following button to see the result: </p>
      
      <form>
         <input type="button" value="Alert Dialog" οnclick="getAlert();" /> <br>
         <input type="button" value="Confirm Dialog" οnclick="getConfirmation();" /> <br>
         <input type="button" value="Prompt Dialog" οnclick="getPrompt();" />
      </form>
      
   </body>
</html>


Main.qml


import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Web 0.2
import Ubuntu.Components.Popups 1.0

/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "webviewdialog.liu-xiao-guo"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    // Removes the old toolbar and enables new features of the new header.
    useDeprecatedToolbar: false

    width: units.gu(60)
    height: units.gu(85)

    Page {
        title: i18n.tr("webviewdialog")

        Component {
            id: confirmDlg

            Dialog {
                title: i18n.tr("JavaScript Confirmation")

                Button {
                    text: i18n.tr("OK")
                    onClicked: model.accept()
                }

                Button {
                    text: i18n.tr("Cancel")
                    onClicked: model.reject()
                }

                Component.onCompleted: show()
            }
        }

        Component {
            id: alertDlg

            Dialog {
                title: model.message

                Button {
                    text: i18n.tr("OK")
                    onClicked: model.accept()
                }

                Component.onCompleted: show()
            }
        }

        Component {
            id: promptDlg

            Dialog {
                TextField {
                    id: input
                    text: model.defaultValue
                    onAccepted: model.accept(input.text)
                }

                Button {
                    text: i18n.tr("OK")
                    color: "green"
                    onClicked: model.accept(input.text)
                }

                Button {
                    text: i18n.tr("Cancel")
                    color: UbuntuColors.coolGrey
                    onClicked: model.reject()
                }

                Binding {
                    target: model
                    property: "currentValue"
                    value: input.text
                }

                Component.onCompleted: show()
            }
        }

        WebView {
            anchors.fill: parent
            url: "www/index.html"
            confirmDialog: confirmDlg
            alertDialog: alertDlg
            promptDialog: promptDlg
        }
    }
}



在这里,我们对alertDialog及confirmDialog进行了初始化,这样当我们在index.html中使用alert及confirm时,我们可以得到我们需要的Dialog:


    




整个项目的源码在: https://github.com/liu-xiao-guo/webviewdialog


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值