It maybe a bug. Please read webclectic answer.
If QFileInfo(filename) loss ‘/’, QDir::absolutePath will return the parent string. Like below code.
QFileInfo file("e:/QtExample/mytest/out/res/res1/");
QFileInfo file_no("e:/QtExample/mytest/out/res/res1");
QDir dirFile = file.absoluteDir();
QDir dirFile_no = file_no.absoluteDir();
QString strDirFile = dirFile.absolutePath(); //"E:/QtExample/mytest/out/res/res1
QString strDirFile_no = dirFile_no.absolutePath(); //"E:/QtExample/mytest/out/res
I found it with my QTreeView. My code will call the slot from QTreeView::clicked signals
connect(ui.m_pView,SIGNAL(clicked(QModelIndex)),this,SLOT(myClicked(QModelIndex)));
the slot will get the QModelIndex, then I use QFileSystemMode::fileInfo get the QFileInfo.
QFileInfo rFileInfo = m_model.fileInfo(index);
QDir absDir = rFileInfoDir.absoluteDir();
But the return of QFileInfo always return “e:/QtExample/mytest/out/res/res”, so If I call QFileInfo::absoluteDir get the directory, the directory is the parent of “res1”.So I will get wrong entrylst from my hope directory.
Should I add the ‘/’ after the absoluteFilePath() to get the right QDir?
And why strDirPath equal “E:/QtExample/mytest/out/res/res1”, but rDir will list the “res” directory entrylist?
//rFileInfoDir == E:/QtExample/mytest/out/res/res1
QString strDirPath = rFileInfoDir.absoluteFilePath();
QDir rDir = rFileInfoDir.absoluteDir();
If you go deep into the
absoluteDir()function call you will see why this happens. In Windows thefileNamefunction inqfsfileengine_win.cppis called. In this function there is this code part:You can see that it returns the part of the string left of the last separator. I don’t know if this is the desired behavior or a bug. Maybe you could issue a
Qtbug for this. The documentation is not clear on this matter.