Problem with QString storing file path
-
This program manages and stores multiple image files.
Select and open multiple files via QFiledialog. This file is passed to the background object created by list.
At this time, the path of each file is also passed and saved.QFileDialog dialog(this, tr("Open File")); initializeImageFileDialog(dialog, QFileDialog::AcceptOpen); if(dialog.exec() == QDialog::Accepted){ QStringList fileNames = dialog.selectedFiles(); QString message = "Selected files: "; for (const QString& fileName : fileNames) { message += fileName + "\n"; } for (const QString& fileName : fileNames){ if(!m_pPubManager->AddList(fileName, *setImageBuffer(fileName))){ QMessageBox::information(nullptr, "File Open", "Error: This should be wrong dirname value. \n" + fileName); } }
WPubManager manages the background as a list. list<CBackground *> m_pList;
And add:bool WPubManager::AddList(const QString &fileName, const QImage &img) { m_properties.count = cnt_list++; if(!fileName.isNull()){ m_pBkgnd = new CBackground(cnt_list, fileName, img); if(m_pBkgnd){ m_pList.push_back(m_pBkgnd); return true; } } return false; }
The background constructor saves the passed file path and outputs it for testing in the constructor function. The output is correct.
CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) : m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, m_strBkgndImagePath(&fileName) { .... cout << cnt << "\t" << m_strBkgndImagePath->toStdString() << "\t" << size.width() << endl; // output correct file path value }
However, if you try to output the file path while traversing the object as follows, the file path is broken. An unknown character value is output.
void WPubManager::GetNext() { if (++pos != m_pList.end()) { m_pBkgnd = *(pos); cout << m_pBkgnd->getBkgndPath().toStdString() << endl; } else { cout << "Reached the end of the book." << endl; pos--; } }
Is this a unicode related issue? how can i solve it?
-
@MyNameIsQt Do you paths contain any non-ascii characters?
-
@MyNameIsQt This does not answer my question...
-
@MyNameIsQt said in Problem with QString storing file path:
CBackground::CBackground(unsigned int cnt, const QString &fileName, const QImage &img) :
m_pBkgndHeader{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
m_strBkgndImagePath(&fileName)Hello dangling pointer... c++ basics.