lundi 25 mars 2019

QSqlTableModel and QSQLITE not showing correct columns & header

On a user interface I have a QTableView that looks like this at the very beginning after first start:

QTableView

As soon as the user run the project and create a new database .db (say the user puts the .db file on the Desktop), this QTableView looks like this which the correct behavior:

Correct_QTableView

The problem: After I start saving images and their path all the headers carries different names (1,2,3,4) instead of (path1, path2, image1, image2) as shown previously and there is an id column missing, like this below which is the wrong behavior:

wrong

Below is how I am setting the most important parameters:

mainwindow.h

private:
    QString temporaryFolder;
    dataInfo *mNewDatabaseImages;
    QSqlTableModel *mNewTableImages;
    QStandardItemModel *model;

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    temporaryFolder = "/home/path/toDesktop/folder/a.db";
    QFile dbRem(temporaryFolder);
    dbRem.remove();

    mNewDatabaseImages = new dataInfo(this);
    mNewDatabaseImages->initDataBase(temporaryFolder);
    mNewDatabaseImages->confDataBase();
    mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
    mNewTableImages->setTable("imageTable");
    mNewTableImages->select();
    ui->bookMarkTableView->setModel(mNewTableImages);

    model = new QStandardItemModel();
    ui->bookMarkTableView->setModel(model);
    ui->bookMarkTableView->showColumn(true);
}

In addition to that there are two icons on the user interface with which I open an existing database or I create a new database as it is possible to see below:

icons

This part is on the mainwindow.cpp too and below is the snipped of code:

void MainWindow::openDatabase(MainWindow::Opening opening)
{
    QString nameDatabase;
    if(opening == OPENING) {
        nameDatabase = QFileDialog::getOpenFileName(this,
                                                    "Open Database", QDir::rootPath(),
                                                    "Database (*.db);;Any type (*.*)");
    } else {
        nameDatabase = QFileDialog::getSaveFileName(this,
                                                    "New Database", QDir::rootPath(),
                                                    "Database (*.db);;Any type (*.*)");
    }
    if(nameDatabase.isEmpty()) {
        return;
    }
    if(!mNewDatabaseImages->initDataBase(nameDatabase)) {
        QMessageBox::critical(this, "Error", mNewDatabaseImages->getError());
        return;
    }
    if(!mNewDatabaseImages->confDataBase()) {
        QMessageBox::critical(this, "Error", mNewDatabaseImages->getError());
        return;
    }
    delete mNewTableImages;
    // Work with the database file created
    mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
    mNewTableImages->setTable("imageTable");
    mNewTableImages->select();
    ui->bookMarkTableView->setModel(mNewTableImages);
    ui->bookMarkTableView->showColumn(true);
}

The database I am using is QSQLITE and the way the SQL is written is here if there is any need to see the snipped of that too.

Why does QTableView is not showing the correct headers and columns?

Thanks for pointing in the right direction.

Aucun commentaire:

Enregistrer un commentaire