0

I am very new to Qt and OpenCV and am creating a project integrating both. The problem I am running into is that I have a button to load a file, which uses QFileDialog. The whole thing runs smoothly and my file gets loaded. However, it crashes if I click the load button a second time. It seems like the problem occurs at the call to QFileDialog::getOpenFileName, but I need an expert opinion.

This is the function for the button click.

void MainWindow::on_pushButton_clicked()
{
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);
//    dialog.setAttribute(Qt::WA_DeleteOnClose);
//    dialog.DontUseNativeDialog;

    filename = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                "/home",
                                                tr("Images (*.png *.xpm *.jpg)"));
    imageObject = new QImage();
    imageObject->load(filename);
    image = QPixmap::fromImage(*imageObject);
    scene = new QGraphicsScene(this);
    scene->addPixmap(image);
    scene->setSceneRect(image.rect());

    ui->graphicsView->setScene(scene);
    ui->graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);

    cvHandler = new OpenCVHandler(filename.toStdString());
}

I have already tried both the lines that are commented out. My search also turned up nothing that I could understand easily:

Crash when calling getOpenFileName from QItemDelegate's custom editor

QFileDialog opens a second (possibly parent) unwanted window

Qt File Dialog Rendered Incorrectly and Crashes

If at all relevant, I am on an Ubuntu 16.04 LTS system.

Thank you

Sami
  • 83
  • 2
  • 10
  • I don't know the function `QFileDialog::getOpenFileName` but it seems you work with files. In this case you have to be aware that a file can only be opened once, and if some program tries to open it another time an error occurs. So if you want to access a file, you first have to make sure that no other program uses it/ you have to make sure the file is properly closed when not needed anymore. – Lehue Sep 28 '17 at 15:50
  • Does it crash if you comment out all the code in that function following getting the file name? Comment out every line in `MainWindow::on_pushButton_clicked()` following `QFileDialog::getOpenFileName`. I think the bug is in a different place than you expect. – drescherjm Sep 28 '17 at 15:52
  • @drescherjim It still crashes the second time. The Window freezes and I have to force quit. – Sami Sep 28 '17 at 15:58
  • @A. Hue This makes sense. However, I am not sure how to do that. I am not explicitly opening the file, just calling this function. – Sami Sep 28 '17 at 16:01
  • try to place `try:` and `except: pass` around it, with a return between except and pass – Lehue Sep 28 '17 at 16:04
  • I also tried dialog.close(), but still no cigar. – Sami Sep 28 '17 at 16:10
  • 1
    I see that you create an object called a dialog of the class QFileDialog but you never use it, so that you believe ?. besides what task does OpenCVHandler perform? – eyllanesc Sep 28 '17 at 16:45
  • Are you working in a loop? Those who work with OpenCV have the habit of always wanting to use a while True, and this is not GUI friendly, if so, try to comment on the whole opencv code and check if you still have the same problem. – eyllanesc Sep 28 '17 at 16:48
  • Thanks @eyllanesc that wasn't the problem. It was much simpler and sillier than that. I've answered it myself. – Sami Sep 28 '17 at 17:11

1 Answers1

3

The problem was in the commented lines. I didn't use dialog.DontUseNativeDialog properly. Using it inside the getOpenFileName function did the trick:

filename = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                "/home",
                                                tr("Images (*.png *.xpm *.jpg)"),0,QFileDialog::DontUseNativeDialog);

Thank you all.

Sami
  • 83
  • 2
  • 10
  • I would get rid of `dialog` since you don't use it at all. The static `QFileDialog::getOpenFileName()` makes no use of your dialog. – drescherjm Sep 28 '17 at 17:17