我正在創建自己的 SDDM 主題。在開發程序中,我注意到 userModel.lastUser 在測驗模式下包含一個空字串。我檢查了其他 SDDM 主題——同樣的事情。在正常模式下一切正常。我怎樣才能解決這個問題?
按照官方檔案做的一切:https ://github.com/sddm/sddm/wiki/Theming
我的代碼片段:
Item {
id: root
signal selected(var userName)
property string userName: userModel.lastUser
property string currentIconPath: usersList.currentItem.iconPath
property string currentUserName: usersList.currentItem.userName
ListView {
id: usersList
model: userModel
delegate: Text {
id: userItem
visible: false
text: model.name
function select() {
selected(name)
usersList.currentIndex = index
currentIconPath = icon
currentUserName = name
}
Component.onCompleted: {
if (name === userModel.lastUser) {
userItem.select()
}
console.log(userModel.lastUser)
//userItem.select()
}
}
}
...
uj5u.com熱心網友回復:
我對您的應用程式進行了以下小調整:
- 我消除了
lastUser- 我在這個屬性中找不到意義 - 我更新
currentUserName,currentIconPath當它被選中時,我保留了這部分 - 我將選擇改寫為基于閱讀和寫作
currentIndex。使用currentIndex, 的好處是該ListView控制元件具有內置支持Key_Up和Key_Down按鍵只要ListView控制元件是當前的焦點控制元件 - 我引入了一個 onClicked 處理程式,以便
currentIndex更新并重新宣告鍵盤焦點 - 不清楚為什么您的代表是 invisible
Text,我將其替換Frame為 anItemDelegate因為我可以(1)提供圖示,(2)更改顏色 - 我重寫
Component.onCompleted以初始化currentIndex到串列模型中的最后一項,因為似乎需要初始化對最后一項的焦點。Component.onCompleted已從代表ListView移至. 這是因為當它在委托上時,它會在每次渲染委托時觸發。想象一下當您滾動瀏覽長串列時這種影響。最后一個可見專案不一定是串列中的最后一個專案
import QtQuick
import QtQuick.Controls
Page {
signal selected()
property alias currentIndex: usersList.currentIndex
property var currentUser: currentIndex >= 0 && currentIndex < userModel.count ? userModel.get(currentIndex) : null
property string currentIconPath: currentUser ? currentUser.iconPath : ""
property string currentUserName: currentUser ? currentUser.userName : ""
ListView {
id: usersList
anchors.fill: parent
model: userModel
delegate: Frame {
width: ListView.view.width
property bool isCurrentItem: ListView.isCurrentItem
background: Rectangle {
color: isCurrentItem ? "steelblue" : "#eee"
border.color: "grey"
}
ItemDelegate {
id: userItem
width: parent.width
icon.source: model.iconPath
icon.color: isCurrentItem ? "white" : "grey"
palette.text: isCurrentItem ? "white" : "black"
text: model.userName
onClicked: {
usersList.currentIndex = index;
usersList.forceActiveFocus();
selected();
}
}
}
Component.onCompleted: currentIndex = model.count - 1
}
ListModel {
id: userModel
ListElement { userName: "Bill Gates"; iconPath: "smile-32.svg" }
ListElement { userName: "Steve Jobs"; iconPath: "user-32.svg" }
ListElement { userName: "Jeff Bezos"; iconPath: "smile-32.svg" }
}
footer: Frame {
Text {
text: "currentIndex: " currentIndex " currentUserName: " JSON.stringify(currentUserName) " currentIconPath: " JSON.stringify(currentIconPath) " "
}
}
}
//smile-32.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M16 29.8A13.8 13.8 0 1 1 29.8 16 13.815 13.815 0 0 1 16 29.8zm0-26.6A12.8 12.8 0 1 0 28.8 16 12.815 12.815 0 0 0 16 3.2zm-4.5 10.6a1.2 1.2 0 0 0 .608-.168 1.52 1.52 0 0 0 .464-.43 1.927 1.927 0 0 0 .278-.572 2.234 2.234 0 0 0 0-1.26 1.927 1.927 0 0 0-.278-.571 1.52 1.52 0 0 0-.464-.431 1.185 1.185 0 0 0-1.216 0 1.52 1.52 0 0 0-.464.43 1.927 1.927 0 0 0-.277.572 2.234 2.234 0 0 0 0 1.26 1.927 1.927 0 0 0 .277.571 1.52 1.52 0 0 0 .464.431 1.2 1.2 0 0 0 .608.168zm9.608-.168a1.52 1.52 0 0 0 .464-.43 1.927 1.927 0 0 0 .278-.572 2.234 2.234 0 0 0 0-1.26 1.927 1.927 0 0 0-.278-.571 1.52 1.52 0 0 0-.464-.431 1.185 1.185 0 0 0-1.216 0 1.52 1.52 0 0 0-.464.43 1.927 1.927 0 0 0-.277.572 2.234 2.234 0 0 0 0 1.26 1.927 1.927 0 0 0 .277.571 1.52 1.52 0 0 0 .464.431 1.185 1.185 0 0 0 1.216 0zm3.223 5.743l-.926-.379a7.863 7.863 0 0 1-7.39 4.976.166.166 0 0 0-.032 0 7.863 7.863 0 0 1-7.388-4.976l-.926.379a8.846 8.846 0 0 0 8.313 5.597.21.21 0 0 0 .035 0 8.848 8.848 0 0 0 8.314-5.597z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
//user-32.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M19.5 15h-7A6.508 6.508 0 0 0 6 21.5V29h20v-7.5a6.508 6.508 0 0 0-6.5-6.5zM25 28H7v-6.5a5.506 5.506 0 0 1 5.5-5.5h7a5.506 5.506 0 0 1 5.5 5.5zm-9-14.2A5.8 5.8 0 1 0 10.2 8a5.806 5.806 0 0 0 5.8 5.8zm0-10.633A4.833 4.833 0 1 1 11.167 8 4.839 4.839 0 0 1 16 3.167z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
您可以在線試用!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521819.html
標籤:qtqmlkde等离子
