maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Is there anything wrong with this? Random Segfaults. (https://talk.maemo.org/showthread.php?t=86353)

rash.m2k 2012-08-28 20:47

Is there anything wrong with this? Random Segfaults.
 
I've implemented a custom QObject based C++ listmodel and i get random segfaults wondering if anyone can spot if I've done anything wrong:



.h file:


Code:

#ifndef KEYVALUELISTITEM_H
#define KEYVALUELISTITEM_H
#include <QtGui/QApplication>
#include <QObject>
#include <ItemFilterType.h>
#include <ItemFilter.h>
#include <QDebug>

using namespace com_ebay_www_marketplace_search_v1_services;

class KeyValueListItem : public QObject
{
    Q_OBJECT
    // NOTIFY - Simply to get rid of the warning message xxxx depends on non-NOTIFYable properties: xxxx
    Q_PROPERTY(QString displayValue READ displayValue NOTIFY displayValueChanged)
    Q_PROPERTY(QString key READ key NOTIFY keyChanged)
    Q_PROPERTY(QString value READ value NOTIFY valueChanged)
    Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged)

public:
    KeyValueListItem(const QString &displayValue, const ADBItemFilterTypeEnum &key, const QString &value, const bool &selected, QObject *parent=0);
    QString displayValue() const;
    ADBItemFilterTypeEnum key() const;
    QString value() const;
    bool selected() const;
    void setSelected(const bool &selected);

signals:
    void displayValueChanged();
    void keyChanged();
    void valueChanged();
    void selectedChanged();

private:
    QString m_displayValue;
    ADBItemFilterTypeEnum m_key;
    QString m_value;
    bool m_selected;
};

#endif // KEYVALUELISTITEM_H

.cpp:

Code:

#include "keyvaluelistitem.h"

KeyValueListItem::KeyValueListItem(const QString &displayValue, const ADBItemFilterTypeEnum &key, const QString &value, const bool &selected, QObject *parent)
    : QObject(parent), m_displayValue(displayValue), m_key(key), m_value(value), m_selected(selected)
{
}

QString KeyValueListItem::displayValue() const {
    if (m_displayValue == NULL)
        return QString("NULL");
    else
        return m_displayValue;
}

ADBItemFilterTypeEnum KeyValueListItem::key() const {
    if (m_key == NULL)
        return ItemFilterType_CONDITION;
    else
        return m_key;
}

QString KeyValueListItem::value() const {
    if (m_value == NULL)
        return QString("NULL");
    else
        return m_value;
}

bool KeyValueListItem::selected() const {
    if (m_selected == NULL)
        return false;
    else
        return m_selected;
}

void KeyValueListItem::setSelected(const bool &selected) {
    if (selected != m_selected) {
        m_selected = selected;
        emit selectedChanged();
    }
}


create new items like this, where conditions is QList<QObject*>

Code:

conditions->append(new KeyValueListItem("New other (see details)", ItemFilterType_CONDITION, "1500", false));

jflatt 2012-08-28 22:03

Re: Is there anything wrong with this? Random Segfaults.
 
On that NOTIFY comment, you can add CONSTANT instead to your property declarations if they are indeed read only. Also, why are you checking your private member variables for NULL? They are not pointers. Why a QLIst<QObject*> instead of a QList<KeyValueListItem*>? Why it segfaults? Who knows, not enough details, why don't you run it through gdb and get a backtrace?

rash.m2k 2012-08-28 22:11

Re: Is there anything wrong with this? Random Segfaults.
 
Quote:

Originally Posted by jflatt (Post 1256960)
On that NOTIFY comment, you can add CONSTANT instead to your property declarations if they are indeed read only. Also, why are you checking your private member variables for NULL? They are not pointers. Why a QLIst<QObject*> instead of a QList<KeyValueListItem*>? Why it segfaults? Who knows, not enough details, why don't you run it through gdb and get a backtrace?

Just doing null checking to make sure.

using QLIst<QObject*> because QList<KeyValueListItem*> gives an error which its not registered with the qt meta system

I've got around the problem though by using QVariant::fromValue(xxxx) like in the examples.


All times are GMT. The time now is 23:08.

vBulletin® Version 3.8.8