我在装有 OS-X 10.8.5 的 Mac 上运行 Qt 5.1.1 和 QtCreator 2.8.1。我有一个管理 ImageData 对象的 QAbstractListModel。在 main.cpp 中注册 ImageProvider 后,我可以使用 GridView 加载图像并在 QML 中很好地显示它们。
接下来我在视图中选择单个图像,例如,下面显示了几个带有橙色边框的选定图像:
然后 C++ 模型函数:deleteSelected(),产生预期的结果:
但是,当我尝试调整窗口大小时,比如抓住一个角落,我会崩溃。堆栈跟踪显示:异常类型:EXC_CRASH (SIGABRT),我收到 Qt 错误:
ASSERT failure in QList<T>::at: "index out of range", … QtCore/qlist.h, line 452
The program has unexpectedly finished.
因此,也许我不正确地删除了模型项或未能通知模型更改,但我认为开始和结束 RemoveRows 发出了正确的信号来处理同步?毫无疑问,我在这方面遗漏了其他东西。
我还调用了 begin 和 end ResetModel,它可以防止应用程序在调整大小后崩溃,但在这种情况下,附加到模型的任何其他视图都会恢复为显示所有原始项目。
我已经为此寻找解决方案,尝试了很多代码实验,并研究了此处、此处、此处以及其他几个地方发布的代码。
似乎无法使其正常工作,有什么建议吗?谢谢!
下面是一些相关代码:
主.cpp:
...
// Other Classes:
#include "datamodelcontroller.h"
#include "imageprovider.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
// Initialize and register model:
DataModelController model;
QQmlContext *context = engine.rootContext();
context->setContextProperty("DataModelFromContext", &model);
// Register image provider for each "role" to the model:
ImageProvider *imageProvider = new ImageProvider(&model);
engine.addImageProvider(QLatin1String("provider"), imageProvider);
// Get the main.qml path from a relative path:
PathResolver pathObject("qml/DebugProject/main.qml");
QString qmlPath = pathObject.pathResult;
// Create Component:
QQmlComponent *component = new QQmlComponent(&engine);
component->loadUrl(QUrl(qmlPath));
// Display Window:
QObject *topLevel = component->create();
QQuickWindow *window = qobject_cast<QQuickWindow*>(topLevel);
QSurfaceFormat surfaceFormat = window->requestedFormat();
window->setFormat(surfaceFormat);
window->show();
return app.exec();
}
数据模型控制器.h:
class DataModelController : public QAbstractListModel
{
Q_OBJECT
public:
explicit DataModelController(QObject *parent = 0);
enum DataRoles {
FileNameRole = Qt::UserRole + 1,
ImageRole
};
// QAbstractListModel:
void addData(ImageData*& imageObj);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
// Get the Model Data:
QList<ImageData*> getModelData();
signals:
void imageSelectedStateChange(bool selectedImageStateValue);
public slots:
void testLoadData();
void removeData(int index);
void setSelected(int index);
bool isSelected(int index);
void toggleSelected(int index);
void deleteSelected();
int count();
private:
// Model Data here:
QList<ImageData*> _modelData;
};
数据模型控制器.cpp
DataModelController::DataModelController(QObject *parent) : QAbstractListModel(parent)
{
}
void DataModelController::addData(ImageData*& imageObj) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_modelData << imageObj; // for QList<>
endInsertRows();
}
int DataModelController::rowCount(const QModelIndex &) const {
return _modelData.size();
}
// Slot:
int DataModelController::count() {
return this->rowCount();
}
QVariant DataModelController::data(const QModelIndex & index, int role) const
{
if(!index.isValid())
return QVariant();
ImageData* imgObj = _modelData[index.row()];
if (role == FileNameRole) {
string imgFileName = imgObj->getFileName();
QString fileName(imgFileName.c_str());
return fileName;
}
if (role == ImageRole) {
QString url = QString::number(index.row());
return url;
}
return QVariant();
}
QHash<int, QByteArray> DataModelController::roleNames() const {
QHash<int, QByteArray> roles;
roles[FileNameRole] = "filename";
roles[ImageRole] = "thumbnail";
return roles;
}
QList<ImageData*> DataModelController::getModelData() {
return _modelData;
}
void DataModelController::testLoadData()
{
int width = 256, height = 256;
for (int i = 0; i < 5; i++) {
ostringstream digit;
digit<<i;
string imgPath("qml/DebugProject/TempImages/"+digit.str()+".jpg");
PathResolver path(imgPath.c_str());
QImage img(path.pathResult);
// Initialize an Image Object:
string fileName("file"+digit.str());
ImageData *image = new ImageData(fileName);
image->setData(NULL);
image->nX = width;
image->nY = height;
image->setThumbnailQImage(img, width, height);
image->setSelected(false);
this->addData(image);
}
}
void DataModelController::removeData(int index) {
cout << "deleting index: " << index << endl;
this->beginRemoveRows(QModelIndex(), index, index);
_modelData.removeAt(index);
// delete _modelData.takeAt(index); // tried this
this->endRemoveRows();
//this->beginResetModel();
//this->endResetModel();
}
void DataModelController::setSelected(int index) {
ImageData *imgObj = this->getModelData().at(index);
if (!imgObj->getState()) {
imgObj->setSelected(true);
emit imageSelectedStateChange(imgObj->getState());
}
}
bool DataModelController::isSelected(int index) {
ImageData *imgObj = this->getModelData().at(index);
return imgObj->getState();
}
void DataModelController::toggleSelected(int index) {
ImageData *imgObj = this->getModelData().at(index);
imgObj->setSelected(!imgObj->getState());
emit imageSelectedStateChange(imgObj->getState());
}
void DataModelController::deleteSelected() {
for (int i = this->rowCount()-1; i >= 0; i--) {
if (this->isSelected(i)) {
cout << i << ": state: " << this->isSelected(i) << endl;
this->removeData(i);
cout << this->rowCount();
}
}
}
图像数据.h:
class ImageData
{
public:
ImageData();
ImageData(string filename);
long nX, nY; // image width, height
void setFileName(string filename);
string getFileName() const;
void setSelected(bool state);
bool getState() const;
void setThumbnail(const int width, const int height);
void setThumbnailQImage(QImage &imgStart, int width, int height);
QImage getThumbnail() const;
void setData(float* data);
float* getData() const;
private:
string _fileName;
QImage _thumbnail;
float* _data;
bool _isSelected;
void normalizeAndScaleData(float*& dataVector);
void getMaxMinValues(float& datamax, float& datamin, float*& data, const int numPixels, bool verbose);
unsigned char* getByteArrayFromFloatArray(int bytesPerRow, float*& dataVector);
};
main.qml:
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.1
import QtQuick.Controls.Styles 1.0
import QtQuick.Layouts 1.0
ApplicationWindow {
id: mainAppWindow
width: 1024*1.5*0.5
height: 256
color: "gray"
property real imgScale: 0.5
Window {
id: gridViewMenu
width: 160
height: 128
opacity: 0.8
color: "black"
visible: true
x: 1024;
Column {
id: colButtons
x: 10;
y: 10;
spacing: 25
CustomButton {
id: deleteSelectedButton; text: "Delete Selected"
onClicked: {
DataModelFromContext.deleteSelected();
}
}
CustomButton {
id: testDataButton; text: "Load Test Images"
visible: true
onClicked: DataModelFromContext.testLoadData()
}
}
}
Rectangle {
id: mainRect
width: mainAppWindow.width*0.95
height: mainAppWindow.height*0.8
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
color: "transparent"
// GRIDVIEW is here:
Rectangle {
id: gridContainer
anchors.centerIn: parent
width: parent.width
height: parent.height*0.9
color: "transparent"
GridView {
property int itemWidth: mainRect.width*imgScale;
id: gridView
interactive: true
anchors.centerIn: parent
width: parent.width;
height: parent.height;
cellWidth: itemWidth/3
cellHeight: itemWidth/3
focus: true
model: DataModelFromContext
delegate: gridDelegate
Behavior on opacity {
NumberAnimation { duration: 500; easing.type: Easing.InOutQuad }
}
Keys.onPressed: {
if (event.key == Qt.Key_D) {
DataModelFromContext.deleteSelected()
}
}
} // end of gridView
// GRIDVIEW Delegate:
Component {
id: gridDelegate
Rectangle {
id: gridImageWrapper
width: gridView.cellWidth
height: gridView.cellHeight
color: "black"
Rectangle {
id: imageBorder
anchors.fill: parent
color: "transparent"
border.color: "green"
border.width: 1
z: 1
}
MouseArea {
id: selectImage
anchors.fill: parent
onClicked: {
// toggleSelected triggers the C++ signal: imageSelectedStateChange
DataModelFromContext.toggleSelected(index);
console.log(index + "; " + DataModelFromContext.count() )
}
}
Rectangle {
id: selectedImageBorder
anchors.fill: parent
color: "transparent"
border.color: "orange"
border.width: 2
opacity: 0
z: 2
Connections {
target: DataModelFromContext
onImageSelectedStateChange: {
selectedImageBorder.opacity = DataModelFromContext.isSelected(index);
}
}
}
Image {
property int itemWidth: mainRect.width*imgScale
id: frontIcon
anchors.centerIn: parent
source: "image://provider/" + thumbnail
smooth: true
visible: true
sourceSize.width: itemWidth/3;
sourceSize.height: itemWidth/3;
}
} // end of Grid Delegate Rectangle
} // end of Grid Delegate
} // end of gridContainer Rectangle
} // End: mainRect
} // End Main Application Window
图像数据.cpp:
#include "imagedata.h"
ImageData::ImageData()
{
}
ImageData::ImageData(string filename)
{
_fileName = filename;
}
void ImageData::setFileName(string filename) {
_fileName = filename;
}
string ImageData::getFileName() const {
return _fileName;
}
void ImageData::setSelected(bool state) {
_isSelected = state;
}
bool ImageData::getState() const {
return _isSelected;
}
QImage ImageData::getThumbnail() const {
return _thumbnail;
}
void ImageData::setThumbnailQImage(QImage &imgStart, int width, int height) {
QImage img;
//QImage *imgStart = new QImage(pix, nX, nY, bytesPerRow, QImage::Format_Indexed8);
img = QPixmap::fromImage(imgStart).scaled(width, height, Qt::KeepAspectRatio).toImage();
_thumbnail = img;
}
void ImageData::setData(float* data) {
_data = data;
}
float* ImageData::getData() const {
return _data;
}
// Function: getMaxMinValues
void ImageData::getMaxMinValues(float& datamax, float& datamin, float*& data,
const int numPixels, bool verbose) {
datamin = 1.0E30;
datamax = -1.0E30;
for (int pix = 0; pix < numPixels-1; pix++) {
if (data[pix] < datamin) {datamin = data[pix];}
if (data[pix] > datamax) {datamax = data[pix];}
}
if (verbose) {
std::cout << "Min and Max pixel values = " << datamin << "; " << datamax << std::endl;
}
}
// Function: normalizeAndScaleData
void ImageData::normalizeAndScaleData(float*& dataVector)
{
// ---- Find Max and Min Values:
float datamin, datamax;
this->getMaxMinValues(datamax, datamin, dataVector, nX*nY, false);
// Get average and standard deviation:
float avg = 0, sig = 0;
for (int px = 0; px < nX*nY; px++) {
avg += dataVector[px];
}
avg /= nX*nY;
for (int px = 0; px<nX*nY; px++) {
sig += powf(dataVector[px] - avg, 0.5);
//sig += powf(dataVector[px] - avg, 2.0);
}
sig = pow(sig/(nX*nY), 0.5);
int deviations = 5;
if (datamin < avg-deviations*sig) {datamin = avg-deviations*sig;}
if (datamax > avg+deviations*sig) {datamax = avg+deviations*sig;}
// ---- ScaleImage Data Here (linear scaling):
for (int px = 0; px<nX*nY; px++) {
dataVector[px] = (dataVector[px]-datamin)/(datamax - datamin);
}
}
unsigned char * ImageData::getByteArrayFromFloatArray(int bytesPerRow, float*& dataVector)
{
unsigned char *pix = new unsigned char[nX*nY];
for (int row = 0; row < nY; row++) {
for (int col = 0; col < nX; col++)
pix[row*bytesPerRow + col] = (unsigned char) 255*dataVector[row*nX + col];
}
return pix;
}
// Function: setThumbnail
void ImageData::setThumbnail(const int width, const int height) {
QImage img;
float *dataVector = this->getData();
if (dataVector == NULL) {
dataVector = new float[width*height];
} else {
normalizeAndScaleData(dataVector);
}
// Map to Byte Array (nX = cols of pixels in a row, nY = rows):
int bytesPerPixel = 1; // = sizeof(unsigned char)
int pixelsPerRow = nX;
int bytesPerRow = bytesPerPixel*pixelsPerRow;
unsigned char *pix = getByteArrayFromFloatArray(bytesPerRow, dataVector);
// Calculate the Thumbnail Image:
QImage *imgStart = new QImage(pix, nX, nY, bytesPerRow, QImage::Format_Indexed8);
img = QPixmap::fromImage(*imgStart).scaled(width, height, Qt::KeepAspectRatio).toImage();
_thumbnail = img;
}
编辑:在 userr1728854 下面关于范围检查的评论之后,我编辑了 DataModelController::data() 的第一部分,以检查这是否是问题所在。
我的代码现在看起来像(它比修改原始代码更容易在下面引用,而且我不想通过更改我发布的内容来改变我的问题的上下文):
QVariant DataModelController::data(const QModelIndex & index, int role) const
{
cout << "model index = " << index.row() << endl; // add this to help troubleshoot
if (!index.isValid() || index.row() > this->rowCount() || !this->modelContainsRow(index.row()) ) {
return QVariant();
}
因此,即使这不是向 data() 方法添加范围检查的最可靠方法,该行:
cout << "模型索引 = " << index.row() << endl;
当我调整窗口大小时,至少应该打印:“ index.row() ”,但事实并非如此。因此,调整窗口大小似乎无法访问 data() 方法,并且程序仍然崩溃。