2014-10-28 23 views

Odpowiedz

9

Tak, możesz to zrobić, użyj niestandardowego QItemDelegate do tego celu (użyłem jako przykładu QIntValidator).

Header:

#ifndef ITEMDELEGATE_H 
#define ITEMDELEGATE_H 

#include <QItemDelegate> 

class ItemDelegate : public QItemDelegate 
{ 
    Q_OBJECT 
public: 
    explicit ItemDelegate(QObject *parent = 0); 

protected: 
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 
    void setEditorData(QWidget * editor, const QModelIndex & index) const; 
    void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; 
    void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const; 

signals: 

public slots: 

}; 

#endif // ITEMDELEGATE_H 

Cpp

#include "itemdelegate.h" 
#include <QLineEdit> 
#include <QIntValidator> 

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

QWidget *ItemDelegate::createEditor(QWidget *parent, 
            const QStyleOptionViewItem &option, 
            const QModelIndex &index) const 
{ 
    QLineEdit *editor = new QLineEdit(parent); 
    editor->setValidator(new QIntValidator); 
    return editor; 
} 


void ItemDelegate::setEditorData(QWidget *editor, 
           const QModelIndex &index) const 
{ 
    QString value =index.model()->data(index, Qt::EditRole).toString(); 
     QLineEdit *line = static_cast<QLineEdit*>(editor); 
     line->setText(value); 
} 


void ItemDelegate::setModelData(QWidget *editor, 
           QAbstractItemModel *model, 
           const QModelIndex &index) const 
{ 
    QLineEdit *line = static_cast<QLineEdit*>(editor); 
    QString value = line->text(); 
    model->setData(index, value); 
} 


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

Zastosowanie:

#include "itemdelegate.h" 
//... 
ItemDelegate *itDelegate = new ItemDelegate; 
ui->tableView->setItemDelegate(itDelegate); 

W tym przypadku użytkownik nie będzie mógł wejść błędne dane, ale można używać dalej:

void ItemDelegate::setModelData(QWidget *editor, 
           QAbstractItemModel *model, 
           const QModelIndex &index) const 
{ 
    QLineEdit *line = static_cast<QLineEdit*>(editor); 

    QIntValidator validator; 
    int pos = 0; 
    QString data = line->text(); 
    if(validator.validate(data,pos) != QValidator::Acceptable) 
    { 
     qDebug() << "not valid";//do something 
    } 
    else 
    { 
     model->setData(index, data); 
    } 
} 

Ale w tym przypadku nie zapomnij usunąć linię editor->setValidator(new QIntValidator); ze swojego kodu

+0

Dziękuję, to działa. Ale nie mogę usunąć liczby w komórkach, gdy wstawię prawidłowe dane wejściowe. Co powinienem zrobić? – splunk

+0

@Aimcorz Przypuszczam, że używasz mojego pierwszego przykładu, więc na moim komputerze walidator blokuje wprowadzanie, ale zmiana wyboru na inną komórkę działa normalnie. Mogę zasugerować, żebyś użył mojego drugiego przykładu, w jakiś sposób tego uniknąć, na przykład ustaw pusty łańcuch 'model-> setData (index," ", Qt :: EditRole);" lub coś innego. – Chernobyl