2015-02-23 243 views
1

我已經設置了一個QItemDelegate,它有一個QComboBox的編輯器。我已經把這個項目委託給我的列表視圖。如何在列表視圖中顯示每個項目的所有QComboBox?

目前,只有當我點擊列表視圖中的某個項目時,組合框纔會顯示該特定項目。我把它做這個點擊:

ui->suggestedListView->setEditTriggers(QAbstractItemView::AllEditTriggers); 

我希望的是,在一個列表視圖中的每一項顯示其組合框,而不是使得它的用戶雙擊就能看到它。

這裏是我的項目代表:

#include "include/gui/comboboxdelegate.h" 

ComboBoxDelegate::ComboBoxDelegate(QObject *parent) 
    : QItemDelegate(parent) 
{ 
} 

QWidget *ComboBoxDelegate::createEditor(QWidget *parent, 
     const QStyleOptionViewItem &, 
     const QModelIndex &) const 
{ 
    QComboBox *editor = new QComboBox(parent); 
    editor->addItem("Address"); 
    editor->addItem("Address2"); 
    editor->addItem("City"); 
    editor->addItem("Country"); 
    editor->addItem("Date of Birth"); 
    editor->addItem("Email"); 
    editor->addItem("Fax Number"); 
    editor->addItem("First Name"); 
    editor->addItem("Gender"); 
    editor->addItem("Last Activity Timestamp"); 
    editor->addItem("Last Name"); 
    editor->addItem("Middle Name"); 
    editor->addItem("Mobile Number"); 
    editor->addItem("Phone Number"); 
    editor->addItem("Reference Code"); 
    editor->addItem("Signup Category"); 
    editor->addItem("IP Address"); 
    editor->addItem("Signup Timestamp"); 
    editor->addItem("Signup URL"); 
    editor->addItem("State/Province/Region"); 
    editor->addItem("Zip/Postal Code"); 
    editor->addItem("Discard"); 

    return editor; 
} 

void ComboBoxDelegate::setEditorData(QWidget *editor, 
             const QModelIndex &index) const 
{ 
    QString value = index.model()->data(index, Qt::EditRole).toString(); 

    QComboBox *cBox = static_cast<QComboBox *>(editor); 
    cBox->setCurrentIndex(cBox->findText(value)); 
} 

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 
            const QModelIndex &index) const 
{ 
    QComboBox *cBox = static_cast<QComboBox *>(editor); 
    QString value = cBox->currentText(); 

    model->setData(index, value, Qt::EditRole); 
} 

void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, 
     const QStyleOptionViewItem &option, const QModelIndex &) const 
{ 
    editor->setGeometry(option.rect); 
} 

這裏是我如何設置我的QList觀點:

ui->suggestedListView->setItemDelegate(new ComboBoxDelegate(ui->suggestedListView)); 
ui->suggestedListView->setEditTriggers(QAbstractItemView::AllEditTriggers); 

這甚至可能嗎?如果不是,那可能是另一種解決方案?

回答

2
  1. 通過QStyle::drawControl
  2. 手柄只需點擊一下你在明年的方式委託(::editorEvent)畫在你的委託組合框:創建一個編輯器(QComboBox),並迫使它顯示下拉列表。

P.S.使用QStyledItemDelegate而不是QItemDelegate

+0

這使我朝着正確的方向前進。謝謝!我使用完全相同的功能,除了將QItemDelegate更改爲QStyledItemDelegate並添加了paint()函數。 – 2015-02-24 16:56:01

相關問題