当前位置: 代码迷 >> 综合 >> QtQuick 技巧 4
  详细解决方案

QtQuick 技巧 4

热度:71   发布时间:2023-12-16 08:54:46.0

QtQuick 技巧 4

基于 QtQuick.Controls 1.x

QML 窗体透明

ApplicationWindow {flags: Qt.Window|Qt.FramelessWindowHintstyle: ApplicationWindowStyle {background: Item{ }}
}
ApplicationWindow {flags: Qt.Window|Qt.FramelessWindowHintstyle: ApplicationWindowStyle {background: Rectangle{ color: "transparent" }}
}

QML 不规则窗体

ApplicationWindow {flags: Qt.Window|Qt.FramelessWindowHintstyle: ApplicationWindowStyle {background: Image{ source: "不规则.png" }}
}

ListView 注意事项

currentIndex 必需在 ListView::highlightRangeMode 设置为 ListView.StrictlyEnforceRange 才能实时更新,不然可能永远都是 0

Text 注意事项

自动分行,需要指明 Text::width。不然分行没有参考值。

QtQuick 母版页

//~ Panel.qml
Item {property alias headerHeight: headerLoader.heightproperty alias footerHeight: footerLoader.heightproperty Component headerComponent: nullreadonly property Item headerItem: headerLoader.itemLoader {id: headerLoaderwidth: parent.widthheight: 40sourceComponent: headerComponentBinding {target: headerLoader.itemproperty: "anchors.fill"value: headerLoader}}property Component footerComponent: nullreadonly property Item footerItem: footerLoader.itemLoader {id: footerLoaderwidth: parent.widthheight: 40anchors.bottom: parent.bottomBinding {target: footerLoader.itemproperty: "anchors.fill"value: footerLoader}}property Component contentComponent: nullreadonly property Item contentItem: contentLoader.itemLoader {id: contentLoaderwidth: parent.widthanchors.top: headerLoader.bottomanchors.bottom: footerLoader.top;Binding {target: contentLoader.itemproperty: "anchors.fill"value: contentLoader}}
}

使用:

Panel {headerComponent: Rectangle {color: "black"}footerComponent: Rectangle {color: "black"}contentComponent: ListView {delegate: Rectangle { width: parent.width; height: 40; color: "green" }model: 10}
}

全局单例模式

  1. 入口文件的 id 和属性

  2. 静态 JavaScript 文件

  3. qml 单例,QML 实现

  4. qml 单例,c++ 实现

  5. 注册上下文属性

货币展示

/*** places 保留几位有效小数* symbol 法币符号* thousand 每千为分隔符* decimal 数与小数分隔符*/
Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
    places = !isNaN(places = Math.abs(places)) ? places : 2;symbol = symbol !== undefined ? symbol : "$";thousand = thousand || ",";decimal = decimal || ".";var number = this,negative = number < 0 ? "-" : "",i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",j = (j = i.length) > 3 ? j % 3 : 0;return symbol + negative + (j ? i.substr(0, j) + thousand : "")+ i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand)+ (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");};var revenue = 12345678;
console.log(revenue.formatMoney()); // $12,345,678.00
console.log(revenue.formatMoney(0, "HK$ ")); // HK$ 12,345,678