PDA

View Full Version : QFileDialog called twice?



davidlamhauge
28th October 2018, 08:30
Hi,
I have a problem that is driving me crazy.
I am working on an animation software. I made a suggestion for a Xsheet feature, that would show you the present drawings in your project. In the QDialog there was a Papagayo button, and if you pressed the button, you could choose a papagayo file, and have the contents loaded to the QStringList mPapaLines, from where it is written to the Xsheet (a QTableWidget). It worked flawlessly.
There are two functions loadPapa() and writePapa(). loadPapa() is called when you open the file, and read its content into a QStringList. writePapa() is called after loadPapa(), and everytime there are changes in the Xsheet.
Now I have (upon request) made the xsheet dockable, It is the same code and 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();
}
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 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();

There are no connections made in the xsheet.ui, and I am out of ideas.
Can anyone explain what is happening?

ChristianEhrlicher
28th October 2018, 11:39
Maybe xsheet.cpp:48 is called twice so there are two signal/slot connections

davidlamhauge
28th October 2018, 12:04
Maybe xsheet.cpp:48 is called twice so there are two signal/slot connections

That was it!
The connect is in the initUI() function. It's the first time I use a class with such a function, so I called initUI() rigth after ui->setupUi(this), without knowing that it is called automatically.
You get wiser by the day...