2

私は数値エディタを持っていますQSpinBox

NumericEditor::NumericEditor(QWidget *widget): QSpinBox(widget)

このエディタを使用して、のタイプを編集しQVariant::IntますQTableWidget

QItemEditorCreatorBase *numericEditor = new QStandardItemEditorCreator<NumericEditor>();
factory->registerEditor(QVariant::Int, numericEditor); 

データは通常どおりテーブルに入力されます。「色」という言葉の使用は無視してください。カラーエディタの例に基づいています。

QTableWidgetItem *nameItem2 = new QTableWidgetItem(QString("label2"));
QTableWidgetItem *colorItem2 = new QTableWidgetItem();
colorItem2->setData(Qt::DisplayRole, QVariant(int(4)));
table->setItem(1, 0, nameItem2);
table->setItem(1, 1, colorItem2);

スピンボックスが表示され、QTableWidgetで正常に機能します。

私の望みは、テーブルがセルを編集するときに使用するQSpinBoxのインスタンスにアクセスして、QVariant::Int最小値と最大値を設定できるようにすることです。

これどうやってするの?

4

1 に答える 1

0

を使用して列にデリゲートをインストールできます。QTableWidget::setItemDelegateForColumnエディターを開くと、そのcreateEditorメソッドが呼び出されます。

class MyDelegate : public QStyledItemDelegate {
public:
    QWidget *createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
        // You could create the widget from scratch or call 
        // the base function which will use the QItemEditor you already wrote
        QWidget * editor = QStyledItemDelegate::createEditor(parent, option, index);

        // do whatever you need to do with the widget
        editor->setProperty("minimum", 100);
        editor->setProperty("maximum", 100);

        return editor;
    }
};
于 2012-06-03T09:56:08.103 に答える