D
D
Denis2021-06-10 15:16:51
Qt
Denis, 2021-06-10 15:16:51

How to display ACCESS table in QTablView?

I just started to master cpp and QT, and there was a need for information from the access database to appear in the tableView.
However, the debugger complains and issues:
"QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
Error in opening
16:27:57: C:\Users\user1\Desktop\QT\projects\build-QVIZ-Desktop_Qt_6_1_0_MinGW_64_bit-Release\release \QVIZ.exe exited with code 0"

I can't figure out what to do about it.
Most likely there is an error somewhere in the code, but apparently there is not enough experience to understand where.
Here is the code in main.cpp

#include "mainwindow.h"
#include <QApplication>
 
 
int main(int argc, char *argv[])
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
        db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DNS=''; resultsDB.accdb");
        if (db.open())
            qDebug() << "good";
        else
        {
            qDebug() << "bad";
            return -0;
        }
 
        QTableView view;
        QSqlTableModel model;
 
        model.setTable("resultsDB");
        model.select();
        view.setModel(&model);
        view.show();
 
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


Here is a piece from mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("placeholdername");
    setWindowIcon(QIcon(":/img/img/Icon.png"));
    setCentralWidget(ui->stackedWidget);
 
 
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
        db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DNS=''; resultsDB.accdb");
        if (db.open())
            qDebug() << "good";
        else
        {
            qDebug() << "bad";
            return -0;
        }
 
        QSqlTableModel model;
        model.setTable("resultsDB");
        model.select();
        ui->tableView->setModel(&model);
 
};


The database named resultsDB is located in the release folder of the project.

And there is one more question, immediately. How to make it so that the text from the prescribed lineages goes to a certain cell opened in the tableView database. It is necessary to make it so that when writing the text in lineedits, he goes to an empty database, where there are simply cells for him. It may not be necessary to mess with the tableView if it can be done.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question