Reading horizontal header item of QStandardItemModel



  • With this set of codes rendering to a table view and enabling save to text file, I am having trouble reading the horizontal header labels. Reading the horizontal header item data returns nulls, and reading the header data leads to the program crashing.

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtSql>
    #include <QStandardItemModel>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QSqlDatabase db;
        QStandardItemModel *model;
    
    private slots:
        void on_actionSave_As_triggered();
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.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())
        {
            QSqlQuery query(db);
            if (query.exec("SELECT *, '' AS comment FROM audusd.ts_min_econevent ORDER BY min LIMIT 10000"))
            {
                model = new QStandardItemModel(query.size(), query.record().count(), this);
                QStringList header;
                int row = 0;
                while (query.next())
                {
                    for (int column = 0; column < query.record().count(); ++column)
                    {
                        if (row == 0)  header << query.record().fieldName(column);
                        QStandardItem *item = new QStandardItem(query.value(column).toString());
                        model->setItem(row, column, item);
                    }
                    ++row;
                }
                model->setHorizontalHeaderLabels(header);
            }
            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 j = 0; j < columns; j++)
        {
            // adding this line results in the program crashing: textData += model->headerData(columns, Qt::Horizontal).toString();
            textData += model->horizontalHeaderItem(columns)->data().toString();
            if (j < columns - 1) textData += "#";
        }
        textData += "\n";
    
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                if (!model->data(model->index(i, j)).isNull())
                {
                    textData += model->data(model->index(i, j)).toString();
                    if (j < columns - 1) textData += "#";
                }
            }
            textData += "\n";
        }
    
        QFile csvFile(fileName);
        if (csvFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        {
            QTextStream out(&csvFile);
            out << textData;
            csvFile.close();
        }
    }
    


  • textData += model->horizontalHeaderItem(columns)->data().toString(); should become textData += model->horizontalHeaderItem(j)->data().toString();



  • Yep, j instead of columns...

    With columns replaced with j, model->headerData(j, Qt::Horizontal).toString(); works (don't know why model->horizontalHeaderItem(j)->data().toString(); doesn't work - it returns null).


Log in to reply
 

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