• 4522阅读
  • 5回复

Qtableview 如何委托实现按下回车到下一行? [复制链接]

上一主题 下一主题
离线lastforest
 

只看楼主 倒序阅读 楼主  发表于: 2020-08-12
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;        
      }
    }
}

离线lastforest

只看该作者 1楼 发表于: 2020-08-13
难道有这么难么, 我感觉是一个比较简单的问题啊 ,为什么这么多浏览,没人回答呢~~!
离线lastforest

只看该作者 2楼 发表于: 2020-08-13
我是想,能不能在eventFilter里捕获key_Return,然后发射信号,再在主程序里处理,但是我实在是不知道,如何获取委托类发射的信号,真的是难到我了!  
离线alexltr

只看该作者 3楼 发表于: 2020-08-16
前段时间我也在做相同的需求:
    if 按回车键
        if 内容合法
            if 不是在最后一行,则定位到下一行
            if 是在最后一行,则增加一行,并定位到新增的行

一开始也是想在delegate里面实现,但尝试后还是改在主程序里去处理了,主要是好判断及定位tableView的当前位置
tableView->installEventFilter(this);
evenFilter的代码大概如下, 供参考.
你也可以继续在delegate里尝试,有结果后再跟大家分享一下


  1. //当用户在tableViewXXX的最后一行按向下键或回车键时,新增加一行
  2. bool xxxEditor::eventFilter(QObject *obj, QEvent *event)
  3. {
  4.     if (event->type() == QEvent::KeyPress) {
  5.         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
  6.         QModelIndex index = ui->tableViewXXX->currentIndex();
  7.         if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Return
  8.              || keyEvent->key() == Qt::Key_Enter) {
  9.             QString XXXCode = mXXXModel->data(index, Qt::EditRole).toString();
  10.             //如果输入的XXX代码正确
  11.             if (!XXXCode.isEmpty()) {
  12.                 //如果当前是最后一行,则添加一行
  13.                 if (index.row() == mXXXModel->rowCount() - 1) {
  14.                     addXXXRow();
  15.                 } else { //如果不是最后一行,则定位到下一行
  16.                     index = mXXXModel->index(index.row() + 1, index.column());
  17.                     ui->tableViewXXX->setCurrentIndex(index);
  18.                 }
  19.                 return true;
  20.             } else { //如果输入的XXX代码不正确, 则保持在当前行
  21.                 return false;
  22.             }
  23.         }
  24.     }
  25.     return QObject::eventFilter(obj, event);
  26. }





我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线alexltr

只看该作者 4楼 发表于: 2020-08-16
回 lastforest 的帖子
lastforest:我是想,能不能在eventFilter里捕获key_Return,然后发射信号,再在主程序里处理,但是我实在是不知道,如何获取委托类发射的信号,真的是难到我了![表情]   (2020-08-13 19:47) 


connect(ui->tableView->itemDelegate(), SIGNAL().......

这个捕捉不到信号吗?
我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线lastforest

只看该作者 5楼 发表于: 2020-08-21
是的,终于尝试试成功了。就是用信号
电脑坏了,源码发不上来了。大致是在eventfilter中发射信号,在主调用程序中处理。
主要是要申明一个delegate实例,就能取得信号了。
快速回复
限100 字节
 
上一个 下一个