Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use the QCommandLineParser
Forum Updated to NodeBB v4.3 + New Features

How to use the QCommandLineParser

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 591 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I am trying to create a file tree of tableview using QCommandLineParser.

    I've seen the example provided by Qt, but I'm new to QCommandLineParser and I'm not familiar with it, so I'm asking. How can I see only the folder on the D drive instead of the whole directory?

    • Example
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QCoreApplication::setApplicationVersion(QT_VERSION_STR);
        QCommandLineParser parser;
        parser.setApplicationDescription("Qt Dir View Example");
        parser.addHelpOption();
        parser.addVersionOption();
        QCommandLineOption dontUseCustomDirectoryIconsOption("c", "Set QFileSystemModel::DontUseCustomDirectoryIcons");
        parser.addOption(dontUseCustomDirectoryIconsOption);
        QCommandLineOption dontWatchOption("w", "Set QFileSystemModel::DontWatch");
        parser.addOption(dontWatchOption);
        parser.addPositionalArgument("directory", "The directory to start in.");
        parser.process(app);
        const QString rootPath = parser.positionalArguments().isEmpty()
            ? QString() : parser.positionalArguments().first();
    
        QFileSystemModel model;
        model.setRootPath("");
        if (parser.isSet(dontUseCustomDirectoryIconsOption))
            model.setOption(QFileSystemModel::DontUseCustomDirectoryIcons);
        if (parser.isSet(dontWatchOption))
            model.setOption(QFileSystemModel::DontWatchForChanges);
        QTreeView tree;
        tree.setModel(&model);
        if (!rootPath.isEmpty()) {
            const QModelIndex rootIndex = model.index(QDir::cleanPath(rootPath));
            if (rootIndex.isValid())
                tree.setRootIndex(rootIndex);
        }
    
        // Demonstrating look and feel features
        tree.setAnimated(false);
        tree.setIndentation(40);        // 들여쓰기 위치
        tree.setSortingEnabled(true);   // 정렬 여부
        const QSize availableSize = tree.screen()->availableGeometry().size();
        tree.resize(availableSize / 2);
        tree.setColumnWidth(0, tree.width() / 3);
    
        // Make it flickable on touchscreens
        QScroller::grabGesture(&tree, QScroller::TouchGesture);
    
        tree.setWindowTitle(QObject::tr("Dir View"));
        tree.show();
    
        return app.exec();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • I Offline
      I Offline
      IknowQT
      wrote on last edited by
      #7
      foreach(QFileInfo info, QDir::drives())
      	{
      		QEventLoop loop;
      		QObject::connect(m_pDirModel, &QFileSystemModel::directoryLoaded, &loop, &QEventLoop::quit);
      	
      		qDebug() << "fileName : " << info.fileName();
      		qDebug() << "absoluteFilePath : " << info.absoluteFilePath();
      		qDebug() << "filePath : " << info.filePath();
      		qDebug() << "absoluteDir : " << info.absoluteDir();
      		qDebug() << "absolutePath : " << info.absolutePath();
      	
      		// 읽을 디렉토리 설정
      		m_pDirModel->setRootPath(info.filePath());
      		//m_pDirModel->setRootPath("D:/yhJeong_Doc/");
      		loop.exec();
      	}
      
      
      this->ui.treeView->setModel(m_pDirModel);
      	this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest"));
      

      this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest"));

      this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest")); It is solved by setting the path.

      1 Reply Last reply
      0
      • I IknowQT

        I am trying to create a file tree of tableview using QCommandLineParser.

        I've seen the example provided by Qt, but I'm new to QCommandLineParser and I'm not familiar with it, so I'm asking. How can I see only the folder on the D drive instead of the whole directory?

        • Example
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            QCoreApplication::setApplicationVersion(QT_VERSION_STR);
            QCommandLineParser parser;
            parser.setApplicationDescription("Qt Dir View Example");
            parser.addHelpOption();
            parser.addVersionOption();
            QCommandLineOption dontUseCustomDirectoryIconsOption("c", "Set QFileSystemModel::DontUseCustomDirectoryIcons");
            parser.addOption(dontUseCustomDirectoryIconsOption);
            QCommandLineOption dontWatchOption("w", "Set QFileSystemModel::DontWatch");
            parser.addOption(dontWatchOption);
            parser.addPositionalArgument("directory", "The directory to start in.");
            parser.process(app);
            const QString rootPath = parser.positionalArguments().isEmpty()
                ? QString() : parser.positionalArguments().first();
        
            QFileSystemModel model;
            model.setRootPath("");
            if (parser.isSet(dontUseCustomDirectoryIconsOption))
                model.setOption(QFileSystemModel::DontUseCustomDirectoryIcons);
            if (parser.isSet(dontWatchOption))
                model.setOption(QFileSystemModel::DontWatchForChanges);
            QTreeView tree;
            tree.setModel(&model);
            if (!rootPath.isEmpty()) {
                const QModelIndex rootIndex = model.index(QDir::cleanPath(rootPath));
                if (rootIndex.isValid())
                    tree.setRootIndex(rootIndex);
            }
        
            // Demonstrating look and feel features
            tree.setAnimated(false);
            tree.setIndentation(40);        // 들여쓰기 위치
            tree.setSortingEnabled(true);   // 정렬 여부
            const QSize availableSize = tree.screen()->availableGeometry().size();
            tree.resize(availableSize / 2);
            tree.setColumnWidth(0, tree.width() / 3);
        
            // Make it flickable on touchscreens
            QScroller::grabGesture(&tree, QScroller::TouchGesture);
        
            tree.setWindowTitle(QObject::tr("Dir View"));
            tree.show();
        
            return app.exec();
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @IknowQT said in How to use the QCommandLineParser:

        How can I see only the folder on the D drive instead of the whole directory?

        What do you mean and how is this related to QCommandLineParser?
        What is "whole directory"? Do you mean all folders on drive D?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply
        0
        • jsulmJ jsulm

          @IknowQT said in How to use the QCommandLineParser:

          How can I see only the folder on the D drive instead of the whole directory?

          What do you mean and how is this related to QCommandLineParser?
          What is "whole directory"? Do you mean all folders on drive D?

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @jsulm

                  m_pDirModel = new usrFileSystemModel(this);
          	m_pDirModel->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot);
          
          	foreach(QFileInfo info, QDir::drives())
          	{
          		QEventLoop loop;
          		QObject::connect(m_pDirModel, &QFileSystemModel::directoryLoaded, &loop, &QEventLoop::quit);
          
          		// 읽을 디렉토리 설정
          		m_pDirModel->setRootPath(info.filePath());
          		loop.exec();
          	}
          
          
          	this->ui.treeView->setModel(m_pDirModel);
          
          	// [두번째 이후 모든 컬럼 숨김]
          	for (int i = m_pDirModel->columnCount() - 1; i > 0; --i)
          		this->ui.treeView->setColumnHidden(i, true);
          
          
          	// [펼치기/감추기 아이콘 숨김]
          	this->ui.treeView->setRootIsDecorated(false);
          
          	// [헤더 없애기]
          	this->ui.treeView->setHeaderHidden(true);
          
          	// [폰트 설정]
          	this->ui.treeView->setFont(cGlobalParam::gGetUseMultipleFont(13));
          
          	// [행 사이즈 설정] 
          	usrItemDelegate* itemdelegate = new usrItemDelegate(this);
          	itemdelegate->SetHeight(60);
          	this->ui.treeView->setItemDelegate(itemdelegate);
          

          Currently, it shows the entire driver.
          What I want is to show only the local folder.

          For example, when there is a path called D:/Test/Source, I want to display only the contents in the Source folder. In a folder called Source, there may be folders and may contain files.

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #4

            @IknowQT said in How to use the QCommandLineParser:

            What I want is to show only the local folder.

            The documentation has even an example on how to do so: https://doc.qt.io/qt-5/qfilesystemmodel.html#example-usage

            Don't know how this is related the QCommandLineParser though.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            I 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @IknowQT said in How to use the QCommandLineParser:

              What I want is to show only the local folder.

              The documentation has even an example on how to do so: https://doc.qt.io/qt-5/qfilesystemmodel.html#example-usage

              Don't know how this is related the QCommandLineParser though.

              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              @Christian-Ehrlicher

              m_pDirModel->setRootPath("D:/yhJeong_Doc/");
              

              I did this, but the result is the same.

              JonBJ 1 Reply Last reply
              0
              • I IknowQT

                @Christian-Ehrlicher

                m_pDirModel->setRootPath("D:/yhJeong_Doc/");
                

                I did this, but the result is the same.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #6

                @IknowQT said in How to use the QCommandLineParser:

                m_pDirModel->setRootPath("D:/yhJeong_Doc/");

                Since you say earlier

                For example, when there is a path called D:/Test/Source, I want to display only the contents in the Source folder

                why do you set the root path to D:/yhJeong_Doc/? What do you expect that to do/show?

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  IknowQT
                  wrote on last edited by
                  #7
                  foreach(QFileInfo info, QDir::drives())
                  	{
                  		QEventLoop loop;
                  		QObject::connect(m_pDirModel, &QFileSystemModel::directoryLoaded, &loop, &QEventLoop::quit);
                  	
                  		qDebug() << "fileName : " << info.fileName();
                  		qDebug() << "absoluteFilePath : " << info.absoluteFilePath();
                  		qDebug() << "filePath : " << info.filePath();
                  		qDebug() << "absoluteDir : " << info.absoluteDir();
                  		qDebug() << "absolutePath : " << info.absolutePath();
                  	
                  		// 읽을 디렉토리 설정
                  		m_pDirModel->setRootPath(info.filePath());
                  		//m_pDirModel->setRootPath("D:/yhJeong_Doc/");
                  		loop.exec();
                  	}
                  
                  
                  this->ui.treeView->setModel(m_pDirModel);
                  	this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest"));
                  

                  this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest"));

                  this->ui.treeView->setRootIndex(m_pDirModel->index("D:/rootTest")); It is solved by setting the path.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved