Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to show more than 1 column in QSqlQueryModel



  • Hi everyone,

    I have been bashing my head for a week now while trying to understand the best way to display sql data in qml.

    I have sub-classed QSqlQueryModel (I tried with QAbstractTableModel too, but didn't understand how to get it working.)

    //qml
                            Button {
                                anchors.fill:parent
                                text: modelData
                                onClicked: getCol(modelData)
                                function getCol(tab){
                                    aSqlDB.selectTable(tab);
                                    columnlist.visible = true;
                                    columnlist.model = aSqlDB.getData();
                                }
                ListView {
                    id: columnlist
                    width: 600
                    height: 25
                    y:25
                    z:1
                    visible: false
                    orientation: ListView.Horizontal
    
                    delegate: Rectangle {
                        height: 25
                        width: 100
                            Button {
                                anchors.fill:parent
                                text: modelData
                                    
                                    }
    
                            }
                        }
    
    //SqlModel cpp
    SqlModel::SqlModel(QObject *parent) : QSqlQueryModel(parent)
    {
    
    }
    
    QVariant SqlModel::data(const QModelIndex &index, int role) const
    {
        QVariant value = QSqlQueryModel::data(index, role);
        return value;
    }
    
    
    //Db cpp where i make the model
    
    
    SqlModel *SqlDB::getData(){
        qDebug() << lasttable();
    
        QString q = "select * from " + lasttable();
    
        SqlModel *query = new SqlModel(this);
    
    
    
        QSqlQuery qr(m_db);
    
        qr.prepare(q);
        qr.exec();
    
        query->setQuery(q, m_db);
    
        return query;
    }
    
    

    How can i make the listview to show all the columns of the query? Do i need to implement rowCount and columnCount too that the listview could get all the data or what is the correct way to approach this problem?

    Thanks,
    Izba

    EDIT:

    Thanks to @wahynes for creating so good travel guide at https://forum.qt.io/topic/40636/how-to-integrate-qsqltablemodel-with-a-tableview-defined-in-qml/4

    I finally got a hang of it.

    I changed from QSqlQueryModel to QSqlTableModel and subclassed it the way wahynes had done it. After that it was little bit tweaking the qml code to generate all the columns.

    Here is my qml if it helps someone who is having same kind of problem :)

    //QML
    
                ListView {
    
                        id: tablelist
                        visible: false;
                        width: (dbview.width - 100)
                        height: 25
                        z: -1
                        orientation: ListView.Horizontal
    
                        delegate: Rectangle {
                            height: 25
                            width: 100
                            layer.enabled: true
                            layer.effect: DropShadow{
                                transparentBorder: true
                                horizontalOffset: 0
                                verticalOffset: 5
                                color: "gray"
                            }
    
                            Button {
                                anchors.fill:parent
                                text: modelData
                                onClicked: getCol(modelData)
                                function getCol(tab){
                                    aSqlDB.selectTable(tab);
    
                                    columnlist.model = aSqlDB.getData();
                                    columnlist.visible = true;
                                    columnlist.g();
                                }
    
                            }
    
                        TableView {
                            id: columnlist
                            visible: false;
    
                            function g() {
                                var columns = aSqlDB.getColumns()
    //Edit2
                                var ccount = columnlist.columnCount;
                                for(var i = ccount; i >= 0; i--){
                                    columnlist.removeColumn(i);
                                }
    //End of edit
                                for(var str in columns)
                                {
                                    var role  = columns[str]
                                    var columnString = 'import QtQuick 2.9; import QtQuick.Controls 1.4; TableViewColumn {role: "' + role + '"; title: "' + role + '"; }';
                                    var column = Qt.createQmlObject(columnString, columnlist)
                                    if (!columnlist.addColumn(column))
                                        console.log("Error column",name)
                                }
                            }
                        }
    
    //And here is my CPP part with the getData and
    
    SqlModel *SqlDB::getData(){
    
        QString q = "select * from " + lasttable();
    
        SqlModel *query = new SqlModel(this, m_db);
    
        query->setTable(lasttable());
        query->select();
        QSqlRecord rec = query->record();
        m_columns.clear();
        for(int i = 0; i< rec.count(); i++){
            m_columns.append(rec.fieldName(i));
        }
        return query;
    }
    
    

    Finally i can focus on other problems.

    Thanks,
    Izba

    EDIT 2: I modified the qml file a litte cause i forgot to always remove the old columns before adding new ones.


Log in to reply