2015-09-17 2 views
0

У меня есть ListView, включая много переключателей. Список больше видимой области. Один из переключателей отмечен. Иногда, если кнопка выбрана радио находится за пределами видимой области, я хочу, чтобы перейти к нему:Как вертикальная прокрутка к определенному товару

ScrollView { 
    anchors.fill:parent 
    ListView { 
     anchors.fill: parent 
     model: valuesList 
     delegate: RadioButton { 
      id: radioBtn 
      //check of value is index type and do the corresponding checked? test 
      checked: valueIsIndex ? (parseInt(valueFromParent) == index ? true : false) : (valueFromParent == valueString ? true : false) 
      onClicked: { 
       root.selected(valueString, index) 
      }  
      Component.onCompleted: { 
       if(checked) 
       //Here i want to scroll the list to display this radiobutton 
      } 
     } 
    } 
} 

Любые идеи, Howto, чтобы получить список прокручивается? Я много играл с hightlights и contentY, но ничего не получилось.

Я использую ScrollView вокруг ListView, чтобы автоматически получать полосы прокрутки на рабочем столе. На мобильных устройствах у меня есть только потрясающий ListView.

EDIT

я получить его на пути с помощью BaCaRoZzo. Вот мой текущий рабочий пример:

ScrollView { 
    id: scrollView 
    anchors.fill:parent 
    property int yOfCheckedRadioButton: 0 
    ListView { 
     id:listView 
     anchors.fill: parent 
     spacing: Math.round(appWindow.height*0.05) 
     model: internalValuesList 
     delegate: RadioButton { 
      id: radioBtn 
      //check of value is index type and do the corresponding checked? test 
      checked: checktest() 
      style: MyRadioButtonStyle { 
       myRadioBtn: radioBtn 
       labelString: value 
      } 
      Component.onCompleted: { 
       //set the position of the checked RadioButton to scroll to it later onContentHeightChange 
       if(checked) { 
        var checkedRadioBtnPositionY = Math.round((radioBtn.height + listView.spacing) * index - radioBtn.height * 1.5) 
        if(checkedRadioBtnPositionY > 0) 
         scrollView.yOfCheckedRadioButton = checkedRadioBtnPositionY 
        else 
         scrollView.yOfCheckedRadioButton = 0 
       } 
      } 
     } 
     onContentHeightChanged: { 
      //scroll to the checked RadioButton 
      contentY = scrollView.yOfCheckedRadioButton 
     } 
    } 
} 
+0

'ContentY' должно работать (как показано, например, [этот ответ] (http://stackoverflow.com/a/30378458/2538363)). Вы должны обязательно учитывать присутствие «ListView» здесь. – BaCaRoZzo

+0

В качестве основного теста я попытался следовать в конце 'ListView': ' Component.onCompleted: { contentY = 200 } ' Но это тоже ничего не меняет. Что я делаю неправильно? – fhammer

+0

Не нравится. [Это] (http://pastebin.com/embed_js.php?i=EwT9nfRH) работает – BaCaRoZzo

ответ

0

я помню проблемы с свитка, прежде чем Qt 5.4, когда я нашел обходной путь, как:

 ScrollView { 
      anchors.fill: parent // mind how you stretch it 
      contentItem: 
       Flow { 
        id: flow 
        spacing: 10 // mind gaps 
        width: parent.parent.width - 20 // select proper width 

        // Put anything you would like to scroll in here 
        // Mind that Flow positions items one after another 
        // left to right, top to bottom 

        // You can also try containers other than Flow 
        // but whether it works or not may depend on Qt version 

        ExclusiveGroup { id: tabPositionGroup } 
        RadioButton { 
         text: "RB1" 
         checked: true 
         exclusiveGroup: tabPositionGroup 
        } 
        RadioButton { 
         text: "RB2" 
         exclusiveGroup: tabPositionGroup 
        } 
       } 
     } 

ли ScrollView нужен явный contentItem или нет, это другой вопрос, и это, конечно, может не понадобиться, но это не повредит, если SrollView должен решить, что он на самом деле прокручивает.

+0

Кажется, что это не работает, когда я пытаюсь обернуть 'ScrollView' вокруг' ListView'. Я попытался поместить «ListView» в «Flow», но затем он просто показывает кнопку ONE. – fhammer

+0

Что делать, если вы добавили некоторые переключатели, как в фиксированном примере? Непосредственно к потоку? Я использовал такой шаблон для разных типов контента, включая динамический. – AlexanderVX

+0

И я думаю, что я пропустил ширину для Потока. Также исправлено. – AlexanderVX

Смежные вопросы