56,6 → 56,7 |
SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
: QItemDelegate(parent) |
{ |
// SpinBoxDelegate::getItems(); |
} |
//! [0] |
|
64,10 → 65,21 |
const QStyleOptionViewItem &/* option */, |
const QModelIndex &/* index */) const |
{ |
//QSpinBox *editor = new QSpinBox(parent); |
//editor->setMinimum(0); |
//editor->setMaximum(100); |
QStringList ID_list; |
QStringList Name_list; |
|
// getItems(); |
|
|
|
|
|
ID_list = items.keys(); |
Name_list = items.values(); |
|
QComboBox *editor = new QComboBox(parent); |
|
/* |
editor->addItem("0"); |
editor->addItem("1"); |
editor->addItem("2"); |
78,8 → 90,11 |
editor->addItem("7"); |
editor->addItem("8"); |
editor->addItem("9"); |
// editor->addItem("0"); |
*/ |
|
// editor->addItem("0"); |
|
editor->addItems(Name_list); |
return editor; |
} |
//! [1] |
88,25 → 103,33 |
void SpinBoxDelegate::setEditorData(QWidget *editor, |
const QModelIndex &index) const |
{ |
// int value = index.model()->data(index, Qt::EditRole).toInt(); |
QString value = index.model()->data(index, Qt::EditRole).toString(); |
QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
// spinBox->setValue(value); |
QString value = index.model()->data(index, Qt::EditRole).toString(); |
QComboBox *comboBox = static_cast<QComboBox*>(editor); |
if (value == "0") comboBox->setCurrentIndex(0); |
if (value == "1") comboBox->setCurrentIndex(1); |
if (value == "2") comboBox->setCurrentIndex(2); |
if (value == "3") comboBox->setCurrentIndex(3); |
if (value == "4") comboBox->setCurrentIndex(4); |
if (value == "5") comboBox->setCurrentIndex(5); |
if (value == "6") comboBox->setCurrentIndex(6); |
if (value == "7") comboBox->setCurrentIndex(7); |
if (value == "8") comboBox->setCurrentIndex(8); |
if (value == "9") comboBox->setCurrentIndex(9); |
comboBox->setEditable(true); |
QStringList ID_list; |
QStringList Name_list; |
int curr_index; |
|
ID_list = items.keys(); |
Name_list = items.values(); |
|
curr_index = ID_list.indexOf(value); |
comboBox->setCurrentIndex(curr_index); |
/**************************************************** |
if (value == "0") comboBox->setCurrentIndex(0); |
if (value == "1") comboBox->setCurrentIndex(1); |
if (value == "2") comboBox->setCurrentIndex(2); |
if (value == "3") comboBox->setCurrentIndex(3); |
if (value == "4") comboBox->setCurrentIndex(4); |
if (value == "5") comboBox->setCurrentIndex(5); |
if (value == "6") comboBox->setCurrentIndex(6); |
if (value == "7") comboBox->setCurrentIndex(7); |
if (value == "8") comboBox->setCurrentIndex(8); |
if (value == "9") comboBox->setCurrentIndex(9); |
******************************************************/ |
comboBox->setEditable(true); |
|
|
//comboBox->setItemText(0, value); |
|
} |
116,17 → 139,21 |
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
const QModelIndex &index) const |
{ |
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
// spinBox->interpretText(); |
// int value = spinBox->value(); |
QStringList ID_list; |
QStringList Name_list; |
|
// model->setData(index, value, Qt::EditRole); |
|
QComboBox *comboBox = static_cast<QComboBox*>(editor); |
int currIndex; |
currIndex = comboBox->currentIndex(); |
QString value = comboBox->itemText(currIndex); |
// QString value = comboBox->itemText(currIndex); |
|
ID_list = items.keys(); |
Name_list = items.values(); |
|
QString value = ID_list.at(currIndex); |
model->setData(index, value, Qt::EditRole); |
|
|
} |
//! [3] |
|
137,6 → 164,70 |
editor->setGeometry(option.rect); |
} |
//! [4] |
|
void SpinBoxDelegate::getItems(){ |
QMap <QString, QString> result_map; |
QSqlQuery q; |
QString query_str; |
bool ok; |
|
query_str = tr("select * from `ListOfClasses`"); |
q.prepare(query_str); |
|
|
ok = q.exec(); |
|
|
|
|
if (!ok) { |
/* |
QString error_str; |
error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû "); |
error_str.append(inctance); |
QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå. |
this, // Ðîäèòåëüñêèé âèäæåò. |
QObject::tr("Database Error"), // Çàãîëîâîê. |
q.lastError().text()); // Òåêñò ñîîáùåíèÿ. |
// tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ. |
|
*/ |
items.clear(); |
return; |
} |
|
|
// field_inctance = q.record().indexOf(tr("TableWhithInstance")); |
items.clear(); |
while(q.next()){ |
|
QString ID; |
QString ClassName; |
ID = q.value(0).toString(); // îïðåäåëÿåì ID |
ClassName = q.value(1).toString(); // îïðåäåëÿåì Name |
items.insert(ID, ClassName); |
} |
|
|
// return result_map; |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MyDEDelegate::MyDEDelegate( |
bool calpopup, |
QObject *parent) |
179,3 → 270,5 |
const QModelIndex& /* index */) const { |
editor->setGeometry(option.rect); |
} |
|
|