Model Test: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (Added missing scope)
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''English''' [[Model Test Spanish|Spanish]]


=Model Test=
'''English''' [[Model_Test_Spanish|Spanish]]


ModelTest provides a way to check for common errors in implementations of [http://doc.qt.nokia.com/qabstractitemmodel.html QAbstractItemModel] ''[doc.qt.nokia.com]''.
 
 
ModelTest provides a way to check for common errors in implementations of [http://doc.qt.io/qt-5/qabstractitemmodel.html QAbstractItemModel].


ModelTest continuously checks a model as it changes, helping to verify the state and catching many common errors the moment they show up.
ModelTest continuously checks a model as it changes, helping to verify the state and catching many common errors the moment they show up.
Line 14: Line 15:
* The parent of the first index of the first row is a QModelIndex()
* The parent of the first index of the first row is a QModelIndex()
* Calling index() twice in a row with the same values will return the same QModelIndex
* Calling index() twice in a row with the same values will return the same QModelIndex
* If rowCount() says there are X number of rows, model test will verify that is true.
* If rowCount() says there are X number of rows, model test will verify that is true.  
* Many possible off by one bugs
* Many possible off by one bugs
* hasChildren() returns true if rowCount() is greater then zero.
* hasChildren() returns true if rowCount() is greater then zero.
Line 21: Line 22:
To use the model test do the following:
To use the model test do the following:


# Include the pri file at the end of your project pro file using the include() command like so:<br />''include(../path/to/dir/modeltest.pri)''
===== Qt 5.11 or later =====
1. Add the QtTest module to your pro file like: ''QT += testlib''
 
2. Then in your source include <QAbstractItemModelTester> and instantiate QAbstractItemModelTester with your model so the test can live for the lifetime of your model. For example:
<code>#include <QAbstractItemModelTester>
 
QDirModel *model = new QDirModel(this);
new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);</code>
 
3. That is it. When the test finds a problem it will assert. The qFatal() output will contain some hints on how to fix problems that the test finds.
 
More infos available [https://www.kdab.com/new-in-qt-5-11-improvements-to-the-model-view-apis-part-2/ here]


# Then in your source include “modeltest.h” and instantiate ModelTest with your model so the test can live for the lifetime of your model. For example: <br />
===== Earlier versions of Qt =====
1. Download the Model test from [https://code.qt.io/cgit/qt/qt.git/tree/tests/auto/modeltest here (Qt 4)] or [https://code.qt.io/cgit/qt-creator/qt-creator.git/plain/src/shared/modeltest/ here (Qt 5)]


# That is it. When the test finds a problem it will assert. modeltest.cpp contains some hints on how to fix problems that the test finds.
1. Include the pri file at the end of your project pro file using the include() command like so:
''include(../path/to/dir/modeltest.pri)''


The source can be found [http://qt.gitorious.org/qt/qt/trees/4.7/tests/auto/modeltest here] ''[qt.gitorious.org]''
2. Then in your source include "modeltest.h" and instantiate ModelTest with your model so the test can live for the lifetime of your model. For example:
<code>#include <modeltest.h>


===Categories:===
QDirModel *model = new QDirModel(this);
new ModelTest(model, this);</code>


* [[:Category:Qt Labs Projects|Qt_Labs_Projects]]
3. That is it. When the test finds a problem it will assert. modeltest.cpp contains some hints on how to fix problems that the test finds.

Latest revision as of 19:14, 23 July 2019

English Spanish


ModelTest provides a way to check for common errors in implementations of QAbstractItemModel.

ModelTest continuously checks a model as it changes, helping to verify the state and catching many common errors the moment they show up.

Modeltest.png

Some of the conditions caught include:

  • Verifying X number of rows have been inserted in the correct place after the signal rowsAboutToBeInserted() says X rows will be inserted.
  • The parent of the first index of the first row is a QModelIndex()
  • Calling index() twice in a row with the same values will return the same QModelIndex
  • If rowCount() says there are X number of rows, model test will verify that is true.
  • Many possible off by one bugs
  • hasChildren() returns true if rowCount() is greater then zero.
  • and many more…

To use the model test do the following:

Qt 5.11 or later

1. Add the QtTest module to your pro file like: QT += testlib

2. Then in your source include <QAbstractItemModelTester> and instantiate QAbstractItemModelTester with your model so the test can live for the lifetime of your model. For example:

#include <QAbstractItemModelTester>

QDirModel *model = new QDirModel(this);
new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);

3. That is it. When the test finds a problem it will assert. The qFatal() output will contain some hints on how to fix problems that the test finds.

More infos available here

Earlier versions of Qt

1. Download the Model test from here (Qt 4) or here (Qt 5)

1. Include the pri file at the end of your project pro file using the include() command like so: include(../path/to/dir/modeltest.pri)

2. Then in your source include "modeltest.h" and instantiate ModelTest with your model so the test can live for the lifetime of your model. For example:

#include <modeltest.h>

QDirModel *model = new QDirModel(this);
new ModelTest(model, this);

3. That is it. When the test finds a problem it will assert. modeltest.cpp contains some hints on how to fix problems that the test finds.