QFileDialog loads twice (Xsheet feature)

Hi,
I have a problem that is driving me crazy.
If you saw the video of the Xsheet feature I am working on, you saw that I pressed the papagayo button, loaded the file, and everything was okay.
Now I have made the xsheet dockable, it still works, but the filedialog appears twice?! I have to pick the file twice? After I have picked it the first time, it loads the data into the xsheet, so it calls AND executes the writePapa function, before it reopens the fileDialog. Why? The function loadPapa is 100% the same code as before I made it dockable.

void Xsheet::loadPapa()
{
    mPapaLines->clear();
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("Open *.pgo file"), "", tr("Pgo Files (*.pgo)"));
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString tmp = in.readLine();
        tmp.remove("\t");
        mPapaLines->append(tmp);
    }
    file.close();
    writePapa();
}

Any ideas? I’m lost here…

Stick a qDebug() call in the loadPapa function. Is the function is getting called twice?

I have debugged it and I have set breakpoints. The result is the same.

  • It loads the papagayo file and closes the file.
  • It calls the writePapa(), and writes in the xsheet
  • it goes back to the ‘}’ after the writePapa() call
  • It jumps up to QTextStream in(&file);
  • It jumps up to QFile file(fileName)
  • it jumps back to the dialog, and opens the dialog…
    If I press Cancel the second time, it returns on the if (!file.open…, and the mPapaLines is empty, which means that the DIAL column will be erased at the next updateUi.

If I do a grep for loadPapa, it shows three results:
app/src/xsheet.cpp:48: connect(ui->btnPapa, SIGNAL(clicked(bool)), this, SLOT(loadPapa()));
app/src/xsheet.cpp:130:void Xsheet::loadPapa()
app/src/xsheet.h:37: void loadPapa();

The above should be Ok, and I have not made any connections in the xsheet.ui file. I have also tried changing the connect: connect(ui->btnPapa, &QPushButton::clicked, this, &Xsheet::loadPapa );. No luck whatsoever…

I think I will post a question in the Qt Forum. I’m sure it is a Qt issue more than a Pencil2D issue.

After reading my answer I found a workaround that at least will save the loaded data.
I moved the ‘mPapaLines->clear();’ to after the if (!file.open… check, so even if you press Cancel on the second call, the data stays intact.
The issue remains…

A guy on the Qt forum solved it.
My connects are in the initUI() function. This overridden function initUI() is called automatically, and I didn’t know that, so I called it manually right after ui->setupUi(this). Thus there were two connects to the loadPapa() function.
Learning every day…

1 Like