TableView with QSqlRelationalTableModel cells are all "2"
-
(I'll put my qml code in the post reply)
root.currentSqlTableModel is a class derived from QSqlRelationalTableModel with nothing else implemented.
The HorizontalHeaderView and VerticalHeaderView can properly show, but the TableView only shows all cells of the number "2" which is weird. I also tried to remove the overridedata()
function but nothing changed (and I cannot jump to the breakpoints in data() function when loading the TableView). -
The problem is that you are accessing your role as a context property, that's not recommended since it is implicit and can cause shadowing issues like here.
You are using an
ItemDelegate
as a delegate, it inheritsAbstractButton
and it therefore has adisplay
property. The 2 you see correspond toAbstractButton.TextBesideIcon (default)
.The preferred way to map roles to delegate is now with required property. Since you have a name clashing with
display
you can't really declare it as a required. You can usemodel
instead (or not useItemDelegate
):delegate: ItemDelegate { id: delegate required property var model Text { text: delegate.model.display anchors.fill: parent anchors.margins: 10 color: 'black' font.pixelSize: 15 verticalAlignment: Text.AlignVCenter } }
Just FYI, a Text is not really needed here since ItemDelegate can display text lready, here's a proposed alternative:
delegate: ItemDelegate { required property var model text: model.display padding: 10 font.pixelSize: 15 }
-
Rectangle { color: Colors.surface1 HorizontalHeaderView { id: horizontalHeader anchors.left: sqlTableView.left anchors.top: parent.top syncView: sqlTableView clip: true } VerticalHeaderView { id: verticalHeader anchors.top: sqlTableView.top anchors.left: parent.left syncView: sqlTableView clip: true } TableView { id: sqlTableView anchors.left: verticalHeader.right anchors.top: horizontalHeader.bottom anchors.right: parent.right anchors.bottom: parent.bottom ... clip: true model: root.currentSqlTableModel delegate: ItemDelegate { Text { text: display anchors.fill: parent anchors.margins: 10 color: 'black' font.pixelSize: 15 verticalAlignment: Text.AlignVCenter } } } }
-
Hi,
How are you setting up your models ?
How did you implement them ? -
@SGaist This is my impl of table model:
SqlTableModel.h:#include <QQmlEngine> #include <QtSql/QSqlRelationalTableModel> class SqlTableModel : public QSqlRelationalTableModel { Q_OBJECT QML_ELEMENT public: using QSqlRelationalTableModel::QSqlRelationalTableModel; }}
then I import this model to my qml and defined
pragma ComponentBehavior: Bound ApplicationWindow { id: root property SqlTableModel currentSqlTableModel ... }
-
@Yihua-Liu
OK, but what's in the data? Quite outside of QML why don't you print out what's in each column of each row fromdata()
method, so we can see they are not2
? What happens if you replaceQSqlRelationalTableModel
with aQSqlTableModel
? What if you use aQStandardItemModel
? Need to pin down where the issue lies, e.g. in the back-end or the front-end. -
@JonB Hi, as I mentioned above, no matter I write a override data() function or not, the program just does not execute data() function. For example, if I write qDebug() in
[[nodiscard]] QVariant data(const QModelIndex& index, int role) const override;
function, there will be no outputs. Besides, I checked the data when initializing my SQL table model bytable_model->record(row).value(field_name).toString()
and the values are exactly the same as the values in the database, so it seems not to be a backend issue, but I'm not sure the issue is front-end or interface because I could not find a way to get what display is :( -
Does this example help?
-
@Yihua-Liu
Glancing at the code, if you make sure you do haveHAVE_QT5
defined and you changeQDeclarativeContext
toQQmlContext
per https://doc.qt.io/qt-5/qtquick-porting-qt5.html#c-code does it still not work/compile?Like I said, I don't use QML but is the issue of getting the right data to do with setting the correct roles for QML?
-
The problem is that you are accessing your role as a context property, that's not recommended since it is implicit and can cause shadowing issues like here.
You are using an
ItemDelegate
as a delegate, it inheritsAbstractButton
and it therefore has adisplay
property. The 2 you see correspond toAbstractButton.TextBesideIcon (default)
.The preferred way to map roles to delegate is now with required property. Since you have a name clashing with
display
you can't really declare it as a required. You can usemodel
instead (or not useItemDelegate
):delegate: ItemDelegate { id: delegate required property var model Text { text: delegate.model.display anchors.fill: parent anchors.margins: 10 color: 'black' font.pixelSize: 15 verticalAlignment: Text.AlignVCenter } }
Just FYI, a Text is not really needed here since ItemDelegate can display text lready, here's a proposed alternative:
delegate: ItemDelegate { required property var model text: model.display padding: 10 font.pixelSize: 15 }
-