void OperationsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { const QVariant value = index.data(Qt::EditRole); if (value.type() == QVariant::StringList) { QListWidget *lw = qobject_cast<QListWidget *>(editor); const auto items = lw->findItems(index.data(Qt::DisplayRole).toString(), Qt::MatchExactly); if (!items.empty()) lw->setCurrentItem(items.first()); else lw->setCurrentItem(lw->item(0)); const int extraWidth = 25; const int extraHeight = 6; lw->setMinimumWidth(lw->sizeHintForColumn(0) + extraWidth); // to prevent possible hiding bottom part of the list const int h = lw->count() * (lw->visualItemRect(lw->currentItem()).height() + extraHeight); const int y = (lw->parentWidget() && (lw->parentWidget()->rect().bottom() < lw->y() + h)) ? lw->parentWidget()->rect().bottom() - h - extraHeight : lw->y(); lw->setGeometry(lw->x(), y, lw->width(), h); // now lw can be partially hidden behind the tree view // if tree view has small rect, so set parent of lw // to app window and map coordinates accordingly to leave lw in place const auto globalCoord = lw->parentWidget()->mapToGlobal(lw->geometry().topLeft()); lw->setParent(appWindow); const auto newLocalCoord = appWindow->mapFromGlobal(globalCoord); lw->setGeometry(newLocalCoord.x(), newLocalCoord.y(), lw->width(), h); } else // single value QStyledItemDelegate::setEditorData(editor, index); }
void ItemOrderList::insertItem(const QString &label, bool checked, bool highlight, const QIcon &icon, const ItemPtr &item, int targetRow) { QListWidget *list = ui->listWidgetItems; QListWidgetItem *listItem = new QListWidgetItem(icon, label); const int row = targetRow >= 0 ? qMin(list->count(), targetRow) : list->count(); list->insertItem(row, listItem); listItem->setCheckState(checked ? Qt::Checked : Qt::Unchecked); setItemHighlight(listItem, highlight); m_items[listItem] = ItemWidgetPair(item); // Resize list to minimal size. const int w = list->sizeHintForColumn(0) + list->verticalScrollBar()->sizeHint().width() + 4; list->setMaximumWidth(w); if ( list->currentItem() == NULL ) list->setCurrentRow(row); }
void ItemOrderList::insertItem(const QString &label, bool checked, const QIcon &icon, const ItemPtr &item, int targetRow) { QListWidget *list = ui->listWidgetItems; auto listItem = new QListWidgetItem(icon, label); listItem->setCheckState(checked ? Qt::Checked : Qt::Unchecked); m_items[listItem] = ItemWidgetPair(item, checked); const int row = targetRow >= 0 ? qMin(list->count(), targetRow) : list->count(); list->insertItem(row, listItem); // Resize list to minimal size. if ( !isVisible() ) { const int w = list->sizeHintForColumn(0) + list->verticalScrollBar()->sizeHint().width() + 4; list->resize( w, list->height() ); } if ( list->currentItem() == nullptr ) list->setCurrentRow(row); }
QWidget* MultiDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { const QAbstractItemModel* model = index.model(); QVariant value = model->data( index, Qt::EditRole); switch (value.type()) { case QMetaType::QTime: { QTimeEdit* editor = new QTimeEdit( parent); editor->setMaximumWidth( editor->sizeHint().width()); //// Get value snapshot into editor editor->setTime( value.toTime()); return editor; } case QMetaType::QDate: { QDateEdit* editor = new QDateEdit( parent); setupCalenderWidget( editor); editor->setMaximumWidth( editor->sizeHint().width()); //// Get value snapshot into editor editor->setDate( value.toDate()); return editor; } case QMetaType::QDateTime: { QDateTimeEdit* editor = new QDateTimeEdit( parent); setupCalenderWidget( editor); editor->setMaximumWidth( editor->sizeHint().width()); editor->setDateTime( value.toDateTime()); return editor; } case QMetaType::QImage: // Fall throu case QMetaType::QPixmap: // Fall throu case QMetaType::QIcon: { PixmapViewer* editor = new PixmapViewer( parent); connect( editor, SIGNAL(finished(int)), this, SLOT(closeEmittingEditor())); return editor; } case QMetaType::QStringList: { QVariant varList = index.model()->data( index, ItemDataRole::EnumList); if (varList.isNull()) break; // Not a enum-list, fall to std QListWidget* editor = new QListWidget( parent); foreach (const QString& bitItemText, varList.toStringList()) { QListWidgetItem* bitItem = new QListWidgetItem( bitItemText, editor); bitItem->setFlags(bitItem->flags() | Qt::ItemIsUserCheckable); bitItem->setCheckState(Qt::Unchecked); } int width = editor->sizeHintForColumn(0) + 25; int height = editor->sizeHintForRow(0) * editor->count() + 10; editor->setMinimumWidth( width); editor->setMaximumWidth( width); editor->setMinimumHeight( height); editor->setMaximumHeight( height); //// Get value snapshot into editor QStringList valList = value.toStringList(); int itemCount = editor->count(); for (int i = 0; i < itemCount; ++i) { QListWidgetItem* bitItem = editor->item(i); bool isActive = valList.contains( bitItem->text()); bitItem->setCheckState( isActive ? Qt::Checked : Qt::Unchecked); } return editor; } case QMetaType::QString: { QVariant varList = index.model()->data( index, ItemDataRole::EnumList); if (varList.isNull()) break; // Not a enum-list, fall to std QComboBox* editor = new QComboBox( parent); editor->setSizeAdjustPolicy(QComboBox::AdjustToContents); editor->addItems( varList.toStringList()); editor->setMaximumWidth( editor->minimumSizeHint().width()); //// Get value snapshot into editor editor->setCurrentIndex( editor->findText( value.toString())); return editor; } default:; } if (index.column() == 0) { emit itemEditTrigged( index); return 0; // No inline editor } QWidget* editor = QItemDelegate::createEditor( parent, option, index); //// Get value snapshot into editor QItemDelegate::setEditorData( editor, index); return editor; }