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. Reading horizontal header item of QStandardItemModel
QtWS25 Last Chance

Reading horizontal header item of QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.8k Views
  • 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

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

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

      "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
      3
      • V Offline
        V Offline
        vorlket
        wrote on last edited by
        #3

        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).

        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