如何在对话框外点击有明显的信号?

时间:2018-04-11 12:01:19

标签: qt qml

我有一个带有'YES'和'NO'按钮的对话框。我想在对话框外面按下关闭对话框并且也不会触发'NO'按钮信号(只需按'NO'按钮就可以触发它)。有可能吗?

Dialog {
    id: dialog
    title: "Save your Info?"
    width: parent.width/2
    modal: true
    closePolicy: Popup.CloseOnPressOutside
    standardButtons: Dialog.Yes | Dialog.No

    onAccepted: console.log("Yes clicked")
    onRejected: console.log("No clicked")
    onClosed: console.log("Dialog closed")
    Component.onCompleted: open()
}

现在,如果我点击对话框之外,它会打印:

  

qml:没有点击

     

qml:对话框已关闭

是否有可能只是通过点击对话关闭对话(实际取消它)而不是在关闭之前拒绝它?

更新:实际上,我只是希望通过点击外部为“是”,“否”和“取消”提供3个不同的信号

2 个答案:

答案 0 :(得分:0)

将模态更改为false(这样您可以在外面按住而closePolicy不是CloseOnPressOutside模式对话框会阻止您的按下),将closePolicy更改为其他任何内容(不是{{1}但是请不要将其保留为默认值。添加CloseOnPressOutside以处理外部对话框。

MouseArea

答案 1 :(得分:-2)

感谢您对穆罕默德的回应。但我试图找到更好的方法,最后我发现了这种方法。我希望它也可以帮助别人。

namespace PLMLJS.Test
{
    [TestClass]
    public class TestQRCoder2Controller
    {
        private HttpClient _httpClient;
        private string _url;

        [TestInitialize]
        public void TestInit()
        {
            if (_httpClient != null)
                _httpClient.Dispose();
            _httpClient = new HttpClient();
            _url = Settings.ServerURL + "/api/qrcoder2";   
        }

        [TestCleanup]
        public void TestCleanup()
        {
            if (_httpClient != null)
                _httpClient.Dispose();
            _url = null;
        }

        [TestMethod]
        public async Task Test_createQRCode_retByteArray()
        {   // This function works.  
            // Successfully receives Bitmap(byte[]) returned from controller (createQRCode_retByteArray)

            CountriesView countriesView = new CountriesView();
            countriesView.Name = "United States";
            countriesView.ISO2 = "US";

            TestInit();

            using (var client = new HttpClient())
            {
                var data = JsonConvert.SerializeObject(countriesView);
                var buffer = System.Text.Encoding.UTF8.GetBytes(data);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = null;

                try
                {
                    response = await _httpClient.PostAsync(_url + "/createQRCode_retByteArray/", byteContent);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: TargetSite=" + e.TargetSite + "   Error messsage=" + e.Message);
                }

                if (response.IsSuccessStatusCode)
                {   // if return Ok(Bitmap qrCodeBitmap) then the following 2 lines work.

                    var result = response.Content.ReadAsStringAsync().Result;
                    // result:   (expected result value)
                    // "\"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBk....."

                    result = result.Replace("\"", string.Empty);
                    byte[] bytearray = Convert.FromBase64String(result);

                    File.WriteAllBytes("c:\\temp\\img\\Ret_ByteArray_" + Path.GetRandomFileName() + ".jpg", bytearray);
                }

                Assert.AreEqual((int)HttpStatusCode.OK, (int)response.StatusCode);
            }

            TestCleanup();
        }

        [TestMethod]
        public async Task Test_createQRCode_retIActionResult()
        {   // This function fails.  
            // Neither anyone below at Controller sends memoryStream back
            // response.Content = new ByteArrayContent(memoryStream.ToArray());
            // response.Content = new StreamContent(memoryStream);

            CountriesView countriesView = new CountriesView();
            countriesView.Name = "United States";
            countriesView.ISO2 = "US";

            TestInit();

            using (var client = new HttpClient())
            {
                var data = JsonConvert.SerializeObject(countriesView);
                var buffer = System.Text.Encoding.UTF8.GetBytes(data);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = null;

                try
                {
                    response = await _httpClient.PostAsync(_url + "/createQRCode_retIActionResult/", byteContent);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: TargetSite=" + e.TargetSite + "   Error messsage=" + e.Message);
                }

                if (response.IsSuccessStatusCode)
                {   
                    // var result = response.Content.ReadAsByteArrayAsync().Result;     fails

                    var result = response.Content.ReadAsStringAsync().Result.Replace("\"", string.Empty);  //What went wrong here?????
                    // result:    (unexpected result value)
                    // "{version:{major:1,minor:1,build:-1,revision:-1,majorRevision:-1,minorRevision:-1},
                    // content:{headers:[]},statusCode:200,reasonPhrase:OK,headers:[],requestMessage:null,
                    // isSuccessStatusCode:true}"

                    byte[] byteArray = Convert.FromBase64String(result);

                    File.WriteAllBytes("c:\\temp\\img\\Ret_IActionResult_" + Path.GetRandomFileName() + ".jpg", byteArray);
                }

                Assert.AreEqual((int)HttpStatusCode.OK, (int)response.StatusCode);
            }

            TestCleanup();
        }
    }

}