1. 程式人生 > >Qt qml ListView 滑鼠點選高亮,縮放等基礎設定

Qt qml ListView 滑鼠點選高亮,縮放等基礎設定

import QtQuick 2.0
import QtQuick.Controls 2.0


 Rectangle {
     id: mainrec

     property int widthx: 170
     property int heightx:800

     width: widthx; height: heightx
     color: "lightgray"

     Component{
         id:listdelegate
         Item{
             id:delegateitem
             width: widthx
             height:widthx
             Column{
                 Text{text:name;color:"#0000ff";font.pointSize: 14}
                 Text{text:number;color:"#000000";font.pointSize: 10}
                 Image{ width: 120; height: 120;source:img}
             }
             states: State {
                 name: "Current"
                 when: delegateitem.ListView.isCurrentItem
                 PropertyChanges { target: delegateitem; x:20;scale: 1.1}
             }
             transitions: Transition {
                 NumberAnimation { easing.type: Easing.Linear; properties: "x"; duration: 200 }
             }

             MouseArea {
                 anchors.fill: parent
                 onClicked:{
                      delegateitem.ListView.view.currentIndex = index
                 }
             }
         }
     }

     Component{
         id:highlightrec
         Rectangle{
             width: widthx
             height:widthx
             color: "#f34b08"
             radius: 5
             border.width: 1
             border.color: "#60f50a"
         }
     }

     ListView{
         id:listinfo
         anchors.fill: parent
         focus: true
         highlight:highlightrec

         highlightFollowsCurrentItem :true
         model:listModelData
         delegate:listdelegate
     }

     ListModel{
         id:listModelData

         ListElement{
             name:"小明"
             number:"4568467"
             img:"qrc:/res/head5.png"
         }

         ListElement{
             name:"YY"
             number:"2741869"
             img:"qrc:/res/head2.png"
         }

         ListElement{
             name:"鄒民兵"
             number:"986153"
             img:"qrc:/res/head3.png"
         }

         ListElement{
             name:"馬超"
             number:"39148209"
             img:"qrc:/res/head4.png"
         }

         ListElement{
             name:"小明"
             number:"4568467"
             img:"qrc:/res/head5.png"
         }

         ListElement{
             name:"YY"
             number:"2741869"
             img:"qrc:/res/head2.png"
         }

         ListElement{
             name:"鄒民兵"
             number:"986153"
             img:"qrc:/res/head3.png"
         }

         ListElement{
             name:"馬超"
             number:"39148209"
             img:"qrc:/res/head4.png"
         }

         ListElement{
             name:"小明"
             number:"4568467"
             img:"qrc:/res/head5.png"
         }

         ListElement{
             name:"YY"
             number:"2741869"
             img:"qrc:/res/head2.png"
         }

         ListElement{
             name:"鄒民兵"
             number:"986153"
             img:"qrc:/res/head3.png"
         }

         ListElement{
             name:"馬超"
             number:"39148209"
             img:"qrc:/res/head4.png"
         }

     }
 }


#include<QGuiApplication>
#include<QQmlApplicationEngine>
#include<QtQuick/QQuickView>
#include<QListWidget>
intmain(intargc,char*argv[])
{
QGuiApplicationcoreApp(argc,argv);
//QQmlApplicationEngineqmlEngine;
//qmlEngine.load(QUrl(QLatin1String("qrc:/main.qml")));
QQuickView
viwer;
viwer.setSource(QUrl("qrc:/main.qml"));
viwer.setVisible(true);
QQuickItem*rootItem=viwer.rootObject();
returncoreApp.exec();
}