0

I created the simplest Gui with only one button to read and show an image using Qt, the slot of the button is:

    void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open image"),
                                                    tr("."),
                                                    tr("Image Files (*.png *.jpg *.jpeg *.bmp * .tif)"));

    image= cv::imread(fileName);
    cv::namedWindow("Original Image");
    cv::imshow("Original Image", image);
}

I have the following error:

error: invalid initialization of reference of type 'const string& {aka const std::basic_string<char>&}' from expression of type 'QString'
     image= cv::imread(fileName);
                               ^

How to use successfully the QFileDialog class to pass a valid path to the imread?

Apastrix
  • 114
  • 2
  • 12
  • 2
    Not exactly a duplicate, but see the answers to this question: http://stackoverflow.com/questions/4214369/how-to-convert-qstring-to-stdstring – Jim Lewis Feb 19 '15 at 20:20

1 Answers1

0

as you can see in the opencv documentation , imread method must be used as it follows: Mat cv::imread ( const String & filename, int flags = IMREAD_COLOR ); QString is a type of string compatible only with the qt libraries, so you must convert QString to String in order to properly use the imread method.

use : imread(fileName.toStdString());