2014-10-28 5 views
6

में उपयोगकर्ता इनपुट को मान्य करना मेरे पास QTableView है और मैं उपयोगकर्ता इनपुट को सत्यापित करना चाहता हूं। यदि उपयोगकर्ता QTableView के किसी सेल में एक अवैध मान डालता है, तो मैं उस सेल को हाइलाइट करना चाहता हूं और QPushButton अक्षम करना चाहता हूं।QTableView

मैं इसे कैसे प्राप्त कर सकता हूं? क्या मैं QValidator का उपयोग कर सकता हूं?

उत्तर

9

हां, आप यह कर सकते हैं, इस उद्देश्य के लिए कस्टम QItemDelegate का उपयोग करें (मैंने उदाहरण के रूप में QIntValidator का उपयोग किया था)।

हैडर:

#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 

सीपीपी

#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); 
} 

उपयोग:

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

इस मामले उपयोगकर्ता में सक्षम इनपुट गलत डेटा नहीं होगा, लेकिन आप अगले उपयोग कर सकते हैं:

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); 
    } 
} 

लेकिन इस मामले में अपने कोड

+0

से editor->setValidator(new QIntValidator); लाइन को निकालना न भूलें, धन्यवाद, यह काम करता है। लेकिन जब मैं एक वैध इनपुट डालता हूं तो मैं कोशिकाओं में संख्याओं को हटा नहीं सकता। मुझे क्या करना चाहिए? – splunk

+0

@Aimcorz मुझे लगता है कि आप मेरे पहले उदाहरण का उपयोग करते हैं, इसलिए मेरे कंप्यूटर सत्यापनकर्ता ब्लॉक में प्रवेश करते हैं लेकिन किसी अन्य सेल में चयन बदलना सामान्य काम करता है। मैं सुझाव दे सकता हूं कि आप मेरा दूसरा उदाहरण उपयोग करें, इससे बचने के लिए कुछ तरीका प्रदान करें, उदाहरण के लिए खाली स्ट्रिंग 'मॉडल-> सेटडाटा (इंडेक्स, "", क्यूटी :: एडिटरोल) सेट करें;' या कुछ और। – Chernobyl

संबंधित मुद्दे