2017-01-02 8 views
0

QItemDelegateからサブクラス化されたカスタムデリゲートは、最初の列にQComboBoxを、他のすべての列にはQLineEditを提供します。QItemDelegateのエディターのサイズ

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

QWidget *SensorDisplayDelegate::createEditor(QWidget *parent, 
              const QStyleOptionViewItem &option, 
              const QModelIndex &index) const 
{ 
    int col = index.column(); 
    if(col == 0) 
    { 
     QComboBox *comboBox = new QComboBox(parent); 
     connect(comboBox, SIGNAL(activated(int)), this, SLOT(setData(int))); 
     comboBox->setEditable(false); 
     //comboBox->setMaximumSize(editorSize); 
     comboBox->setInsertPolicy(QComboBox::NoInsert); 
     currentComboBox = comboBox; 
     return comboBox; 
    } 
    else  
    { 
     QLineEdit *lineEdit = new QLineEdit(parent); 
     return lineEdit; 
    } 

    return NULL; 
} 

void SensorDisplayDelegate::setEditorData(QWidget *editor, 
              const QModelIndex &index) const 
{ 
    int col = index.column(); 
    if(col == 0) 
    { 
     QComboBox *comboBox = static_cast<QComboBox*>(editor); 
     QStringList comboItems = index.data(Qt::EditRole).toStringList(); 

     comboBox->addItem("Add New Sensor"); 
     comboBox->addItems(comboItems); 

     QCompleter *completer = new QCompleter(comboItems); 
     completer->setCaseSensitivity(Qt::CaseInsensitive); 
     comboBox->setCompleter(completer); 
     comboBox->showPopup(); 
    } 
    else 
    { 
     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); 
     lineEdit->setText(index.data(Qt::EditRole).toString()); 
     lineEdit->show(); 
    } 
} 

void SensorDisplayDelegate::setModelData(QWidget *editor, 
             QAbstractItemModel *model, 
             const QModelIndex &index) const 
{ 
    int col = index.column(); 
    if(col == 0) 
    { 
     QComboBox *comboBox = static_cast<QComboBox*>(editor); 
     if(comboBox->currentIndex() == 0) 
      emit addNewSensor(); 
     else 
      emit populateSensorView(comboBox->currentText()); 
    } 
    else 
    { 
     QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); 
     model->setData(index, QVariant(lineEdit->text())); 
    } 

} 

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

QSize SensorDisplayDelegate::sizeHint(const QStyleOptionViewItem &option, 
             const QModelIndex &index) const 
{ 
    return editorSize; 
} 


void SensorDisplayDelegate::setData(int option) 
{ 
    emit commitData(currentComboBox); 
    emit closeEditor(currentComboBox); 
} 

editTriggerがselectClickedに設定されています。 QTableView内のセル全体をコンボボックスで覆いたい。しかし、今はちょうど左手の角にちらつきとして現れる。 QTableViewのmousePressedをリッスンするイベントフィルターを通してセルサイズを渡して、最小サイズを設定しようとしました。ただし、デリゲートの対応するスロットは決して呼び出されません。ここでは、コードです:

MultiEventFilter.cpp:qAppにインストール

bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event) 
{ 
    if(event->type() == QEvent::MouseButtonPress) 
    { 
     if(obj->objectName() == "sensorlocationTableView") 
     { 
      QTableView *sensorView = static_cast<QTableView*>(obj); 
      QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes(); 
      if(!idxs.empty()) 
      { 
       QModelIndex idx = idxs.at(0); 
       emit passCellSize(QSize(sensorView->columnWidth(idx.column()), 
             sensorView->rowHeight(idx.row()))); 
      } 
     } 
    } 

    return false; 
} 

MainWindow.cpp:

eFilter = new MultiEventFilter(); 
    connect(eFilter, SIGNAL(passCellSize(QSize)), 
      sensor_display_delegate, SLOT(setEditorSize(QSize))); 

SensorDisplayDelegate.cppスロット:QSIZE editorSizeプライベートメンバーである

void SensorDisplayDelegate::setEditorSize(const QSize &size) 
{ 
    editorSize = size; 
} 

。 エディタのサイズを正しく設定するにはどうすればよいですか? QLineEditエディタにも適用できる一般的なものが必要です。 また、エディタが閉じているときに明示的にcommitData()を発行する必要がありますか?これは、QComboBoxに関連するコード例では見られませんでした。

答えて

0

eventFilterは、選択指標が設定される前にクリックイベントを傍受している可能性があります。だから、あなたはいつも空のidxs IndexListを打っていますか?

bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event) 
{ 
    if(event->type() == QEvent::MouseButtonPress) 
    { 
     if(obj->objectName() == "sensorlocationTableView") 
     { 
      emit locationTableViewClicked(); 
     } 
    } 

    return false; 
} 

.... 

connect(eFilter, SIGNAL(locationTableViewClicked()), 
     sensor_display_delegate, SLOT(setEditorSize())); 
... 

void SensorDisplayDelegate::setEditorSize() 
{ 
    QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes(); 
    if(!idxs.empty()) 
    { 
     QModelIndex idx = idxs.at(0); 
     editorSize = QSize(sensorView->columnWidth(idx.column()), 
          sensorView->rowHeight(idx.row())); 
    } 
} 

ようなもので、そのループを交換してみてください

関連する問題