Impossible to access or update class member with Segmentation fault

时间:2019-03-19 15:05:55

标签: c++ qt c++11 segmentation-fault

I've been getting segmentation fault error with below code.

Header file is below

#include <QSortFilterProxyModel>
#include <QDateTime>

class TransactionFilterProxy : public QSortFilterProxyModel
{
    Q_OBJECT

public:
    explicit TransactionFilterProxy(QObject *parent = 0);


    static const QDateTime MIN_DATE;

    static const QDateTime MAX_DATE;

    void setDateRange(const QDateTime &from, const QDateTime &to);

protected:
    bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const;

private:

    QDateTime dateFrom;
    QDateTime dateTo;
};

And CPP file is below

#include "transactionfilterproxy.h"
#include "transactiontablemodel.h"
#include <QDateTime>
#include <cstdlib>

const QDateTime TransactionFilterProxy::MIN_DATE = QDateTime::fromTime_t(0);

const QDateTime TransactionFilterProxy::MAX_DATE = QDateTime::fromTime_t(0xFFFFFFFF);

TransactionFilterProxy::TransactionFilterProxy(QObject *parent) :
    QSortFilterProxyModel(parent),
    dateFrom(MIN_DATE),
    dateTo(MAX_DATE)
{

}

bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{


    QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);

    int type = index.data(TransactionTableModel::TypeRole).toInt();
    QDateTime datetime = index.data(TransactionTableModel::DateRole).toDateTime();

    if( datetime < dateFrom || datetime > dateTo)  //crashes here
        return false;

    return true;
}

void TransactionFilterProxy::setDateRange(const QDateTime &from, const QDateTime &to)
{
    this->dateFrom = from; //crashes here
    this->dateTo = to;
    invalidateFilter();
}

Application crashes when the QDateTime class members dateFrom was accessed. Tried to run debug mode with QTcreator, it shows SIGSEGV error. Any suggestion would be great.

0 个答案:

没有答案