- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 QML MapItemView
组件与基于 C++ QAbstractListModel
的模型一起使用。 MapItemView
在重置模型、添加新项目或删除现有项目时工作正常。但是,MapItemView
并未反射(reflect)对已添加项目的更改。
我第一次遇到这个问题是在 Qt 5.4 上,但在更新到 Qt 5.5 后我仍然遇到这个问题
以下示例显示了 2 个不同模型的问题:一个基于 QAbstractListModel
的 C++ 模型和一个 QML ListModel
。可以从一个模型切换到另一个模型,按下右上角的按钮:
QTimer
每秒修改其内容。无论模型类型是什么,MapItemView
都不会显示模型更改。从一种模型切换到另一种模型时,可以看到 MapView
已更新。
我可能遗漏了一些非常明显的东西,但我不明白它是什么。预先感谢您的帮助。
main.cpp 代码:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "playermodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
PlayerModel playerModel;
engine.rootContext()->setContextProperty("playerModel", &playerModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
C++ 模型头文件 (playermodel.h):
#ifndef PLAYERMODEL_H
#define PLAYERMODEL_H
#include <QObject>
#include <QAbstractListModel>
#include <QGeoPositionInfoSource>
#include <QTimer>
#include <QDebug>
struct PlayerData
{
PlayerData(){ }
PlayerData(int _Azimuth, double lat, double lng){
Azimuth = _Azimuth;
Latitude = lat;
Longitude = lng;
}
int Azimuth = -1;
double Latitude = 0.;
double Longitude = 0.;
QVariant getRole(int role) const;
enum Roles{
RoleAzimuth = Qt::UserRole + 1,
RoleLatitude,
RoleLongitude
};
};
class PlayerModel : public QAbstractListModel
{
Q_OBJECT
public:
PlayerModel();
~PlayerModel();
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
void resetModel();
void updateModel();
public slots:
void testUpdateModel();
protected:
QHash<int, QByteArray> roleNames() const;
private:
QTimer m_timer;
QVector< PlayerData> m_lstValues ;
};
#endif // PLAYERMODEL_H
C++ 模型 (playermodel.cpp)
#include "playermodel.h"
QVariant PlayerData::getRole(int role) const
{
switch (role)
{
case Roles::RoleAzimuth:
return Azimuth;
case Roles::RoleLatitude:
return Latitude;
case Roles::RoleLongitude:
return Longitude;
default:
return QVariant();
}
}
PlayerModel::PlayerModel()
{
resetModel();
connect(&m_timer, SIGNAL(timeout()), this, SLOT(testUpdateModel()));
m_timer.start(1000);
}
PlayerModel::~PlayerModel()
{
}
void PlayerModel::testUpdateModel()
{
updateModel();
}
int PlayerModel::rowCount(const QModelIndex & parent) const
{
Q_UNUSED(parent);
return m_lstValues.size();
}
QVariant PlayerModel::data( const QModelIndex & index, int role ) const
{
if ( (index.row() < 0) || (index.row() >= rowCount()) )
return QVariant();
return m_lstValues[ index.row()].getRole( role);
}
void PlayerModel::resetModel()
{
qDebug() << "Reset players model";
beginResetModel();
m_lstValues.clear();
//populate with dummy value
m_lstValues.push_back( PlayerData( 10, 47.1, -1.6 ));
m_lstValues.push_back( PlayerData( 20, 47.2, -1.6 ));
m_lstValues.push_back( PlayerData( 30, 47.1, -1.5 ));
m_lstValues.push_back( PlayerData( 40, 47.2, -1.5 ));
endResetModel();
}
void PlayerModel::updateModel()
{
qDebug() << "update players model upon timeout";
//change the Azimuth of every model items
int row = 0;
for (PlayerData player : m_lstValues)
{
setData( index(row), (player.Azimuth + 1) % 360, PlayerData::RoleAzimuth);
row++;
}
//qDebug() << "First element azimuth is now : " << data( index(0),PlayerData::RoleAzimuth).toInt() << "°";
}
bool PlayerModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if ( (index.row() < 0) || (index.row() >= rowCount()) ) return false;
PlayerData& player = m_lstValues[ index.row() ];
switch (role)
{
case PlayerData::RoleAzimuth:
player.Azimuth = value.toInt();
break;
case PlayerData::RoleLatitude:
player.Latitude = value.toDouble();
break;
case PlayerData::RoleLongitude:
player.Longitude = value.toDouble();
break;
}
emit dataChanged(index, index );//, QVector<int>( role));
return true;
}
bool PlayerModel::removeRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(count);
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row);
m_lstValues.remove( row);
endRemoveRows();
return true;
}
QHash<int, QByteArray> PlayerModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[PlayerData::Roles::RoleAzimuth] = "Azimuth";
roles[PlayerData::Roles::RoleLatitude] = "Latitude";
roles[PlayerData::Roles::RoleLongitude] = "Longitude";
return roles;
}
Qt::ItemFlags PlayerModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
最后是 QML 文件:
import QtQuick 2.4
import QtQuick.Window 2.2
import QtLocation 5.3
import QtPositioning 5.0
Window {
id:mainWnd
visible: true
width : 1024
height:768
property bool useQMLModel: true
Map {
id: map
anchors.fill: parent
anchors.margins: 50
plugin: Plugin{ name:"osm";}
center: QtPositioning.coordinate(47.1, -1.6)
zoomLevel: map.maximumZoomLevel
MapItemView{
id:mapItemView
model: mainWnd.useQMLModel ? qmlModel : playerModel
delegate: MapQuickItem {
//anchorPoint:
id:delegateMQI
rotation: model.Azimuth
sourceItem: Rectangle{
id:defaultDelegate
width:32
height:32
radius:16
opacity: 0.6
rotation:Azimuth
color:"blue"
Text{
text: Azimuth
anchors.centerIn : parent
}
}
coordinate: QtPositioning.coordinate(Latitude,Longitude)
}
}
MouseArea{
anchors.fill: parent
enabled : useQMLModel
//preventStealing: true
propagateComposedEvents: true
onClicked:
{
//Modify an item
var newAzim = Math.random()*360;
qmlModel.setProperty(0, "Azimuth", newAzim);
//Check modification
console.log("Azim:" + qmlModel.get(0).Azimuth );
qmlModel.setProperty(0, "Color", "blue");
//add a new item
qmlModel.append({"Latitude": 47.05 + Math.random() *0.2, "Longitude":-1.75 + Math.random() *0.3, "Azimuth":0, "Color":"red"})
console.log("Nb item:" + qmlModel.count );
map.update();
map.fitViewportToMapItems();
mouse.accepted = false
}
}
}
Connections{
target:mapItemView.model
onDataChanged:{
if (useQMLModel)
console.log("dataChanged signal Azim:" + qmlModel.get(0).Azimuth );
else
console.log("dataChanged signal Azim:" + playerModel.data( topLeft, 0x0101) );
}
}
ListModel{
id:qmlModel
ListElement {
Latitude: 47.1
Longitude: -1.6
Azimuth: 10.0
}
}
Rectangle{
anchors.top : parent.top
anchors.left : parent.left
width : 400
height : 300
radius: 10
color:"grey"
ListView{
id:lstView
model:mapItemView.model
anchors.fill:parent
delegate: Text{
width:parent.width
height:50
verticalAlignment: TextInput.AlignVCenter
fontSizeMode : Text.Fit
font.pixelSize: 42
minimumPixelSize: 5
text: "Latitude : " + Latitude + " - Longitude :" + Longitude + " - Azimuth : " + Azimuth
}
}
}
Rectangle{
anchors.right : parent.right
anchors.top : parent.top
radius : 10
color : "red"
width : 200
height : 50
Text{
anchors.centerIn: parent
text:"switch model"
}
MouseArea{
anchors.fill: parent
onClicked:{
mainWnd.useQMLModel = !mainWnd.useQMLModel;
}
}
}
}
最佳答案
以防万一有人面临同样的问题issue经博主反馈,问题已在Qt 5.6.0中解决
Note that this is fixed by changeset Ib92252d18c2229bc6d43e11362b8f13cdb48f315 (https://codereview.qt-project.org/#/c/123660/ ) already merged in the 5.6 branch
关于qt - MapItemView 在 dataChanged 信号后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536014/
使用 QTableView和 QAbstractTableModel - 当模型发出 dataChanged对于正在编辑的单元格的事件,用户在单元格中键入的字符串(但未按 Enter 以“提交”编辑)
下面是我添加的行类。它由代码调用,而不是表调用,我希望它在添加新行时正确调用 dataChanged,尽管这不起作用,但表不执行任何操作。我做错了什么? void MyModel::add(const
我的问题如下: 有一个QTableView和一个QStandardItemModel是这样使用的: ui->tableView->setModel(model); model->setItem(myr
我从 QAbstractTableModel 派生了一个模型,现在我想通知一整行的数据已被更改。例如,如果更改了索引为 5 的行的数据(4 列),则使用以下代码将按预期工作。 emit dataCha
我在 QLabel 中显示 QTableWidget 中的项目计数,但每当我进行一些更新时,我都必须手动更新计数(标签)。当我删除或添加行时是否会发出任何信号?我尝试了 cellChanged 和 i
当我在这段代码中复制Google Chrome地址栏中的url时,QClipboard::dataChanged()的插槽DetectClipboardUrl有时会被调用两次,在Win7 32位上使用
我实现了一个 abstractListModel,它定期调用 dataCanged() 来更新列表。在 listmodel 中,我有一个变量,我想在 listview 之外显示,但也希望它在 list
我有一个连接到 QAbstractTableModel 子类的 QTableView。我正在编辑模型拥有的数据以响应不同控件(不是 QTableView)中的用户输入,然后我在模型上调用发出 data
我将 QML MapItemView 组件与基于 C++ QAbstractListModel 的模型一起使用。 MapItemView 在重置模型、添加新项目或删除现有项目时工作正常。但是,MapI
在 Qt 中,我有一个模型子类 QAbstractItemModel - 它是在 QTreeView 中显示的树。 该模型支持各种形式的变化,它们都可以正常工作。相关的两个是: 1)少量相关行中的一些
我正在使用 Python 和 PySide2 Qt 绑定(bind)。 我的程序旨在从 csv 文件加载记录,将它们显示为表中的行,并在询问时将每条记录上传到远程数据库。每次上传都需要几秒钟,所以
当模型以任何方式修改时,我试图施展一些魔法。对添加或删除的项目使用react非常容易,但我很难找到一种对被修改(重命名)的项目使用react的方法 model.dataChanged.connect(
大家好,这里是 Stackoverflow 中的所有 Qt 模型/ View 编程专家。我正在从事的项目要求我为 XML 数据定义一个 QAbstractItemModel 派生模型。之前在实现这个模
我希望有人可以就我观察到的 dataChanged 信号问题提供一些帮助。在使用受限的单元格范围发出此信号后,数据模型似乎在每个可见单元格上调用 data(...)。 例如: QModelIndex
例如,如果我在 QAbstractItemModel 中调整子项的大小,是否只为父索引发出一个 dataChanged 信号就足够了,或者我应该为每个 child ? 最佳答案 在我的测试中,仅仅为父
当模型数据更改时,我无法更新 QTableView 的布局。我尝试使用 dataChanged.emit(index,index)、layoutChanged.emit() 以及最后的手段 reset
下面的代码在 self.emit 行中断。它在 PyQt4 中运行良好。如何修复此代码以使其在 PyQt5 中工作? from PyQt5 import QtCore, QtGui, QtWidget
这是我在源模型中的 setData: bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role
我有一个 QTableView,它设置了一个 QStandardItemModel。用户在 View 中的某个索引中编辑数据,然后模型发出 dataChanged() 信号。在我处理 SIGNAL 的
我正在尝试制作一个 Qt 应用程序来监视对系统剪贴板的更改。每次用户将一些文本复制到剪贴板(应用程序外部)时,我的应用程序应该以某种方式更改该文本并将更改的文本复制到剪贴板。 问题是剪贴板只会变空!
我是一名优秀的程序员,十分优秀!