简体   繁体   English

Qt文件夹浏览器多次打开

[英]Qt folder browser opening more than once

I have a Qt GUI app, and i have a button to browse for an output folder. 我有一个Qt GUI应用程序,并且有一个浏览输出文件夹的按钮。 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. 如果您重复此操作,则将其关闭3次,4次等。

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. 现在,我只是为单击按钮时添加了一个bool标志。 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 . 名称on_outputFolderBrowseBtn_pressed表示这是按下按钮时调用的插槽 In it, you are connecting the clicked signal of that same button with your slot. 在其中,您将同一按钮的clicked信号与插槽连接。 A new connection is created each time you press the button, so you end up with as many calls to your BrowseOutputFolder slot as you pressed the button. 每次您按下按钮都会创建一个新的连接,因此,您最终在按下按钮时对BrowseOutputFolder插槽的调用次数就会BrowseOutputFolder

Just call your slot instead of connecting it. 只需呼叫您的插槽即可,而不要连接它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM