Mix of editable and non-editable column(s) in table view



  • I am working with materialized view in postgres for rendering a table view. What class should I use if I want to make queried materialized view column(s) non-editable and extra blank column(s) editable, and later save them as a csv file? I thought about creating blank column(s) in the view and use QSqlTableModel to make them editable but it would make the other columns editable as well.


  • Lifetime Qt Champion

    Hi,

    One thing you can do is put a QIdentityProxyModel where you reimplement the flags method to return the correct values depending on the column.

    Hope it helps



  • Thanks, it works. How do you display user (from keyboard) input in the cell of editable column?


  • Lifetime Qt Champion

    If the cell contains text, triggering an edit e.g. by double clicking the cell should give you an editor.



  • The cell contains nothing (it's a comment column in which i write comment on other columns and save as text file, without modifying the backend data). Double-click allows me to edit, but it doesn't display the input data. Here's what I've done:

    editablesqlmodel.h:

    #ifndef EDITABLESQLMODEL_H
    #define EDITABLESQLMODEL_H
    
    #include <QSqlQueryModel>
    
    class EditableSqlModel : public QSqlQueryModel
    {
    public:
        EditableSqlModel();
        Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    };
    
    #endif // EDITABLESQLMODEL_H
    

    editablesqlmodel.cpp:

    #include "editablesqlmodel.h"
    
    EditableSqlModel::EditableSqlModel()
    {
    
    }
    
    Qt::ItemFlags EditableSqlModel::flags(const QModelIndex &index) const
    {
        Qt::ItemFlags flags = QSqlQueryModel::flags(index);
        if (index.column() == 15) flags |= Qt::ItemIsEditable;
        return flags;
    }
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "editablesqlmodel.h"
    
    #include <QFileDialog>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("192.168.2.103");
        db.setPort(5433);
        db.setUserName("vorlket");
        db.setPassword("K1156312j");
        db.setDatabaseName("fxproj");
    
        if (db.open())
        {
            this->model = new EditableSqlModel();
            model->setQuery("SELECT *, '' AS comment FROM audusd.ts_month_econevent ORDER BY month");
    
            ui->tableView->setModel(model);
        }
    
    }
    
    MainWindow::~MainWindow()
    {
        db.close();
        delete ui;
    }
    
    void MainWindow::on_actionSave_As_triggered()
    {
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save Comment on Economic Event"), "", tr("CSV (*.csv);;All Files (*)"));
    
        QString textData;
        int rows = model->rowCount();
        int columns = model->columnCount();
    
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                textData += model->data(model->index(i, j)).toString();
                textData += ",";
            }
            textData += "\n";
        }
    
        QFile csvFile(fileName);
        if (csvFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        {
            QTextStream out(&csvFile);
            out << textData;
            csvFile.close();
        }
    }
    


  • what do you mean by

    allows me to edit, but it doesn't display the input data

    could you explain better?

    P.S.
    in MainWindow::on_actionSave_As_triggered before textData += model->data(model->index(i, j)).toString(); you have to check if the appended string contains , and do not append an empty column at the end

    for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
    if(j>0)
     textData += ',';
    const QString cellData = model->data(model->index(i, j)).toString();
    if(cellData.contains(','))
     textData += '\"' + cellData + '\"' ;
    else
    textData += cellData;
    }
    textData += "\n";
    }
    


  • The double-click triggers the edit mode but data input disappears upon leaving the cell. So You enter the input, press enter, and you see nothing.

    P.S. Ok, thanks.



  • This is probably because you use QSqlQueryModel as a base, i'm quite sure the setData() for that model does nothing.
    You could consider switching to QStandardItemModel or use a proxy like KExtraColumnsProxyModel to add the editable extra column



  • Yeah that makes sense. I will try your suggestion. Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.