【问题标题】:How can I add the text on the top of the gridview in QML?如何在 QML 的 gridview 顶部添加文本?
【发布时间】:2018-03-22 03:23:24
【问题描述】:

我正在我的程序中设计一个布局。我想在 gridview 的顶部添加一个文本,但我只能让它们重叠在一起。如何在 gridview 顶部添加文本?谢谢。

我的代码:

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtMultimedia 5.8
import QtQuick.Layouts 1.3
import com.contentplayermod.filemodel 1.0
import QtQuick.Window 2.2

ApplicationWindow {
    id:main_win
    visible: true
    width: 640
    height: 480
    title: qsTr("Player")
    property int idx: 0
    property bool isActive: true

    Row {
        Text  {
            id:text1
            anchors.bottom: grid_main.top
            width: 240
            height: 35
            text: myModel.folder.toString()
            font.family: "Helvetica"
            font.pointSize: 20
            color: "blue"
            focus: true

        }
    }


     GridView {
                       id:grid_main
                       anchors.fill: parent
                       anchors.top: text1.bottom
                       cellWidth: 100; cellHeight: 100
                       focus: true
                       currentIndex: 0
                     ...

     }

【问题讨论】:

    标签: qt gridview qml qt5


    【解决方案1】:

    你的问题是在GridView中你设置anchors的方式是冲突的,一方面你表明它占据了main_win的空间,另一方面你表明顶部anchor是Text项目。

    您必须做的是GridView 的顶部是文本,而其他锚点来自父级。

    ApplicationWindow {
        id:main_win
        visible: true
        width: 640
        height: 480
        title: qsTr("Player")
        property int idx: 0
        property bool isActive: true
    
        Text  {
            id:text1
            anchors.top: parent.top
            width: 240
            height: 35
            text: myModel.folder.toString()
            font.family: "Helvetica"
            font.pointSize: 20
            color: "blue"
            focus: true
    
        }
    
        GridView {
            id: grid_main
            // annchors
            anchors{
                bottom: parent.bottom
                right: parent.right
                left: parent.left
                top: text1.bottom
            }
            cellWidth: 100; cellHeight: 100
            focus: true
            currentIndex: 0
            ...
        }
    

    【讨论】:

    • 我试过你的方法,但是文字消失了。只显示一个网格视图。
    • @OPfan 我刚试了,你说得对,我去看看是什么问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多