Icônes non visibles dans un QListView associé à un QFileSystemModel



  • Bonjour à tous,

    Utilisant le système wmi de windows, j'ai du implémenter ma propre fenêtre d'explorateur.
    Pour cela j'ai associé un QListView et un QFileSystemModel ( a a coté de cela un QTreeView avec un autre QFileSystemModel qui ont le même soucis) . Cela marche globalement bien. J'ai juste un petit soucis, selon le dossier où je suis certaines icônes ne sont pas visible.

    Pour vous donner un exemple sur le Bureau ( et c'est sous dossier) l'icone symbolisant les dossiers n’apparaît pas , de plus à certain moment (completement aléatoire) même celles des fichiers n’apparaîssent pas.
    Pour autant pour vous donner un autre exemple dans le dossier "Mes documents" toute les icônes sont visibles.

    alt text

    alt text

    De plus dès que ma souris bouge dans la QListView le texte suivant apparaît dans la sortie d'application :

    onecore\com\combase\objact\objact.cxx(834)\combase.dll!00007FFB69BF80F3: (caller: 00007FFB69BF73B6) ReturnHr(1966) tid(47f8) 800401F0 CoInitialize n’a pas été appelé.
    

    Voici comment est coder mon implémentation :

    #ifndef DIALOGFILE_H
    #define DIALOGFILE_H
    
    #include <QDialog>
    
    #include <QFileSystemModel>
    
    namespace Ui {
    class DialogFile;
    }
    
    enum modefileDialog{OPEN_MODE, OPEN_MULTIPLE_MODE, MODE, SAVE_MODE, SELECT_DIRECTORY};
    
    class DialogFile : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit DialogFile(QWidget *parent = nullptr);
        ~DialogFile();
    
        void setCaption(QString caption);
        void setNamesFilter(QStringList filter);
        void setNamesFilter(QStringList filterName, QStringList filter);
        void setCurrentDirectory(QString currentDirectory);
        void setMode(modefileDialog mode);
        void setDefaultExtention(QString ext){extention = ext;}
    
        QFileInfo getSelectedFile();
        QFileInfoList getSelectedFiles();
        QDir        getSelectedDirectory(){ return directory;}
    
    
    private slots:
        void on_treeView_Lecteur_clicked(const QModelIndex &index);
    
        void on_listView_Fichier_clicked(const QModelIndex &index);
    
        void on_listView_Fichier_doubleClicked(const QModelIndex &index);
        void on_lineEdit_Filename_textChanged(const QString &arg1);
        void on_pushButton_Ok_clicked();
    
        void on_pushButton_Cancel_clicked();
    
     
        void on_comboBox_currentIndexChanged(int index);
    
    protected :
        virtual void showEvent(QShowEvent * event);
        void ChangeCurrentFilter(int indice);
    
    
        virtual void changeEvent(QEvent *event);
    
    
    
    private:
        Ui::DialogFile *ui;
        QFileSystemModel *dirModel;
        QFileSystemModel *fileModel;
    
        modefileDialog mode;
        QFileInfoList file;
        QDir directory;
        QString extention;
        QStringList filtres;
    };
    
    #endif // DIALOGFILE_H
    
    
    #include "dialogfile.h"
    #include "dialogfile.h"
    #include "ui_dialogfile.h"
    
    #include <QtDebug>
    #include <QKeyEvent>
    #include <QInputDialog>
    #include <QFileIconProvider>
    #include <QStandardPaths>
    #include <QModelIndexList>
    
    
    
    DialogFile::DialogFile(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogFile)
    {
        ui->setupUi(this);
        // Creates our new model and populate
    #if _WIN32
         QString mPath = "C:/";
    #elif __linux__
        QString mPath = "/";
    #endif
        dirModel = new QFileSystemModel(this);
    
    
       // Set filter
       dirModel->setFilter(QDir::NoDotAndDotDot |
                           QDir::AllDirs);
    
       // QFileSystemModel requires root path
       dirModel->setRootPath(mPath);
    
       // Attach the model to the view
       ui->treeView_Lecteur->setModel(dirModel);
    
       for (int i = 1; i < dirModel->columnCount(); ++i)
           ui->treeView_Lecteur->hideColumn(i);
    
       // FILES
    
       fileModel = new QFileSystemModel(this);
    
       // Set filter
       fileModel->setFilter(QDir::NoDotAndDotDot |
                            QDir::Files | QDir::AllDirs);
    
       // QFileSystemModel requires root path
       fileModel->setRootPath(mPath);
    
       // Attach the file model to the view
        ui->listView_Fichier->setModel(fileModel);
        //ui->listView_Fichier->update();
    }
    
    DialogFile::~DialogFile()
    {
        delete ui;
    }
    
    void DialogFile::setCaption(QString caption)
    {
        this->setWindowTitle(caption);
    
    }
    
    void DialogFile::setNamesFilter(QStringList filter)
    {
    
        setNamesFilter(filter,filter);
    }
    
    void DialogFile::setNamesFilter(QStringList filterName, QStringList filter)
    {
    
        ui->comboBox->clear();
        ui->comboBox->addItems(filterName);
        ui->comboBox->setCurrentIndex(0);
        filtres = filter;
        ChangeCurrentFilter(0);
    
    }
    
    void DialogFile::setCurrentDirectory(QString currentDirectory)
    {
        ui->listView_Fichier->setRootIndex(fileModel->setRootPath(currentDirectory));
        ui->treeView_Lecteur->expand(dirModel->index(currentDirectory));
        ui->lineEdit_Path->setText(currentDirectory);
    
    }
    
    void DialogFile::setMode(modefileDialog p_mode)
    {
    
        mode = p_mode;
         bool multiple = false;
         if(mode == OPEN_MULTIPLE_MODE)
             multiple = true;
    
        if( mode == OPEN_MODE || mode == OPEN_MULTIPLE_MODE)
        {
            ui->pushButton_Ok->setText(tr("Ouvrir"));
        }
        else if ( mode == SAVE_MODE)
        {
            ui->pushButton_Ok->setText(tr("Sauvegarder"));
        }
        else if (mode == SELECT_DIRECTORY)
        {
            ui->pushButton_Ok->setText(tr("Selectionner"));
        }
    
        if ( multiple )
        {
            ui->listView_Fichier->setSelectionBehavior(QAbstractItemView::SelectRows);
            ui->listView_Fichier->setSelectionMode(QAbstractItemView::ExtendedSelection);
        }
        else
        {
            ui->listView_Fichier->setSelectionBehavior(QAbstractItemView::SelectItems);
            ui->listView_Fichier->setSelectionMode(QAbstractItemView::SingleSelection);
        }
    
    }
    
    QFileInfo DialogFile::getSelectedFile()
    {
        if ( (file.size() == 0) && (mode == SAVE_MODE))
        {
            if (  ui->lineEdit_Filename->text()!= "")
            {
                file.push_back(fileModel->rootPath()+"\\"+ui->lineEdit_Filename->text());
            }
        }
        if ( ui->comboBox->currentIndex() < filtres.size() && file.size()!=0)
        {
            QStringList list = filtres[ui->comboBox->currentIndex()].split(" ");
            if ( !list.contains("*."+ file[0].suffix()))
                file[0].setFile(file[0].absoluteFilePath()+extention);
            return file[0];
        }
    
        return QFileInfo();
    
    }
    
    QFileInfoList DialogFile::getSelectedFiles()
    {
        if ( ui->comboBox->currentIndex() < filtres.size() && file.size()!=0)
        {
            QStringList list = filtres[ui->comboBox->currentIndex()].split(" ");
    
            for (int i = 0; i< file.size();++i)
            {
                if ( !list.contains("*."+ file[i].suffix()))
                    file[0].setFile(file[i].absoluteFilePath()+extention);
            }
        }
       return file;
    }
    
    
    void DialogFile::on_treeView_Lecteur_clicked(const QModelIndex &index)
    {
        setCurrentDirectory(dirModel->fileInfo(index).absoluteFilePath());
    }
    
    void DialogFile::on_listView_Fichier_clicked(const QModelIndex &index)
    {
        if (mode == SELECT_DIRECTORY)
        {
            if ( fileModel->isDir(index))
            {
                directory.setPath(fileModel->filePath(index));
                ui->lineEdit_Filename->setText(directory.dirName());
            }
    
        }
        else if ( mode == SAVE_MODE || mode == OPEN_MODE)
        {
            file.clear();
            if (!fileModel->isDir(index) )
            {
                file.push_back(fileModel->filePath(index));
                ui->lineEdit_Filename->setText( file.back().fileName() );
    
            }
        }
        else if ( mode == OPEN_MULTIPLE_MODE)
        {
            file.clear();
            QString filesname;
            QModelIndexList list = ui->listView_Fichier->selectionModel()->selectedRows();
            for ( int i  = 0; i < list.size(); ++i)
            {
                if (!fileModel->isDir(index))
                {
                    file.push_back(fileModel->filePath(list[i]));
                    filesname += "\"" + file.back().fileName() + "\" ";
                }
            }
            ui->lineEdit_Filename->setText(filesname);
        }
    
    }
    
    void DialogFile::on_listView_Fichier_doubleClicked(const QModelIndex &index)
    {
        if (fileModel->isDir(index))
        {
            setCurrentDirectory(fileModel->fileInfo(index).absoluteFilePath());
        }
        else
        {
            file.clear();
            file.push_back(fileModel->filePath(index));
            done(QDialog::Accepted);
        }
    }
    
    
    
    
    
    
    
    void DialogFile::on_lineEdit_Filename_textChanged(const QString &arg1)
    {
    
        if (arg1 == "")
            ui->pushButton_Ok->setEnabled(false);
        else if ( mode == OPEN_MODE && (file.size()==0))
            ui->pushButton_Ok->setEnabled(false);
        else if ( mode == OPEN_MODE && !QFile::exists(file[0].absoluteFilePath()))
            ui->pushButton_Ok->setEnabled(false);
        else
            ui->pushButton_Ok->setEnabled(true);
    }
    
    void DialogFile::showEvent(QShowEvent *event)
    {
        ui->lineEdit_Filename->setText("");
    }
    
    void DialogFile::ChangeCurrentFilter(int indice)
    {
        if ( indice < filtres.size())
        {
            QString FiltreaAppliquer = filtres[indice];
            QStringList lesFiltre =  FiltreaAppliquer.split(" ");
            fileModel->setNameFilters(lesFiltre);
            fileModel->setNameFilterDisables(false);
        }
    }
    
    
    
    
    
    void DialogFile::changeEvent(QEvent *event)
    {
        if (event->type() == QEvent::LanguageChange)
        {
           ui->retranslateUi(this);
        }
        QDialog::changeEvent(event);
    }
    
    //**************************** Validation *****************************
    
    void DialogFile::on_pushButton_Ok_clicked()
    {
            done(QDialog::Accepted);
    }
    
    void DialogFile::on_pushButton_Cancel_clicked()
    {
        done( QDialog::Rejected);
    }
    
    
    
    void DialogFile::on_comboBox_currentIndexChanged(int index)
    {
        ChangeCurrentFilter(index);
    }
    
    


  • Salut,

    Je veux bien tester mais il manque le fichier de l'interface:
    dialogfile.ui



  • Bonjour,
    voici le fichier en question :

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>DialogFile</class>
     <widget class="QDialog" name="DialogFile">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>764</width>
        <height>518</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Dialog</string>
      </property>
      <property name="styleSheet">
       <string notr="true">QPushButton
    {
    padding: 5px 10px 5px 10px;
    }
    QListView, QTreeView, QLineEdit, QComboBox
    {
    	color : black;
    	background-color: white;
    }</string>
      </property>
      <layout class="QGridLayout" name="gridLayout_2">
       <item row="1" column="0" colspan="3">
        <widget class="QWidget" name="widget" native="true">
         <property name="styleSheet">
          <string notr="true"/>
         </property>
         <layout class="QHBoxLayout" name="horizontalLayout_2">
          <property name="topMargin">
           <number>0</number>
          </property>
          <property name="bottomMargin">
           <number>0</number>
          </property>
          <item>
           <spacer name="horizontalSpacer">
            <property name="orientation">
             <enum>Qt::Horizontal</enum>
            </property>
            <property name="sizeType">
             <enum>QSizePolicy::Expanding</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>40</width>
              <height>20</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_NouveauDossier">
            <property name="text">
             <string>Nouveau Dossier</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
       <item row="3" column="0" colspan="3">
        <layout class="QGridLayout" name="gridLayout">
         <item row="0" column="0">
          <widget class="QLabel" name="label">
           <property name="text">
            <string>Nom du fichier : </string>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QLineEdit" name="lineEdit_Filename"/>
         </item>
         <item row="1" column="1">
          <spacer name="horizontalSpacer_2">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeType">
            <enum>QSizePolicy::Expanding</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item row="1" column="2">
          <widget class="QPushButton" name="pushButton_Ok">
           <property name="text">
            <string>OK</string>
           </property>
          </widget>
         </item>
         <item row="1" column="3">
          <widget class="QPushButton" name="pushButton_Cancel">
           <property name="text">
            <string>Annuler</string>
           </property>
          </widget>
         </item>
         <item row="0" column="2" colspan="2">
          <widget class="QComboBox" name="comboBox"/>
         </item>
        </layout>
       </item>
       <item row="2" column="0">
        <widget class="QWidget" name="widget_2" native="true">
         <property name="styleSheet">
          <string notr="true">QPushButton
    {
    	text-align: left;
    }</string>
         </property>
         <layout class="QVBoxLayout" name="verticalLayout_2">
          <property name="leftMargin">
           <number>0</number>
          </property>
          <property name="topMargin">
           <number>0</number>
          </property>
          <property name="rightMargin">
           <number>0</number>
          </property>
          <property name="bottomMargin">
           <number>0</number>
          </property>
          <item>
           <widget class="QPushButton" name="pushButton_Bureau">
            <property name="text">
             <string>Desktop</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_Utilisateur">
            <property name="text">
             <string>Home</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer_2">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_MesDocuments">
            <property name="text">
             <string>Document</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer_3">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_Images">
            <property name="text">
             <string>Images</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer_4">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_Musique">
            <property name="text">
             <string>Musique</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer_5">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_Telechargement">
            <property name="text">
             <string>Telechargement</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <spacer name="verticalSpacer_6">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
            </property>
            <property name="sizeHint" stdset="0">
             <size>
              <width>20</width>
              <height>40</height>
             </size>
            </property>
           </spacer>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_Video">
            <property name="text">
             <string>Video</string>
            </property>
            <property name="iconSize">
             <size>
              <width>35</width>
              <height>35</height>
             </size>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
       <item row="2" column="1" colspan="2">
        <widget class="QWidget" name="widget_3" native="true">
         <layout class="QHBoxLayout" name="horizontalLayout">
          <property name="leftMargin">
           <number>3</number>
          </property>
          <property name="topMargin">
           <number>0</number>
          </property>
          <property name="rightMargin">
           <number>0</number>
          </property>
          <property name="bottomMargin">
           <number>0</number>
          </property>
          <item>
           <widget class="QSplitter" name="splitter">
            <property name="orientation">
             <enum>Qt::Horizontal</enum>
            </property>
            <widget class="QTreeView" name="treeView_Lecteur">
             <property name="maximumSize">
              <size>
               <width>200</width>
               <height>16777215</height>
              </size>
             </property>
             <attribute name="headerVisible">
              <bool>false</bool>
             </attribute>
            </widget>
            <widget class="ListViewFileDialog" name="listView_Fichier">
             <property name="selectionMode">
              <enum>QAbstractItemView::MultiSelection</enum>
             </property>
             <property name="selectionRectVisible">
              <bool>false</bool>
             </property>
            </widget>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
       <item row="0" column="0">
        <widget class="QPushButton" name="pushButton_DossierParent">
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>0</height>
          </size>
         </property>
         <property name="text">
          <string>Dossier Parent</string>
         </property>
        </widget>
       </item>
       <item row="0" column="1">
        <widget class="QLineEdit" name="lineEdit_Path">
         <property name="readOnly">
          <bool>true</bool>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
     <customwidgets>
      <customwidget>
       <class>ListViewFileDialog</class>
       <extends>QListView</extends>
       <header>listviewfiledialog.h</header>
      </customwidget>
     </customwidgets>
     <resources/>
     <connections/>
    </ui>
    
    


  • Il manque encore un fichier:

    ./ui_dialogfile.h:29:10: fatal error: 'listviewfiledialog.h' file not found
    #include "listviewfiledialog.h"
    


  • Le .h:

    #ifndef LISTVIEWFILEDIALOG_H
    #define LISTVIEWFILEDIALOG_H
    
    #include <QObject>
    #include <QListView>
    
    class ListViewFileDialog : public QListView
    {
        Q_OBJECT
    public:
        ListViewFileDialog(QWidget* parent = nullptr);
    
    public slots:
        void mousePressEvent(QMouseEvent *event);
    
    signals:
        void DossierParent();
    };
    
    #endif // LISTVIEWFILEDIALOG_H
    

    et le .cpp

    #include "listviewfiledialog.h"
    
    #include <QMouseEvent>
    
    ListViewFileDialog::ListViewFileDialog(QWidget* parent):
        QListView(parent)
    {
    
    }
    
    void ListViewFileDialog::mousePressEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::XButton1)
        {
            emit DossierParent();
        }
        else
            QListView::mousePressEvent(event);
    }
    
    


  • J'ai fait des tests sous MacOS et Linux 18.04 et tout s'affiche correctement.
    Bonne nouvelle, ton code est donc bon.
    Il semble donc qu'il s'agisse d'un bug spécifique à Windows (malheuresement je ne peux faire de test sur Windows)

    Tu pourrais essayer de voir dans la base des bugs de Qt si quelque chose ressemble à ce que tu décris.
    Bonne chance.


Log in to reply