• 10082阅读
  • 3回复

[提问]QML TableView添加列问题 [复制链接]

上一主题 下一主题
离线charles_gz
 

只看楼主 倒序阅读 楼主  发表于: 2014-10-14




我是在动态的添加TableView的列,name列正常显示了,之后的都为“”,是否不可以这样动态的添加?还是我的使用方法问题希望给予指点 谢谢

离线彩阳

只看该作者 1楼 发表于: 2014-10-14
这样暂时看不出问题,如果怀疑还有问题,那么就制作一个小的例子,作为附件上传。我来看看。
上海Qt开发联盟,热忱地欢迎你的加入!
离线toby520

只看该作者 2楼 发表于: 2014-10-16
import QtQuick 2.0
import QtQuick.Controls 1.1
ApplicationWindow {
    visible: true
    width: 640
    height: 480
    property string roleName:"buyer";
    property string titleName: "Another Column"
    property string strTestColumn : "import QtQuick 2.1; import QtQuick.Controls 1.0; import QtQuick.Layouts 1.0; TableViewColumn{id:idTestColumn; role:\""+roleName+"\"; title: \""+titleName+"\" ; width: 200 ; Component.onCompleted: {console.log(\"#\");libraryModel.count}}"
    property int ic: 0

    ListModel {
        id: libraryModel
        dynamicRoles: true
        Component.onCompleted: {
            console.log("hell0---------------------------------0")
            append({title: "A Masterpiece" , author: "Gabriel" , pages: 500, mc: "1"})
            append({title: "Brilliance"    , author: "Jens"    , pages: 700, mc: "2"})
            append({title: "Outstanding"   , author: "Frederik2", pages: 600, mc: "3"})
            append({title: "Outstanding2"   , author: "Frederik3", pages: 600, mc: "3"})
        }
    }

    TableView{
        id:idTable
        x: 10
        y: 50
        width:parent.width
        model: libraryModel                             // Table Data model
        TableViewColumn{ role: "title"  ; title: "Title" ; width: 100 }
        TableViewColumn{ role: "author" ; title: "Author" ; width: 200 }

        Component.onCompleted: {
            console.log("hell0---------------------------------2");
        }
    }

    function addCloumn(propstr,str){
        var ii= 0
        //it starts at zero "0"
        console.log("hell0---------------------------------1 "+propstr)
        for( ii=0; ii < libraryModel.count;ii++)
        {
            libraryModel.setProperty(ii, propstr, propstr+ii);
            console.log(libraryModel.get(ii).propstr + "\n");
        }
        idTable.addColumn(Qt.createQmlObject(str,idTable,'firstObject'));
        idTable.model = null;
        idTable.model = libraryModel;
    }

    Component.onCompleted: {
        // addCloumn();
    }
    //Data model for Table

    MouseArea{
        id:ma
        anchors.fill: parent
        onClicked: {
            ic++;
            var strqml="";
            var time = new Date();
            if(ic%2==0){
                roleName = "buyer"+time.getSeconds();
                titleName = "BUYER";
                strqml = strTestColumn;

            }else{
                roleName = "seller"+time.getSeconds();;
                titleName = "SELLER";
                strqml = strTestColumn;
                //addCloumn(roleName,strSecondColumn);
            }
            addCloumn(roleName,strqml);
        }
    }
}
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
离线charles_gz

只看该作者 3楼 发表于: 2014-11-04
回 toby520 的帖子
toby520:import QtQuick 2.0
import QtQuick.Controls 1.1
ApplicationWindow {
    visible: true
    width: 640
....... (2014-10-16 14:05) 

感谢,
快速回复
限100 字节
 
上一个 下一个