2016-08-20 32 views
4

我想將一些樣式應用於我正在處理的新qt 5.7應用程序,而下面的程序根本不起作用。它給出了錯誤: qrc:/SignInView.qml:67不能分配給不存在的屬性「風格」 而我無法在設計模式中出於同樣的原因進行編輯。如何將樣式應用於QML中的TextField?看起來「樣式」屬性不可用

import QtQuick 2.7 
import QtQuick.Controls 2.0 
import QtQuick.Layouts 1.1 
import QtQuick.Controls.Styles 1.4 

Page { 
    id: page1 
    ColumnLayout { 
     id: columnLayout1 
     height: 100 
     anchors.right: parent.right 
     anchors.left: parent.left 
     anchors.top: parent.top 

     Label { 
      text: qsTr("Label") 
      font.pointSize: 16 
      horizontalAlignment: Text.AlignHCenter 
      Layout.fillWidth: true 
     } 

     Image { 
      id: image1 
      width: 200 
      height: 200 
      Layout.alignment: Qt.AlignHCenter | Qt.AlignTop 
      fillMode: Image.PreserveAspectCrop 
      anchors.horizontalCenter: parent 
      source: "qrc:/qtquickplugin/images/template_image.png" 

      Button { 
       id: button1 
       text: qsTr("Button") 
       anchors.bottomMargin: 10 
       anchors.rightMargin: 10 
       anchors.bottom: parent.bottom 
       anchors.right: parent.right 
      } 
     } 

     Rectangle { 
      id: field1 
      width: 200 
      height: 40 
      color: "#ffffff" 
      Layout.fillWidth: true 



      Label { 
       id: label1 
       text: qsTr("Full Name") 
       anchors.topMargin: 0 
       anchors.left: parent.left 
       anchors.leftMargin: 5 
       anchors.top: parent.top 
      } 
      TextField { 
       style: TextFieldStyle { 
        textColor: "black" 
        background: Rectangle { 
         radius: 2 
         implicitWidth: 100 
         implicitHeight: 24 
         border.color: "#333" 
         border.width: 1 
        } 
       } 
      } 
     } 
    } 
} 

我一直是想以此爲榜樣:

http://doc.qt.io/qt-5/qml-qtquick-controls-styles-textfieldstyle.html

它未能在Qt Creator中給予該樣式不存在錯誤的樣式屬性。 我認爲這是我的圖書館沒有加載或可能是我設置的環境。 我沒有按鈕或任何其他地方的風格。我假設如果我有進口它會工作,但事實並非如此。

SO上的一個相關問題在這裏:QML - How to change TextField font size 但是,這裏似乎只是工作。

回答

15

在Qt Quick Controls 2中,沒有像TextField::style這樣的屬性。一般來說,沒有辦法使用Qt Quick Controls 1和Qt Quick Controls 2中的樣式對象。兩個主要版本的Qt Quick Controls之間的API不兼容。請參見以下文檔頁瞭解更多詳情:

一個新的API不兼容主要版本被引入,因爲基本上沒有辦法使重基於加載程序的Qt Quick Controls 1架構表現相當不錯。因此,所有動態加載的Component s都在Qt Quick Controls 2中被丟棄。曾經動態加載的樣式對象提供的動態實例化的代理現在成爲控件的一部分,而不是實例化的「就地」。從本質上說:

TextField { 
    style: TextFieldStyle { 
     textColor: "white" 
     background: Rectangle { color: "black" } 
    } 
} 

TextField { 
    color: "white" 
    background: Rectangle { color: "black" } 
} 

你可以閱讀更多有關歷史here。特別是this post強調了Qt Quick Controls 2中的基本結構變化。

相關問題