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. Problem with print out all file names out of one folder
Qt 6.11 is out! See what's new in the release blog

Problem with print out all file names out of one folder

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    asdfghjk
    wrote on last edited by
    #1

    Hi,
    I want to write a program who read in all file names out of one folder and after this print out files of a special typ(mp3´s).

    my current code:
    @void MainWindow::on_Weiter_clicked()
    {

    QWidget newWindow;

    newWindow.resize(600,500);

    std::cout <<"new window muesste erstellt sein"<<std::endl;//Überprüfung ab wann es nichtmehr funktioniert

    QVBoxLayout *vbl = new QVBoxLayout(&newWindow);

    QDir directory (sPath);

    QStringList Filter;
    Filter<< "*.mp3";

    QStringList Liste = directory.entryList(Filter);

    QStringList::const_iterator titel;

    std::cout <<"Liste im string"<<std::endl;//wieder überprüfung ab wann es nichtmehr funktioniert

    for(titel=Liste.constBegin(); titel!=Liste.constEnd(); ++titel)
    {
    std::cout <<(*titel).toLatin1().constData()<<std::endl;// das wird nichtmehr ausgegeben

    int z=0;
    
    QLabel label ((*titel).toLatin1().constData(),&newWindow);
    
     vbl->addWidget(&label);
    
     newWindow.show();
    
     z++;
    
     std::cout <<"ende der for schleife"<<std::endl;
    

    }
    }@

    The "int z" should be the beginning of dynamic label objects (I don't know how many files I have to display) but I don't know how to make them.
    My biggest problem is that the "newWindow" is created but not displayed(it is a problem with the "for" but I don't know what I have to change)
    Until the " std::cout <<"Liste im string"<<std::endl; " the program print out my check text´s but every check text in the "for" isn't printed out. The "sPath" is a QString what gets the Path from a file system model.
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QString sPath ="C:\";
    dirmodel = new QFileSystemModel(this);
    dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    dirmodel ->setRootPath(sPath);
    ui->treeView->setModel(dirmodel);
    
    
    std::cout <<"Window erstellen tut"<<std::endl;//zur Überprüfung
    

    }

    void MainWindow::on_treeView_clicked(const QModelIndex &index)
    {
    QString sPath=dirmodel->fileInfo(index).absoluteFilePath();
    }
    @

    Can someone help me?

    P.s. Sorry for my bad English.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      everytime you assign something to sPath you assign it to a scoped variable. This means it is only available in the method where you do it and gets deleted at the end of the method automatically.

      You need to assign the value to a member variable of the class. Did you already define sPath as an member of MainWindow? If so just leave out the QString infront of it.

      It's good practice to name your member variables accordingly to their scope. E.g.:
      @
      class MainWindow {
      ....
      protected:
      QString m_sPath;
      }

      MainWindow::MainWindow()
      {
      ...
      m_sPath ="C:/";
      ...
      }

      void MainWindow::on_Weiter_clicked()
      {
      ...
      QDir directory(m_sPath);
      ...
      }

      void MainWindow::on_treeView_clicked(const QModelIndex &index)
      {
      m_sPath=dirmodel->fileInfo(index).absoluteFilePath();
      }
      @

      This helps you prevent making the mistake in the future ;)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      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