【问题标题】:display text in tableviewcolumn in treeView QtQuick controls 1.4在treeView QtQuick控件1.4中的tableviewcolumn中显示文本
【发布时间】:2016-01-20 18:13:02
【问题描述】:

我试图创建一个出现在 Qt 5.5 中的简单 treeView,问题是树的项目中的文本不显示,尽管模型已满。

这是我的代码:

TreeViewPM.qml

Qt 代码: 切换视图

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQml.Models 2.2
import KMTreeModelPM 1.0

Item {

    KMTreeModelPM {
        id: treeModel
    }

    ItemSelectionModel {
        id: sel
        model: treeModel
    }
    Text {
        id:txt
        text: " "
    }


    TreeView {
        id: view
        anchors.fill: parent
        anchors.margins:  12
        selection: sel
        headerVisible : false
        itemDelegate: Rectangle {
                   color: ( styleData.row % 2 == 0 ) ? "white" : "lightblue"
                   height: 20

                   Text {
                       anchors.verticalCenter: parent.verticalCenter
                       anchors.left: parent.left // by default x is set to 0 so this had no effect
                       text: styleData.value
                   }
               }
        TableViewColumn {
            id : title
            title: "Title"
            role: "title"
            resizable: true
            horizontalAlignment : Text.AlignLeft
        }
        model: treeModel

        onDoubleClicked: txt.text = treeModel.data(index,0) 

    }

}

要复制到剪贴板,请将视图切换到纯文本模式

main.qml

Qt 代码: 切换视图

import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQml.Models 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.4

Rectangle {
    width: 785
        TabView {
            x: 17
            y: 8
            width: 970
            height: 800
            currentIndex: 2
            visible: true
            Tab {
                id: tabAddProfile
                title: qsTr("Add Profile")
                AddProfilePage{}
            }
            Tab {
                id: tabAddTypeSubType
                title: qsTr("Add Type/SubType")
                AddTypeSubTypePage{}
            }
            Tab {
                id: tabAddDetail
                height: 413
                visible: true
                title: qsTr("Add Detail")
                TreeViewPM {}
            }

        }

    }

【问题讨论】:

    标签: qml qtquickcontrols qt5.5


    【解决方案1】:

    这可能是你的模型。很难知道我们何时无法访问它。

    以下作品:

    #include <QGuiApplication>
    #include <QtQml>
    #include <QDebug>
    #include <QtGui>
    
    static void initStandardTreeModel(QStandardItemModel *model)
    {
        QStandardItem *item;
        item = new QStandardItem(QLatin1String("Row 1 Item"));
        model->insertRow(0, item);
    
        item = new QStandardItem(QLatin1String("Row 2 Item"));
        item->setCheckable(true);
        model->insertRow(1, item);
    
        QStandardItem *childItem = new QStandardItem(QLatin1String("Row 2 Child Item"));
        item->setChild(0, childItem);
    
        item = new QStandardItem(QLatin1String("Row 3 Item"));
        item->setIcon(QIcon());
        model->insertRow(2, item);
    }
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QStandardItemModel* model = new QStandardItemModel();
        initStandardTreeModel(model);
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("theModel", model);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    Window {
        width: 800
        height: 400
        visible: true
    
        TreeView {
            id: view
            anchors.fill: parent
            anchors.margins:  12
    
            TableViewColumn {
                id : title
                title: "Title"
                role: "display"
            }
            model: theModel
        }
    }
    

    【讨论】:

    • 我已经尝试过你的代码(在 PyQt 中),但仍然没有显示文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多