I have a Qt GUI app, and i have a button to browse for an output folder. But there is a bug i cannot figure out. When you launch the app and click the browser button it works fine and you can close it, etc. Then if you click the button a second time, a browser window opens and you can select the folder, but this time when you close it, the window immediately reappears a second time. And if you repeat this, it will make you close it 3 times, 4 times, etc.
I have been unable to see why this is happening with the code i have at the moment.
void Dialog::on_outputFolderBrowseBtn_pressed()
{
QObject::connect(ui->outputFolderBrowseBtn, SIGNAL(clicked()), this, SLOT(BrowseOutputFolder()));
}
void Dialog::BrowseOutputFolder()
{
QFileDialog dialog;
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::ShowDirsOnly);
QString folderName = QFileDialog::getExistingDirectory(this, tr("Output folder"), "", QFileDialog::ShowDirsOnly);
if(folderName.size() != 0)
{
QDir folder(folderName);
if(!folder.exists())
{
SecureLogger::Instance()->LogError("Folder does not exist ", __FILE__, __LINE__);
}
ui->OutputFolderPath->setText(folderName);
}
}
I have ran it through the debugger and it keeps jumping back to this line:
QString folderName = QFileDialog::getExistingDirectory(this, tr("Output folder"), "", QFileDialog::ShowDirsOnly);
Can anyone see why this is happening?
EDIT: I have fixed the problem, but not necessarily solved the issue. For now i simply added a bool flag for when the button is clicked. So the code now looks like this:
void Dialog::on_outputFolderBrowseBtn_pressed()
{
m_clicked = true;
QObject::connect(ui->outputFolderBrowseBtn, SIGNAL(clicked()), this, SLOT(BrowseOutputFolder()));
}
void Dialog::BrowseOutputFolder()
{
QString folderName;
if (m_clicked)
{
folderName = QFileDialog::getExistingDirectory(this, tr("Select Folder"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
m_clicked = false;
}
if(!folderName.isEmpty())
{
QDir folder(folderName);
if(!folder.exists())
{
SecureLogger::Instance()->LogError("Folder does not exist ", __FILE__, __LINE__);
}
ui->OutputFolderPath->setText(folderName);
}
}
This is just hack work around and doesnt tell me what was wrong in the first place, so any insight on why that line was getting called over and over would be greatly appreciated.
The name
on_outputFolderBrowseBtn_pressed
means that this is a slot called when the button is pressed. In it, you are connecting theclicked
signal of that same button with your slot. A new connection is created each time you press the button, so you end up with as many calls to yourBrowseOutputFolder
slot as you pressed the button.Just call your slot instead of connecting it.