PDA

View Full Version : Readonly Model implementation requirements for QComboBox



DeepDiver
8th November 2007, 14:32
Hi @all,

I'm implementing a model by sub classing QAbstractTableModel.
The model is read-only.
I did implement the methods rowCount(), columnCount() and data().

Using this model with a QComboBox causes one problem: The listview is not popping up.

I was already walking through the QComboBox implementation in order to see what might cause this - but I have absolutely NO idea ....

Anyone an idea???

THX alot

mchara
8th November 2007, 15:17
Hi, could you check if i.e. QTableView shows some contents? If QTableView or ListView is also empty, post code of your inherited model.

Oh... one more thing - make sure you have setModel for comboBox...

DeepDiver
8th November 2007, 15:37
The content is loaded to the combo box, because I see the content combobox and
by pressing the up and down arrows I can navigate through the items.

The only trouble is the listview is not popping up. :crying:

jpn
8th November 2007, 17:13
Did you subclass QComboBox? Did you reimplement any of its event handlers or so? I made a simple test and it works fine for me:


#include <QtGui>

class TableModel : public QAbstractTableModel
{
public:
TableModel(int rows, int cols, QObject* parent = 0)
: QAbstractTableModel(parent), rows(rows), cols(cols) { }

int rowCount(const QModelIndex& parent = QModelIndex()) const { return rows; }
int columnCount(const QModelIndex& parent = QModelIndex()) const { return cols; }

QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
{
if (index.isValid() && role == Qt::DisplayRole)
return QString("%1,%2").arg(index.row()).arg(index.column());
return QVariant();
}

private:
int rows;
int cols;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QComboBox combo;
combo.setModel(new TableModel(5, 2, &combo));
combo.show();
return app.exec();
}

DeepDiver
8th November 2007, 17:49
Thanks for the small demo code.

I had a bug in the data() implementation where I did not pay enough attention to the role.

THX

jpn
8th November 2007, 17:58
Thanks for the small demo code.
You're welcome :)

I had a bug in the data() implementation where I did not pay enough attention to the role.
I'm curious. What kind of bug? The side effect (even if data is shown in table, combo popup is not shown) is confusing..

DeepDiver
8th November 2007, 18:10
I was always returning the cell data.
Something like:



if (!index.isValid())
return QVariant();
if (role == Qt::DecorationRole)
return QIcon(/* */);
switch(index.column())
{
case 0: return "col0";
case 1: return "col1";
case 2: return "col2";
...
}
return QVariant();

What does that mean?

I return a string for the role Qt::SizeHintRole - which causes each item to have a size of 0.
That's why the list view has a total height of 0 as well.

:crying:

Again THX a lot!