實現一個點擊輸入框可以彈出的數字軟鍵盤控件 qt 5.12

我們將創建兩個自定義組件: 1. `NumericInputField`:一個輸入框,當點擊時彈出數字鍵盤。 2. `NumericKeyboard`:一個可縮放的數字鍵盤。 設計思路: - `NumericInputField` 是一個常規的輸入框,但點擊后會彈出 `NumericKeyboard`。 - `NumericKeyboard` 是一個彈出式鍵盤,它的大小可以等比例縮放(通過設置寬度,高度按比例計算)。 - 鍵盤內部包含數字0-9、小數點、退格、取消和確定按鈕。 - 鍵盤的尺寸和字體大小都可以通過屬性設置。 步驟: 1. 創建 `NumericKeyboard` 組件。 2. 創建 `NumericInputField` 組件,它包含一個 `TextInput` 和一個彈出鍵盤的機制。 3. 在 `NumericInputField` 中,當點擊輸入框時,顯示 `NumericKeyboard`,并將鍵盤與輸入框的位置關聯(例如在輸入框下方彈出)。 注意:為了支持等比例縮放,鍵盤的寬度可以設置,高度則根據寬度按比例計算(例如高度是寬度的0.6倍)。鍵盤內部的按鈕大小和字體大小也根據鍵盤的當前尺寸進行縮放。 由于要求每個自定義組件的代碼,我們將分別實現: 組件一:NumericKeyboard.qml 組件二:NumericInputField.qml 然后提供一個使用示例。

可縮放彈出式數字軟鍵盤組件

