Problem with print out all file names out of one folder



  • 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.


  • Moderators

    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 ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.