Qt-interest Archive, March 2002
Combobox widget derived from QComboBox, feedback required
Message 1 in thread
Hi all,
A few days ago, I posted a question about how to retrieve the value
attached to a selected item in a combo-box. Not having received any
reply, I have derived the following class from QComboBox. I would like to
receive your feedback about the approach.
#include <qstring.h>
#include <qarray.h>
typedef struct {
QString text;
QString value;
} EYComboBoxEntry;
class EYComboBox : public QComboBox
{
Q_OBJECT
public:
EYComboBox(QWidget* parent=0, const char* name=0);
~EYComboBox();
void fill(QArray<ComboBoxEntry> entries);
QString currentValue();
protected:
QArray<EYComboBoxEntry> _entries;
};
// Implementation
EYComboBox::EYComboBox(QWidget* parent, const char* name)
: QComboBox(parent, name)
{
}
EYComboBox::~EYComboBox()
{
}
void EYComboBox::fill(QArray<EYComboBoxEntry> entries)
{
_entries = entries;
for (int i = 0; i < _entries.size(); i++) {
insertItem(_entries[i].text, i);
}
}
QString EYComboBox::currentValue()
{
return _entries[currentItem()].value;
}
Thank you.
--
[ signature omitted ]
Message 2 in thread
Hello,
Your codes would work correct, but may cause SEGV error.
QArray class documet says:
"QArray stores the array elements directly in the array.
It can only deal with simple types, i.e. C++ types, structs
and classes that have no constructors, destructors or
virtual functions. QArray uses bitwise operations to copy
and compare array elements. "
QString uses implicit sharing, so bitwise copy would cause
reference count v.s. counter mismatch. You can use QVector
instead of QArray.
Naoyuki
On 2002 March 19 Tuesday 22:23, Eddy Young wrote:
> Hi all,
>
> A few days ago, I posted a question about how to retrieve
> the value attached to a selected item in a combo-box. Not
> having received any reply, I have derived the following
> class from QComboBox. I would like to receive your
> feedback about the approach.
>
> #include <qstring.h>
> #include <qarray.h>
>
> typedef struct {
> QString text;
> QString value;
> } EYComboBoxEntry;
>
> class EYComboBox : public QComboBox
> {
> Q_OBJECT
>
> public:
> EYComboBox(QWidget* parent=0, const char*
> name=0); ~EYComboBox();
>
> void fill(QArray<ComboBoxEntry> entries);
> QString currentValue();
>
> protected:
> QArray<EYComboBoxEntry> _entries;
> };
>
> // Implementation
> EYComboBox::EYComboBox(QWidget* parent, const char* name)
>
> : QComboBox(parent, name)
>
> {
> }
>
> EYComboBox::~EYComboBox()
> {
> }
>
> void EYComboBox::fill(QArray<EYComboBoxEntry> entries)
> {
> _entries = entries;
>
> for (int i = 0; i < _entries.size(); i++) {
> insertItem(_entries[i].text, i);
> }
> }
>
> QString EYComboBox::currentValue()
> {
> return _entries[currentItem()].value;
> }
>
>
> Thank you.
Message 3 in thread
try this example:
class ComboBox : public QComboBox {
public:
ComboBox( bool rw, QWidget* parent = 0L, const char* name = 0L );
QListBoxItem* selectedItem() const {
return listBox()->item(currentItem());
}
};
class ListBoxEntry : public QListBoxText {
public:
ListBoxEntry ( ComboBox* cb, const QString& value = QString::null )
: QListBoxText(cb->listBox(), value {}
};
ComboBox* cb = new ComboBox(...);
new ListBoxEntry(cb, "value1");
new ListBoxEntry(cb, "value2");
ListBoxEntry* item = dynamic_cast<ListBoxEntry*>(cb->selectedItem());
Eddy Young wrote:
> Hi all,
>
> A few days ago, I posted a question about how to retrieve the value
> attached to a selected item in a combo-box. Not having received any
> reply, I have derived the following class from QComboBox. I would like to
> receive your feedback about the approach.
>
> #include <qstring.h>
> #include <qarray.h>
>
> typedef struct {
> QString text;
> QString value;
> } EYComboBoxEntry;
>
> class EYComboBox : public QComboBox
> {
> Q_OBJECT
>
> public:
> EYComboBox(QWidget* parent=0, const char* name=0);
> ~EYComboBox();
>
> void fill(QArray<ComboBoxEntry> entries);
> QString currentValue();
>
> protected:
> QArray<EYComboBoxEntry> _entries;
> };
>
> // Implementation
> EYComboBox::EYComboBox(QWidget* parent, const char* name)
> : QComboBox(parent, name)
> {
> }
>
> EYComboBox::~EYComboBox()
> {
> }
>
> void EYComboBox::fill(QArray<EYComboBoxEntry> entries)
> {
> _entries = entries;
>
> for (int i = 0; i < _entries.size(); i++) {
> insertItem(_entries[i].text, i);
> }
> }
>
> QString EYComboBox::currentValue()
> {
> return _entries[currentItem()].value;
> }
>
>
> Thank you.
>
--
[ signature omitted ]