Qt

QtQstringを文字列に変換する



Convert Qt Qstring String



OpencvでQTを使用すると、画像を開くときに問題が発生します。 Qt文字列はQstring形式です。この形式は、Opencvのimread()関数で直接使用することはできません。通常の文字列形式に変換した後にのみ読み取ることができます。この画像は再度読み取られ、処理されます。

文字列とQString間の変換



//From QString to std::string str = qstr.toStdString() //From std::string to QString qstr = QString::fromStdString(str)

ただし、現時点では中国語に変換できないため、中国語をパスに入れない方がよいので、手間が省けます。



ラベルを使用して、選択したパスを示します。

Opencvのimreadを使用して画像を読み取り、imshowで表示することができます。これは、この関数を文字列に直接変換して読み取ることができることを示しています。



#include 'qt1.h' #include #include #include #include #include #include #include #include #include #include #include'stdio.h' #include'iostream' using namespace std using namespace cv qt1::qt1(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this) } void qt1::button() { QString filename filename = QFileDialog::getOpenFileName(this, tr('Select image:'),//Use dialog to get the target address '1', tr('Images (*.png *.bmp *.jpg *.gif)')) if (filename.isEmpty()) { return } QImage image if (!image.load(filename)) { QMessageBox::information(this, tr('Error'), tr('Open file error')) return } QImage *imgScaled = new QImage *imgScaled = image.scaled(200, 200, Qt::KeepAspectRatio)//Set the size QPixmap pixmap = QPixmap::fromImage(*imgScaled) QSize imageSize = pixmap.size() ui.label->setPixmap(pixmap) ui.label->resize(imageSize) //qDebug() << 'filname: '