特定の列の各行にQComboBoxを表示するテーブルをQtに実装する必要があります。Qt、QStandarItemModel:カスタムQComboBoxの委任項目が、モデルのインスタンシエータからその内容を入力します。
この質問に基づいて:QStandardItem + QComboBox QItemDelegateを作成するのに成功しました。この例では、QComboBoxの内容はComboBoxDelegateクラスで静的に定義されていますが、私の場合はQStandardItemModelが作成される関数内でQComboBoxの内容を定義する必要があります。
モデルがメイン・ウィンドウクラスメソッド内で定義されています
void MainWindow::fooHandler() {
QStandardItemModel* mymodel = new QStandardItemModel;
ui->tablePoint->setModel(mymodel);
ComboBoxDelegate* delegate=new ComboBoxDelegate;
ui->tablePoint->setItemDelegateForColumn(2,delegate);
QStringList Pets;
Pets.append("cat");
Pets.append("dog");
Pets.append("parrot");
// So far this is how I tried to store data under Qt::UserRole in "mymodel":
QModelIndex idx = mymodel->index(0, 2, QModelIndex());
mymodel->setData(idx,QVariant::fromValue(Pets), Qt::UserRole);
//Now i fill the table with some values...
QList< QStandardItem * > items;
items.clear();
items << new QStandardItem("col0");
items << new QStandardItem("col1");
items << new QStandardItem("parrot");
items << new QStandardItem("col3");
mymodel->appendRow(items);
items.clear();
items << new QStandardItem("col0");
items << new QStandardItem("col1");
items << new QStandardItem("cat");
items << new QStandardItem("col3");
mymodel->appendRow(items);
}
それから私は、デリゲートクラスからコンボボックスの内容を復元することができるはずです。
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *cBox = static_cast<QComboBox*>(editor);
if(index.column()==2) {
QModelIndex idx = index.model()->index(0, 2, QModelIndex());
cBox->addItem(index.model()->data(idx,Qt::UserRole).toStringList().at(0));
cBox->addItem(index.model()->data(idx,Qt::UserRole).toStringList().at(1));
cBox->addItem(index.model()->data(idx,Qt::UserRole).toStringList().at(2));
}
cBox->setCurrentIndex(cBox->findText(value));
}
プロジェクトはうまくコンパイルするが、ときに私セルをクリックしてQComboBoxの値を変更してください。プログラムが不正になり、「無効なパラメータがCランタイム関数に渡されました。」というメッセージが表示されます。
私はこの問題は、多分愚かな間違いであることを何とか気持ちを持っていますが、私は盲目だと...私は本当にあなたがこの上で役立つapprecciateなり、かなりの時間のために、ここで立ち往生するので
あなたは与えてもらえ私はいくつかのアドバイスを?
読んでいただきありがとうございます!