Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Opening a File at QDir



  • Hello Everyone,

    I'm iterating over files in a folder with QDir und QStringList. With isDir I'm checking if it's a Folder or a Directory. If it's a Folder I want to open it and manipulate the content. Here's the Code:

    QDir ProjectPath("C:\\Users\\X240\\Desktop\\Masterarbeit\\ZF-Schulungssystem\\ZF-Schulungssystem\\TwinCAT\\");
    		int count = 0;
    		QStringList fileNameList = ProjectPath.entryList();
    		for (QStringList::iterator iter = fileNameList.begin(); iter != fileNameList.end(); iter++ )
    		{
    			count++;
    			//check if *iter is directory of File
    			bool isDir = ProjectPath.cd(*iter);
    			if (!isDir) {
    				printf("checking if the configuration exists\n");
    				QString ZFsolutionName = fileNameList.at(count);
    				QFile TCFile (ProjectPath + ZFsolutionName + ".sln"); 
    				if (!TCFile.exists())
    				{
    					printf(QString(ZFsolutionName + " does not exist\n").toStdString().c_str());
    				}
    

    Obviously

    QFile TCFile (ProjectPath + ZFsolutionName + ".sln");
    

    didn't work.

    How i can open files at the given Path?? How to convert the QDir to a QString ??

    Thanks



  • @JohnSRV said in Opening a File at QDir:

    How to convert the QDir to a QString ??

    QString QDir::dirName() const
    QString QDir::absolutePath() const
    QString QDir::absoluteFilePath(const QString &fileName) const
    QString QDir::canonicalPath() const
    QString QDir::filePath(const QString &fileName) const
    QFileInfoList QDir::entryInfoList(...) const
    QString QFileInfo::absoluteFilePath() const
    etc.



  • @JohnSRV

    QFile TCFile (ProjectPath.absolutePath() + ZFsolutionName + ".sln");
    

    This should probably work.

    Why aren't you using: https://doc.qt.io/qt-5/qdir.html#entryInfoList-1

    FileInfo would give you already the information if file or directory. Furthermore, you may use filters to exclude folders for your case.
    FileInfo would give you also the absolute file path https://doc.qt.io/qt-5/qfileinfo.html#absoluteFilePath


Log in to reply