My TableView Doesn't work
-
Hi everybody;
Who can help me to solve my problem.cpp file :
@
QQmlApplicationEngine engine;
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("Select doc_number,first_name,last_name from document");
model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "First Name");
model->setHeaderData(2, Qt::Horizontal, "Last Name");engine.rootContext()->setContextProperty("myModel", model);
@
And this is my QML file :
@
TableView {anchors.fill: parent model: myModel }
@
After run Tableview is empty !
-
Hi,
Did you setup the database connection properly ?
-
[quote author="SGaist" date="1400012262"]Hi,
Did you setup the database connection properly ?[/quote]
Hi again and thank you for your reply SGaist
My setup is good work see :@
bool OpenConnection(){
try
{QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("test"); db.setUserName("root"); db.setPassword(""); //Exception error rules. qDebug() << QSqlDatabase::database().lastError(); QString vMessege; vMessege = QSqlDatabase::database().lastError().text(); QMessageBox msgBox; msgBox.setWindowTitle("Database SQL Exception Error !"); //msgBox.setIcon(QMessageBox::Icon::Critical); msgBox.setText(vMessege); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); if (!db.open()) { msgBox.exec(); return false; } } catch (...) { } return true;
}
@and my main file :
@
if (OpenConnection())
{QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("Select doc_number,first_name,last_name from document"); model->setHeaderData(0, Qt::Horizontal, "ID"); model->setHeaderData(1, Qt::Horizontal, "First Name"); model->setHeaderData(2, Qt::Horizontal, "Last Name"); engine.rootContext()->setContextProperty("myModel", model); } engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
@
-
You should check what model->lastError() returns
-
[quote author="SGaist" date="1400053480"]You should check what model->lastError() returns[/quote]
Check this ScreenShoot , anything is okey but doesn't work !
http://persianupload.com/kleeja/do.php?imgf=140005525273092.jpg -
And you are sure the table contains something ?
-
CMIIW, I think QSqlQueryModel doesn't work out of the box with QML.
The reason is because QML model (as of now) has no notion of columns, but roles.
In other words, you must set rolenames to display for each column for your tableview, and your model must return data in terms of roles.
(see QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole) with your roles set from Qt::UserRole onwards."This Wiki Entry":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML showed how to wrap your model in a way compatible with QML
Why this very unintuitive discrepancy happened I do not know, and I surely want to know why as well
Hope this helps
-
I'm getting old… lack is right, using c++ models with QML requires a bit of work.
You can find the documentation about it "here":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html
-
I test this code for check is good work...
@
QStringList dataList;
dataList.append("Item 1");
dataList.append("Item 2");
dataList.append("Item 3");
dataList.append("Item 4");
engine.rootContext()->setContextProperty("myModel", dataList);@
but now i want to show from database. ...
@
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("Select doc_number,first_name,last_name from document");
model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "First Name");
model->setHeaderData(2, Qt::Horizontal, "Last Name");
engine.rootContext()->setContextProperty("myModel", model);
@doesn't work !!! Why ! what do i need on my QSqlQueryModel code ?
-
I already explained above.
It's not gonna work as-is.QSqlQueryModel works with rows and COLUMNS.
QML views works with rows and ROLES.A Stringlist is just a list, it will work. What you need to do, is make your model look like a list with roles.
Read "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
-
[quote author="lack" date="1400243967"]I already explained above.
It's not gonna work as-is.QSqlQueryModel works with rows and COLUMNS.
QML views works with rows and ROLES.A Stringlist is just a list, it will work. What you need to do, is make your model look like a list with roles.
Read "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML[/quote]
Hi again and thank you for reply... but i try it on Qt 5.2 doesn't work !
Do you test it for yourself ? I need another example. -
What have you tried and doesn't work? We need more information. 'Doesn't work' is not helpful.
If you have read the article, right at the end he showed a generic class extending QSqlQueryModel that is usable from QML.
I tested it against Qt 5.2, and yes it does work. Granted, with little work as QAbstractItemModel::setRoleNames was deprecated, you have to reimplement QAbstractItemModel::roleNames instead, but that is 1 minute work.