qt 新手一枚。
我想实现给tableview的所有行都委托一个LINEEDIT,设置lineEdit的样式,现在我成功实现了将lineedit委托在QtableView的单元格中,
并限制各行中LINEEDIt的输入,如只能是数字,只能是字母或者其他等等,但是现在在lineedit中按下回车,如何到下一行难住我了,请各位大神指导:
我的委托类的代码如下:
cpp 的代码~
#include "grddelegate.h"#include <QRegExpValidator>#include <QKeyEvent>#include <QLineEdit>#include <QDebug>#include <QPalette>
LineEidtDelegate::LineEidtDelegate(QObject *parent)
: QStyledItemDelegate(parent){ // Frow = row;}
QWidget *LineEidtDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
QLineEdit *edt = new QLineEdit(parent);
// edt->setStyleSheet("background-color:yellow;border-color:black;border-style:outside;border-width:2px");
edt->setStyleSheet("border: 1px solid rgb(41, 57, 85);\
border-radius: 3px;\
background: white;\
selection-background-color: green;\
font-size: 14px");
switch (index.row()) { case 0:{ //有效值
edt->setValidator(new QIntValidator(0,99999));
connect(edt,SIGNAL(editingFinished()),SLOT(commiteAndClostEditor()));
qDebug()<<"createEditor";
return edt;
}
}
void LineEidtDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
QLineEdit *edt = static_cast<QLineEdit*>(editor);
QString text = edt->text();
model->setData(index,text,Qt::DisplayRole|Qt::EditRole);
qDebug()<<"setModelData";
}
void LineEidtDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
QString str = index.model()->data(index,Qt::EditRole).toString();
QLineEdit *edt = static_cast<QLineEdit*>(editor);
edt->setText(str);
qDebug()<<"setEditorData";
}
void LineEidtDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
editor->setGeometry(option.rect);
qDebug()<<"updateEditorGeometry";
}
void LineEidtDelegate::commiteAndClostEditor(){
QLineEdit *edt = qobject_cast<QLineEdit*>(sender());
emit commitData(edt);
emit closeEditor(edt);
}
bool LineEidtDelegate::eventFilter(QObject *object, QEvent *event){
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
//qDebug()<<keyEvent->key();
if (keyEvent->key() == Qt::Key_Enter||keyEvent->key() == Qt::Key_Return) //16777220 16777221
{
// qDebug()<<keyEvent->key();
emit keyEnterPess();
return true;
}
}
}