下面我將實現一個完整的可縮放彈出式數字軟鍵盤組件,包含兩個自定義組件:NumericInputField(輸入框)和?NumericKeyboard(鍵盤)。這兩個組件都支持等比例縮放。

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12import QtQml 2.12import QtQuick.Layouts 1.12
import "./image"import QtQuick.Layouts 1.3Window {width: 1200height: 800visible: truetitle: "可縮放彈出式數字鍵盤"color: "#f8fafc"ColumnLayout {anchors.fill: parentanchors.margins: 30spacing: 20// 標題Text {text: "可縮放彈出式數字鍵盤"font.pixelSize: 28font.bold: truecolor: "#1e293b"Layout.alignment: Qt.AlignCenter}// 示例1:基本使用NumericInputField {id: basicInputLayout.preferredWidth: 300Layout.preferredHeight: 60placeholder: "點擊輸入金額"Layout.alignment: Qt.AlignCenter}// 示例2:整數輸入NumericInputField {id: integerInputLayout.preferredWidth: 250Layout.preferredHeight: 50placeholder: "點擊輸入數量"allowDecimal: false // 禁用小數點Layout.alignment: Qt.AlignCenter}// 示例3:自定義鍵盤大小NumericInputField {id: customSizeInputLayout.preferredWidth: 350Layout.preferredHeight: 70placeholder: "點擊輸入百分比"keyboardScale: 0.7 // 鍵盤縮放比例Layout.alignment: Qt.AlignCenter}// 控制面板RowLayout {Layout.alignment: Qt.AlignCenterspacing: 20// 鍵盤縮放控制Slider {id: scaleSliderfrom: 0.5to: 1.2value: 0.8Layout.preferredWidth: 200onValueChanged: {basicInput.keyboardScale = valueintegerInput.keyboardScale = valuecustomSizeInput.keyboardScale = value}}// 縮放值顯示Text {text: "鍵盤縮放: " + scaleSlider.value.toFixed(2)font.pixelSize: 16color: "#64748b"}// 小數點開關Switch {id: decimalSwitchchecked: truetext: "允許小數點"}}// 結果展示GridLayout {columns: 3columnSpacing: 30rowSpacing: 10Layout.alignment: Qt.AlignCenterText { text: "金額:"; font.bold: true; color: "#475569" }Text { text: basicInput.text || "0.00"; color: "#334155" }Text { text: "縮放: " + basicInput.keyboardScale.toFixed(2); color: "#64748b" }Text { text: "數量:"; font.bold: true; color: "#475569" }Text { text: integerInput.text || "0"; color: "#334155" }Text { text: "縮放: " + integerInput.keyboardScale.toFixed(2); color: "#64748b" }Text { text: "百分比:"; font.bold: true; color: "#475569" }Text { text: customSizeInput.text || "0.00"; color: "#334155" }Text { text: "縮放: " + customSizeInput.keyboardScale.toFixed(2); color: "#64748b" }}}// 應用鍵盤設置Component.onCompleted: {integerInput.allowDecimal = decimalSwitch.checked}// 小數點開關變化Connections {target: decimalSwitchfunction onCheckedChanged() {integerInput.allowDecimal = decimalSwitch.checked}}
}
//NumericInputField
import QtQuick 2.12
import QtQuick.Controls 2.12import QtQml 2.12
import "../image"Rectangle {id: rootwidth: 300height: 60radius: 10color: "white"border.color: root.activeFocus ? "#4f46e5" : "#d1d5db"border.width: 2// ===== 公共屬性 =====property alias text: inputField.text       // 輸入文本property string placeholder: "點擊輸入"    // 占位符文本property real keyboardScale: 0.4          // 鍵盤縮放比例property bool allowDecimal: true          // 是否允許小數點// ===== 鍵盤實例 =====property NumericKeyboard keyboard: NumericKeyboard {id: numericKeyboardparent: Overlay.overlay // 確保鍵盤顯示在最上層allowDecimal: root.allowDecimalvisible: false}// ===== 輸入區域 =====TextInput {id: inputFieldanchors.fill: parentanchors.margins: 15verticalAlignment: Text.AlignVCenterfont.pixelSize: 24clip: truereadOnly: true // 只能通過鍵盤輸入// 占位符文本Text {text: root.placeholderfont: inputField.fontcolor: "#9ca3af"visible: inputField.text.length === 0 && !root.activeFocusanchors.verticalCenter: parent.verticalCenter}}// ===== 鍵盤圖標 =====Rectangle {width: 40height: 40radius: 20color: "#eef2ff"anchors.verticalCenter: parent.verticalCenteranchors.right: parent.rightanchors.rightMargin: 10Text {text: "?"font.pixelSize: 24anchors.centerIn: parent}}// ===== 點擊彈出鍵盤 =====MouseArea {anchors.fill: parentonClicked: {// 設置鍵盤位置和大小numericKeyboard.width = root.width * 3 // 鍵盤比輸入框寬
//            numericKeyboard.keyboardScale = keyboardScalenumericKeyboard.text = inputField.text// 打開鍵盤numericKeyboard.open()// 連接信號numericKeyboard.accepted.connect(handleAccepted)}}// ===== 鍵盤接受事件處理 =====function handleAccepted() {inputField.text = numericKeyboard.text}// ===== 鍵盤關閉時斷開連接 =====Connections {target: numericKeyboardfunction onClosed() {numericKeyboard.accepted.disconnect(handleAccepted)}}// ===== 輸入框獲得焦點樣式 =====onActiveFocusChanged: {border.color = activeFocus ? "#4f46e5" : "#d1d5db"}
}
//NumericKeyboard
import QtQuick 2.12
import QtQuick.Controls 2.12import QtGraphicalEffects 1.12
Popup {id: rootwidth: Math.min(parent.width * 0.9, 600)  // 寬度自適應,最大600pxheight: width * 0.6  // 高度按比例計算anchors.centerIn: parentmodal: truedim: trueclosePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside// ===== 公共屬性 =====property alias text: inputField.text      // 綁定輸入文本property bool allowDecimal: true         // 是否允許小數點property real scaleFactor: width / 600   // 縮放因子(基于600px基準)property int baseFontSize: 24            // 基準字體大小property color buttonColor: "#f0f0f0"    // 按鈕默認顏色// ===== 信號 =====signal accepted()  // 點擊確定時觸發signal canceled() // 點擊取消時觸發// ===== 鍵盤背景 =====background: Rectangle {anchors.fill: parentcolor: "blue"radius: 10 * scaleFactorborder.color: "#d1d5db"border.width: 1 * scaleFactor// 鍵盤陰影效果layer.enabled: truelayer.effect: DropShadow {color: "#40000000"radius: 10 * scaleFactorsamples: 21verticalOffset: 3 * scaleFactor}}// ===== 輸入框區域 =====Rectangle {id: inputContainerwidth: parent.width - 40 * scaleFactorheight: 60 * scaleFactoranchors.horizontalCenter: parent.horizontalCenteranchors.top: parent.topanchors.topMargin: 20 * scaleFactorcolor: "white"radius: 8 * scaleFactorborder.color: inputField.activeFocus ? "#4f46e5" : "#d1d5db"border.width: 2 * scaleFactor// 輸入框TextInput {id: inputFieldanchors.fill: parentanchors.margins: 15 * scaleFactorfont.pixelSize: baseFontSize * scaleFactor + 4verticalAlignment: Text.AlignVCenterclip: truereadOnly: true // 禁止直接編輯// 文本改變動畫Behavior on text {SequentialAnimation {PropertyAnimation {target: inputContainerproperty: "border.color"to: "#4f46e5"duration: 100}PropertyAnimation {target: inputContainerproperty: "border.color"to: "#d1d5db"duration: 300}}}}// 清除按鈕Rectangle {width: 30 * scaleFactorheight: 30 * scaleFactorradius: 15 * scaleFactoranchors.verticalCenter: parent.verticalCenteranchors.right: parent.rightanchors.rightMargin: 10 * scaleFactorcolor: clearMouseArea.containsPress ? "#fee2e2" : "#fef2f2"visible: inputField.text.length > 0Text {text: "×"font.pixelSize: baseFontSize * scaleFactorcolor: "#ef4444"anchors.centerIn: parent}MouseArea {id: clearMouseAreaanchors.fill: parentonClicked: inputField.text = ""}}}// ===== 鍵盤網格布局 =====Grid {id: gridanchors.top: inputContainer.bottomanchors.topMargin: 20 * scaleFactoranchors.horizontalCenter: parent.horizontalCenterspacing: 10 * scaleFactorcolumns: 5// 數字鍵1-9Repeater {model: 9KeyButton {text: index + 1width: keyWidthheight: keyHeightonClicked: inputField.insert(inputField.cursorPosition, text)}}// 小數點鍵(根據allowDecimal顯示/隱藏)KeyButton {text: "."width: keyWidthheight: keyHeightvisible: root.allowDecimalonClicked: {if (!inputField.text.includes(".")) {inputField.insert(inputField.cursorPosition, text)}}}// 數字0KeyButton {text: "0"width: keyWidthheight: keyHeightonClicked: inputField.insert(inputField.cursorPosition, text)}// 退格鍵KeyButton {text: "?"width: keyWidthheight: keyHeightbgColor: "#fecaca"textColor: "#b91c1c"onClicked: inputField.text = inputField.text.slice(0, -1)}// 取消鍵KeyButton {text: "取消"width: keyWidthheight: keyHeightbgColor: "#e5e7eb"onClicked: {root.canceled()root.close()}}// 確定鍵KeyButton {text: "確定"width: keyWidthheight: keyHeightbgColor: "#4f46e5"textColor: "white"onClicked: {root.accepted()root.close()}}}// ===== 計算按鍵尺寸 =====property real keyWidth: (root.width - 40 * scaleFactor - 2 * grid.spacing) / 6property real keyHeight: keyWidth * 0.8// ===== 彈出動畫 =====enter: Transition {ParallelAnimation {NumberAnimation { property: "opacity"; from: 0; to: 1; duration: 300 }NumberAnimation { property: "scale"; from: 0.8; to: 1; duration: 300; easing.type: Easing.OutBack }}}// ===== 關閉動畫 =====exit: Transition {ParallelAnimation {NumberAnimation { property: "opacity"; from: 1; to: 0; duration: 300 }NumberAnimation { property: "scale"; from: 1; to: 0.8; duration: 300 }}}
}
KeyButton
//KeyButton
import QtQuick 2.12// ===== 自定義按鈕組件 =====
Rectangle {id: btnradius: 8 * root.scaleFactorcolor: mouseArea.pressed ? Qt.darker(bgColor, 1.2) : bgColorborder.color: mouseArea.pressed ? Qt.darker(borderColor, 1.2) : borderColorborder.width: 1 * root.scaleFactor// ===== 可自定義屬性 =====property alias text: label.textproperty color bgColor: root.buttonColorproperty color textColor: "black"property color borderColor: "#d1d5db"// 按鈕文本Text {id: labelanchors.centerIn: parentfont.pixelSize: root.baseFontSize * root.scaleFactorcolor: btn.textColor}// 鼠標區域MouseArea {id: mouseAreaanchors.fill: parentonClicked: btn.clicked()}// 點擊效果動畫scale: mouseArea.pressed ? 0.95 : 1.0Behavior on scale {NumberAnimation { duration: 100 }}// 點擊信號signal clicked()
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/88683.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/88683.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/88683.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java 深入解析:JVM對象創建與內存機制全景圖

第一章:引言 Java 是一種面向對象的編程語言,對象(Object)是其最基本的組成單位。Java 的“一切皆對象”不僅體現在語法層面,更體現在運行時,幾乎所有數據都以對象形式存在于內存中。 然而,很…

Redis 基本操作筆記

1. Redis 簡介 Redis(Remote Dictionary Server)是一個開源的、高性能的鍵值對存儲系統,通常作為數據庫、緩存、消息中間件等使用。它支持多種數據類型,包括字符串、哈希、列表、集合、有序集合等。 Redis 特點: 性能&…

Docker從環境配置到應用上云的極簡路徑

Docker從環境配置到應用上云的極簡路徑主要包括環境配置、應用容器化、選擇云平臺及部署應用等步驟,具體如下: - 配置Docker環境: - 安裝Docker:根據操作系統下載對應版本的Docker安裝包。如在Linux系統中,可使用命令…

Slicer渲染Dicom到nrrd

Slicer渲染Dicom到nrrd 工作中遇到一些處理Dicom數據的需求,個人通過網絡上的一些教程 對于原始數據嘗試轉換到nrrd時,發現部分的窗體數據的渲染方向不一致 進一步發現這些很多定義的方向是跟設備廠家強相關的,不同廠家對于同一段的Dicom參…

QT中設計qss字體樣式但是沒有用【已解決】

檢查一下stylesheet里面是不是有不能被QT讀取的CSS語言,可能會跟字體顏色沖突錯誤示范:/* 錯誤示例:QSS 中使用 box-shadow */ QPushButton {box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3); /* Qt 不支持此屬性 */ }刪掉就行了如果后續想用陰影…

uniapp獲取狀態欄高度,膠囊按鈕的高度,底部安全區域的高度,自定義導航欄

相關API uni.getSystemInfoSync() uni.getMenuButtonBoundingClientRect() 創建一個utils文件夾,該文件下封裝一個systemInfo.js /*** 系統信息工具類* 封裝獲取系統狀態欄、導航欄和安全區域等相關信息的方法*/// 獲取系統信息并緩存 const systemInfo uni.get…

jQuery 文本屬性值

一、前言在網頁開發中,我們經常需要對頁面上的文本內容進行操作,例如動態修改段落文字、讀取用戶輸入框的內容、更新按鈕文本等。jQuery 提供了簡潔而強大的方法來處理這些常見的文本操作需求。本文將帶你全面了解 jQuery 中用于操作文本內容的三個核心方…

JAVA并發——為什么Java中的ThreadLocal對key的引用為弱引用

1、ThreadLocal 的用途 給每個線程提供自己獨立的變量副本,實現線程間隔離。 常用于: 數據庫連接、Session 緩存、用戶上下文(如 userId)線程池中的線程復用時避免共享污染;實現線程封閉的設計模式 2、內存泄漏 使用弱…

【C++】多線程同步三劍客介紹

目錄 條件變量 頭文件 主要操作函數 1、等待操作 2、喚醒操作 使用示例 信號量 頭文件 主要操作函數 1、信號量初始化 2、等待操作(P操作) 3、信號操作(V操作) 4、獲取信號量值 5、銷毀信號量 使用示例 互斥鎖 …

《Java Web程序設計》實驗報告八 JSP+Servlet+JDBC+MySQL實現課程管理

目 錄 一、實驗目的 二、實驗環境 三、實驗步驟和內容 1、小組成員分工(共計4人) 2、實驗方案 3、實驗結果與分析 4、項目任務評價 四、遇到的問題和解決方法 五、實驗總結 一、實驗目的 1、掌握mysql的安裝、數據庫表單創建 2、掌握JDBC的鏈接…

基于數據挖掘的課程推薦系統研究

摘要本研究設計并開發了一套基于先進數據挖掘技術的智能化課程推薦系統。該系統創新性地采用了協同過濾算法與內容推薦算法相結合的混合推薦策略,通過深度分析學生在學習平臺上的歷史行為數據(包括選課記錄、學習時長、測試成績等)以及課程的…

【SCI 4區推薦】《Journal of Visual Communication and Image Representation》

期刊簡介:《視覺傳達與圖像表示雜志》(Journal of Visual Communication and Image Representation)致力于發表視覺傳達與圖像表示領域的最前沿研究,特別強調多學科交叉領域中的新技術和理論應用。這本期刊涵蓋的研究范圍廣泛&…

20250711_Sudo 靶機復盤

target:192.168.43.20 外部打點 (文件上傳) nmap掃一下,80,22 開放 掃目錄,發現 README.md [17:04:30] 200 - 664B - /Dockerfile [17:04:38] 200 - 34KB - /LICENSE …

STEP 7-Micro/WIN SMART 編程軟件:從入門到精通的使用指南

STEP 7-Micro/WIN SMART 編程軟件:從入門到精通的使用指南 在工業自動化控制領域,編程軟件是連接工程師與 PLC 的橋梁,而 STEP 7-Micro/WIN SMART 作為 S7-200 SMART PLC 的專用編程工具,以其友好的界面和高效的編程能力備受青睞。…

模型訓練與部署注意事項篇---resize

圖像大小的影響在 YOLOv 系列模型的訓練和推理部署過程中,圖像大小的選擇是影響模型性能(精度、速度、泛化能力)的關鍵因素之一。兩者的關系既相互關聯,又存在一定的靈活性,具體可從以下幾個方面詳細分析:一…

【Python】venv:配置獨立鏡像源

為某個特定的 venv 虛擬環境設置 pip 鏡像源,使得該環境下的 pip 安裝始終使用自定義鏡像源,不影響系統 pip,也不依賴用戶級配置文件。環境準備 1. 創建虛擬環境 python -m venv venv2. 激活虛擬環境Windows: .\venv\Scripts\activateLinux/m…

日本語言學校:簽證制度類 Prompt 的結構整理路徑與策略

日本語言學校:簽證制度類 Prompt 的結構整理路徑與策略 我們在構建語言留學語義系統的過程中,嘗試以“簽證風險”為例,探索如何讓結構信息被更好地保留下來。本文不介紹 Prompt 本身,也不夸大其作用,而是希望借此與更…

RFCOMM協議詳解:串口仿真與TCP/IP協議棧移植技術——面試高頻考點與真題解析

一、RFCOMM 協議核心考點與高頻面試問題1.1 協議基礎與核心功能考點解析:RFCOMM(Radio Frequency Communication)是藍牙協議棧中實現串口仿真的核心協議,基于 L2CAP 協議提供類似 RS-232 的可靠數據流傳輸。其核心功能包括&#x…

【編程實踐】利用open3d生成物體的最長邊方向并可視化

1 利用3d軟件生成一個長方體 邊長隨意,長度隨意 2 導出為模型文件并采樣為點云數據 從mesh表面進行采樣,點數根據自己需求進行設置,此處設置為100000。采樣結果:3 識別OBB外接框并可視化長邊方向import numpy as np import open3d…

1. 好的設計原則

目錄一、應該具備的性質二、面向對象設計原則三、詳解3.1 開閉原則3.2 單一職責原則3.3 里氏替換原則3.4 依賴倒置原則3.5 接口隔離原則3.6 合成復用原則3.7 迪米特原則一、應該具備的性質 可擴展性靈活性可插入性 二、面向對象設計原則 以下設計原則的重要性從高到低排列 …