可能な解決策の1つは、余分なアイテムを追加するために使用しているモデルをサブクラス化することです。実装は簡単です。あなたのモデルMyModel
を呼び出す場合、サブクラスは(C++を使用)、次のようになります。
class MyModelWithNoneEntry : public MyModel
{
public:
int rowCount() {return MyModel::rowCount()+1;}
int columnCount() {return MyModel::columnCOunt();}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if (index.row() == 0)
{
// if we are at the desired column return the None item
if (index.column() == NET_NAME && role == Qt::DisplayRole)
return QVariant("None");
// otherwise a non valid QVariant
else
return QVariant();
}
// Return the parent's data
else
return MyModel::data(createIndex(index.row()-1,index.col()), role);
}
// parent and index should be defined as well but their implementation is straight
// forward
}
今、あなたは、コンボボックスにこのモデルを設定することができます。
ニート。私はそれを試してみましょう。 –
メインモデルをサブクラス化するのではなく、実際には新しいQAbstractListModelサブクラスを作成しました。私はその後、メインモデルをコンストラクタに渡して、新しいモデルが既存のモデルのデータにアクセスできるようにしました。それでも、この答えは私を正しい道にしてくれました。それ以外の場合は、Orriginalモデルクラスのサブクラス化がうまくいくかもしれません。受け入れられました。 –
答えがあなたを助けてくれてうれしいです。 – pnezis