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

Using QSqlQueryModel in QML without reimplement it [Solved]



  • Hi all,
    I'm starting with QML and I'm trying with a simple "example":http://developer.qt.nokia.com/wiki/How_to_use_a_QSqlQueryModel_in_QML.

    Now I'd like to know if it's possible to use QSqlQueryModel without the need of subclass it.



  • Yes, you can use QSqlQueryModel directly, but note that you can only access the first column of data.



  • My qml file is:
    @
    import QtQuick 1.0
    import Qt 4.7

    Rectangle {
    color: "lightblue"
    Component {
    id: name_delegate
    Text {
    text: "ciao"
    }
    }

    ListView {
        clip: true
        anchors.margins: 10
        anchors.fill: parent
        model: queryModel
        delegate: name_delegate
    }
    

    }
    @

    in my main window constructor I have:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QSqlQueryModel *queryModel;
    queryModel = new QSqlQueryModel(this);
    queryModel->setQuery("select descrizione from lista_dati;");
    
    m_declarativeView = new QDeclarativeView(this);
    m_declarativeView->rootContext()->setContextProperty("queryModel", queryModel);
    setCentralWidget(m_declarativeView);
    m_declarativeView->setSource(QString("qrc:rappresentazionegraficamagazzino.qml"));
    m_declarativeView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    

    }
    @

    How can I access the query column?



  • No one can explain how to access QSqlQueryModel directly?

    For me it's also usefull to have some documentation about...



  • As Andre said you can access only first column of data. To access all columns you need to subclass it and make all columns visible at first column via different roles.



  • Then again, his query only returns a single column of data anyway, so that should not be a huge issue.

    It seems to me at first sight, that what you are doing is correct. Perhaps "this page":http://doc.trolltech.com/4.7.1/qdeclarativemodels.html#qabstractitemmodel in the documentation is of help: you need to use the role names. The default role name for the DisplayRole is display.



  • [quote author="Andre" date="1302851464"]The default role name for the DisplayRole is display. [/quote]

    Thanks Andre. I needed this. Now It works.

    Where should I find the role name you posted me in the documentation?



  • [quote author="Luca" date="1302950174"][quote author="Andre" date="1302851464"]The default role name for the DisplayRole is display. [/quote]

    Thanks Andre. I needed this. Now It works.

    Where should I find the role name you posted me in the documentation?[/quote]

    I found it in the link you posted...



  • Hi Luca, Andre someone may update the link that talks about role names? I'm facin the same problem.
    thanks


Log in to reply