Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mix of editable and non-editable column(s) in table view
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vorlket
    wrote on last edited by vorlket
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vorlket
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vorlket
            wrote on last edited by vorlket
            #5

            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();
                }
            }
            
            1 Reply Last reply
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vorlket
                wrote on last edited by vorlket
                #7

                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.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2
                  • V Offline
                    V Offline
                    vorlket
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved