Active Topics

 


Reply
Thread Tools
Posts: 642 | Thanked: 486 times | Joined on Aug 2008
#1
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's Avatar
Posts: 534 | Thanked: 723 times | Joined on Oct 2009
#2
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?
 
Posts: 642 | Thanked: 486 times | Joined on Aug 2008
#3
Originally Posted by jflatt View Post
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.

Last edited by rash.m2k; 2012-08-28 at 22:15.
 
Reply


 
Forum Jump


All times are GMT. The time now is 15:09.