Lu Ma
Lu Ma

Reputation: 147

when the window of QFileDialog::getOpenFileName opening, the program has unexpected finished

I'm writing a simple Qt application to test multi-threading (something I am also completely new to).I made a QApplication to manage the GUI,then I write a class VisionApp that contains the class MainWindow which is a subclass of QMainWindow.

In the MainWindow class,I write a function void MainWindow::getfromfilevd() which is connected to the button using this:

QObject::connect(ui->FileVdButton,SIGNAL(clicked()),this,SLOT(getfromfilevd()));

Then I want to read a image from file by using QFileDialog::getOpenFileName,my code is here:

void MainWindow::getfromfilevd()
{
    //mutex.lock();
    from_imgorvd = true;

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),"", tr("Image Files (*.png *.jpg *.bmp *.xpm)"));
    if(fileName.isEmpty()) {
        cv::Mat image;
        image = cv::imread(fileName.toUtf8().constData(), CV_LOAD_IMAGE_COLOR);
        mutex.lock();
        Mat_Img = image.clone();
        mutex.unlock();
    }
}

however,every time I click the button ,the window of QFileDialog opened but it is blank,then my program finished unexpected.

when I use this code:

void MainWindow::getfromfilevd()
{

    from_imgorvd = true;

    cv::Mat image;
    image = cv::imread("/home/somnus/Picture/mouse.jpg", CV_LOAD_IMAGE_COLOR);
    if(! image.data) {
        std::cout << "Could not open or find the image" << std::endl ;
    }
    else {
        mutex.lock();
        Mat_Img = image.clone();
        mutex.unlock();
    }

}

It works well.

I am really wonder which mistake I take... Hope for your help

Upvotes: 4

Views: 1991

Answers (1)

Houssam Badri
Houssam Badri

Reputation: 2509

It should be like this, !fileName.isEmpty() in stead of fileName.isEmpty() because you need to load image when the file name is not empty not the opposite.

Upvotes: 2

Related Questions