0

I can open the XLSX Excel format and read all data but only if the code for it is part of the constructor of the QMainWindow. For first tests to read the Excel it was fine. But when I move the algorithm to separate function to call it over the button press method it fail with popup error "Operating system is not configured to run this application".

Using Windows 10 / Qt 5.15.2 / MinGW

QMainWindow constructor:

t_ps_bps_grad2::t_ps_bps_grad2(QWidget *parent)
  : QMainWindow(parent)
  , ui(new Ui::t_ps_bps_grad2)
/* ************************************************************************** */
{
  ui->setupUi(this);
  db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
  // Working fine if function called from here: loadExcel();
}

function to load Excel:

void t_ps_bps_grad2::loadExcel()
/* ************************************************************************** */
{
  QString pathToExcel = "C:\data.xlsx";
  QString excelDriver = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + pathToExcel;

  db.setDatabaseName(excelDriver);

  if (db.open()) {
    QSqlQuery *query = new QSqlQuery(db);
    if (query->exec("select * from [Data$A1:B20000]")) {
      while (query->next()) {
        double double_p = query->value(0).toDouble();
        // ...
      }
    } 
  }
}

void t_ps_bps_grad2::on_pushButton_clicked()
/* ************************************************************************** */
{
  loadExcel(); // Not working from here !
}

Am I missing something?

Lodhart
  • 205
  • 1
  • 6
  • 14
  • If the db.open() is called in the constructor at least once (for some empty dummy.xlsx file) then it works properly later. Every click on the button will read the XLSX properly. Unfortunately for now I have to have this dummy XLSX close to my binary file for now. – Lodhart Sep 14 '22 at 12:32
  • I tried to convert the file to *.xls format and it works fine: DRIVER={Microsoft Excel Driver (*.xls)}. So it looks like an issue with driver for *.xlsx. – Lodhart Sep 15 '22 at 06:18

0 Answers0