0
我創建了一個代表從Qt的微調框代表實例幾乎複製,我試圖填補一個QTableView中。不過,我收到一個奇怪的問題,表頭顯示,但單元格爲空,無法點擊。QTableView中的細胞是空的,但標題顯示
代碼委託:
#include "double_spinbox_delegate.h"
DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent) : QItemDelegate(parent){}
QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setValue(0);
// if (index.column() == 0){
// editor->setMinimum(0);
// editor->setMaximum(255);
// }
// else{
// editor->setMinimum(0);
// editor->setMaximum(1);
// }
return editor;
}
void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
double value = index.model()->data(index, Qt::EditRole).toDouble();
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->setValue(value);
}
void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->interpretText();
double value = doubleSpinBox->value();
model->setData(index, value, Qt::EditRole);
}
void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
editor->setGeometry(option.rect);
}
和我打電話的形式構造
void MainWindow::InitializeColorTable(){
QTableView *tableColor = ui->tableColor;
QStandardItemModel *model = new QStandardItemModel(4, 4, ui->tableColor);
// QStandardItemModel *model = this->colorTableModel;
tableColor->setModel(model);
DoubleSpinBoxDelegate delegate;
tableColor->setItemDelegate(&delegate);
model->setHorizontalHeaderLabels(QStringList() << tr("Value") << tr("R") << tr("G") << tr("B"));
for (int row = 0; row < model->rowCount(); ++row){
for (int col = 0; col < model->columnCount(); ++col){
QModelIndex index = model->index(row, col, QModelIndex());
model->setData(index, QVariant((row + 1.0) * (col + 1.0)), Qt::EditRole);
}
}
}
謝謝!我知道這將是東西,需要一個全新的一雙眼睛來識別。 – 2013-03-21 03:41:54