取消QTableView中的单元格值

时间:2014-10-29 18:41:08

标签: c++ qt qtableview

如何从QTableView中的单元格中删除值? 我创建了一个QTableView,但如果我按下所选单元格上键盘的取消按钮,则没有任何反应。 如果我要删除该值,我必须双击该单元格并按取消,但我想删除该值而不双击,只需选择单元格并按Canc

keyEvent->key()有用吗?

 @Chernobyl

    MAINWINDOW.H

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QKeyEvent>
    #include <QTableView>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0) ;
        ~MainWindow();
        QTableView *griglia;

    protected:
        bool eventFilter(QObject *obj, QEvent *event);

    private:
        Ui::MainWindow *ui;


    };

    #endif // MAINWINDOW_H





    MAINWINDOW.CPP

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        qApp->installEventFilter(this);
    }


    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        if(obj == griglia && event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_Cancel)
            {
               QModelIndex in =griglia->currentIndex();
               griglia->model()->setData(in," ");
            }
        }
        return QObject::eventFilter(obj, event);
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }




MAIN.CPP



#include "mainwindow.h"
#include "itemdelegate.h"
#include "mymodel.h"
#include <QApplication>
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QIdentityProxyModel>
#include <QRegExpValidator>

#define GRIGLIA_RX "^[F0-9]|1[0-9]|2[0-4]$"


int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QFile styleFile( "style.qss" );
    styleFile.open( QFile::ReadOnly );
    QString style( styleFile.readAll() );
    a.setStyleSheet( style );

    QWidget *mainWindow = new QWidget;    
    QStandardItemModel *model = new QStandardItemModel(48,33);  
    QTableView *griglia = new QTableView;
    griglia->setModel(model);

    QPushButton *calcola = new QPushButton;
    calcola->setText("CALCOLA");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(griglia);
    layout->addWidget(calcola);
    mainWindow->setLayout(layout);


    ItemDelegate *itDelegate = new  ItemDelegate;
    griglia->setItemDelegate(itDelegate);
    mainWindow->showMaximized();
    return a.exec();
}

我添加了QDebug线,但如果我按下Canc,则不会显示任何消息

1 个答案:

答案 0 :(得分:1)

在我的示例中使用下一个事件过滤器或重新实现keyPressEvent

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == ui->tableView && event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if(keyEvent->key() == Qt::Key_Shift)
        {
           qDebug() << "works";
           QModelIndex in =  ui->tableView->currentIndex();
           ui->tableView->model()->setData(in,"");
           //ui->tableView->model()->setData(ui->tableView->currentIndex(),""); //or just this

        }
    }
    return QObject::eventFilter(obj, event);
}

要使用eventFilter,您还应该:

protected:
bool eventFilter(QObject *obj, QEvent *event);//in header

qApp->installEventFilter(this);//in constructor

您可以使用Qt::Key_ShiftQt::Key_Cancel或其他内容。

修改

MAINWINDOW.H

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QKeyEvent>
    #include <QTableView>
    #include "itemdelegate.h"
#include "mymodel.h"
#include <QApplication>
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QIdentityProxyModel>
#include <QRegExpValidator>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0) ;
        ~MainWindow();
        QTableView *griglia;

    protected:
        bool eventFilter(QObject *obj, QEvent *event);

    private:
        Ui::MainWindow *ui;


    };

    #endif // MAINWINDOW_H





    MAINWINDOW.CPP

    #include "mainwindow.h"
    #include "ui_mainwindow.h"


    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
            QWidget *mainWindow = new QWidget;    
    QStandardItemModel *model = new QStandardItemModel(48,33);  
    griglia = new QTableView;
    griglia->setModel(model);

    QPushButton *calcola = new QPushButton;
    calcola->setText("CALCOLA");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(griglia);
    layout->addWidget(calcola);
    mainWindow->setLayout(layout);


    ItemDelegate *itDelegate = new  ItemDelegate;
    griglia->setItemDelegate(itDelegate);

    this->setCentralWidget(mainWindow);
        qApp->installEventFilter(this);
    }


    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        if(obj == griglia && event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_Cancel)
            {
               QModelIndex in =griglia->currentIndex();
               griglia->model()->setData(in," ");
            }
        }
        return QObject::eventFilter(obj, event);
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }




MAIN.CPP



#include "mainwindow.h"


int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QFile styleFile( "style.qss" );
    styleFile.open( QFile::ReadOnly );
    QString style( styleFile.readAll() );
    a.setStyleSheet( style );

    MainWindow my;
    my.showMaximized();
    return a.exec();
}

现在你应该看到&#34;工作&#34;和单元格应该被清除,另一个代码优化由你自己完成。

添加多项选择:

if(obj == ui->tableView && event->type() == QEvent::KeyPress)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if(keyEvent->key() == Qt::Key_Shift)
    {
        qDebug() << "Up";
       //ui->tableView->model()->setData(ui->tableView->currentIndex(),"");
       QList<QModelIndex> index = ui->tableView->selectionModel()->selectedIndexes();
       for(int i = 0; i < index.size(); i++)
           ui->tableView->model()->setData(index.at(i),"");
    }
}