Results 1 to 20 of 29

Thread: Having trouble clearing a QTreeWidget.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Having trouble clearing a QTreeWidget.

    Hello,

    I use Qt4.3.1 Open Source edition for Windows 32 bits (under Windows XP Pro SP2).

    My application has a small interface based upon a QTreeWidget (named "tree") that displays many QTreeWidgetItem *.
    When the user clic an item, the signal itemSelectionChanged() is emited. I've got a SLOT linked to this signal, so that I can analyse the item that has been clicked.
    So consider the following code as my SLOT block :




    With the following code : no problem.
    The QTreeWidget is correctly cleared.
    Qt Code:
    1. {
    2. tree->clear();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by Qt Assistant
    void QTreeWidget::clear () [slot]
    Clears the tree widget by removing all of its items and selections.
    Note: Since each item is removed from the tree widget before being deleted, the return value of QTreeWidgetItem::treeWidget() will be invalid when called from an item's destructor.



    With the following code : no problem.
    The QString contains the informations of the first column of the firts selected item on the QTreeWidget.
    Qt Code:
    1. {
    2. QString item_content = ((tree->selectedItems()).at(0))->text(0);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Qt Assistant
    QList<QTreeWidgetItem *> QTreeWidget::selectedItems () const
    Returns a list of all selected non-hidden items.





    With the following code : problem.
    The application crashes, like it generally happens with memory errors...
    Qt Code:
    1. {
    2. QString item_content = ((tree->selectedItems()).at(0))->text(0);
    3. tree->clear();
    4. }
    To copy to clipboard, switch view to plain text mode 




    I don't really understand the problem, and I can't find any solution.
    The API concerning QTreeWidget and QTreeWidgetItem manages lots of pointers, like the list returned by selectedItems(). Certainly should I get the selected items instead of pointers to the selected ones... But I can't achieve to do that.

    May you help me, please ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having trouble clearing a QTreeWidget.

    If the above slot is connected to a signal that emits a QTreeWidgetItem* parameter, you can't delete the object pointed by that parameter and that's what you do by calling clear() here. And make sure selectedItems() is not empty.

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    Hi Wysota !

    I've tested the selectedItems() returned list to be sure that it's not empty, but I didn't mentionned that before. Moreover the signal itemSelectionChanged() emited by the QTreeWidget doesn't use any parameters...


  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having trouble clearing a QTreeWidget.

    But the signal may be emited by another slot that takes the item as a parameter Either show us the backtrace or make a little test - instead of
    Qt Code:
    1. tree->clear()
    To copy to clipboard, switch view to plain text mode 
    call:
    Qt Code:
    1. QTimer::singleShot(0, tree, SLOT(clear()));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    Hi,

    It appears that the call of clear() throught the Qtimer makes the application crash too



    (I'm sorry for my bad English, I'm French and I do what I can to be understanded ^^)

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having trouble clearing a QTreeWidget.

    In that case I need you to provide more code. Not only the method bodies, but also their prototypes, connect statements and class constructor.

  7. #7
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    The header file :

    Qt Code:
    1. #ifndef interface_impl_h
    2. #define interface_impl_h
    3.  
    4. #include "interface.h"
    5. #include "Listeur.h"
    6. #include "donnee.h"
    7.  
    8. #include <QObject>
    9. #include <QMainWindow>
    10. #include <QtGui>
    11. #include <QUrl>
    12. #include <QDesktopServices>
    13. #include <QFileInfoList>
    14. #include <QFileDialog>
    15. #include <QComboBox>
    16. #include <QList>
    17. #include <QFile>
    18. #include <QDir>
    19. #include <QString>
    20. #include <QPixmap>
    21.  
    22. class interface_impl : public QMainWindow, public Ui::MainWindow_interface
    23. {
    24. Q_OBJECT
    25.  
    26. public:
    27. interface_impl(Listeur *appelant = 0, QWidget *parent = 0);
    28. void resizer_tableaux();
    29. void Enregistrer_config();
    30.  
    31. protected:
    32. void showEvent(QShowEvent *event);
    33. void closeEvent(QCloseEvent *event);
    34.  
    35. private slots:
    36. void SLOT_ajouter_repertoire_scan();
    37. void SLOT_supprimer_repertoire_scan();
    38. void SLOT_rechercher_repertoire_scan();
    39.  
    40. void SLOT_ajouter_repertoire_noscan();
    41. void SLOT_supprimer_repertoire_noscan();
    42. void SLOT_rechercher_repertoire_noscan();
    43.  
    44. void SLOT_actualiser_liste_films();
    45. void SLOT_lancer_le_film(QTreeWidgetItem *, int);
    46.  
    47. void SLOT_selection_changed();
    48. void SLOT_ouvrir_selection();
    49. void SLOT_lancer_selection();
    50.  
    51. void SLOT_rechercher_synopsis_jaquettes_ALL();
    52. void SLOT_actualiser_synopsis_jaquettes_AC();
    53. void SLOT_actualiser_synopsis_jaquettes_QLCS();
    54. void SLOT_actualiser_sysnopsis_jaquettes_VC();
    55. void SLOT_actualiser_jaquettes_MC();
    56.  
    57. private:
    58. void Masquer_checkBoxes();
    59. void Nettoyer_texte_checkBoxes();
    60. void MAJ_checkBoxes_racines();
    61. void MAJ_listes_repertoires();
    62. void Charger_config();
    63. void MAJ_checkBoxes_racines_selon_config(QString, QString); // Texte, valeur activation
    64.  
    65. QString get_filename_selection();
    66. QString get_filepath_selection();
    67.  
    68. Listeur *Listeur_appelant;
    69. int NB_ligne_tableau_recherche_repertoire;
    70.  
    71. QList<QTreeWidgetItem *> liste_selection;
    72. };
    73.  
    74. #endif // interface_impl_h
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    The CPP file :

    Qt Code:
    1. #include <fstream>
    2. #include <iostream>
    3. using namespace std;
    4.  
    5. #include "interface_impl.h"
    6.  
    7. interface_impl::interface_impl(Listeur *appelant, QWidget *parent) : QMainWindow(parent)
    8. {
    9. cout << "interface_impl" << endl;
    10. setupUi(this);
    11.  
    12. Listeur_appelant = appelant;
    13.  
    14. Masquer_checkBoxes();
    15. MAJ_checkBoxes_racines();
    16.  
    17. connect(bouton_scan_plus_rep, SIGNAL(clicked()), this, SLOT(SLOT_ajouter_repertoire_scan()));
    18. connect(bouton_scan_moins_rep, SIGNAL(clicked()), this, SLOT(SLOT_supprimer_repertoire_scan()));
    19. connect(bouton_noscan_plus_rep, SIGNAL(clicked()), this, SLOT(SLOT_ajouter_repertoire_noscan()));
    20. connect(bouton_noscan_moins_rep, SIGNAL(clicked()), this, SLOT(SLOT_supprimer_repertoire_noscan()));
    21. connect(bouton_lister, SIGNAL(clicked()), this, SLOT(SLOT_actualiser_liste_films()));
    22.  
    23. connect(bouton_rechercher, SIGNAL(clicked()), this, SLOT(SLOT_rechercher_synopsis_jaquettes_ALL()));
    24. //connect(bouton_AC, SIGNAL(clicked()), this, SLOT(SLOT_actualiser_synopsis_jaquettes_AC()));
    25. //connect(bouton_QLCS, SIGNAL(clicked()), this, SLOT(SLOT_actualiser_synopsis_jaquettes_QLCS()));
    26. //connect(bouton_VC, SIGNAL(clicked()), this, SLOT(SLOT_actualiser_sysnopsis_jaquettes_VC()));
    27. //connect(bouton_MC, SIGNAL(clicked()), this, SLOT(SLOT_actualiser_jaquettes_MC()));
    28.  
    29. connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(SLOT_lancer_le_film(QTreeWidgetItem*, int)));
    30.  
    31. connect(tree, SIGNAL(itemSelectionChanged()), this, SLOT(SLOT_selection_changed()));
    32. connect(bouton_ouvrir, SIGNAL(clicked()), this, SLOT(SLOT_ouvrir_selection()));
    33. connect(bouton_lancer, SIGNAL(clicked()), this, SLOT(SLOT_lancer_selection()));
    34.  
    35. Charger_config();
    36. }
    37.  
    38. void interface_impl::SLOT_rechercher_synopsis_jaquettes_ALL()
    39. {
    40. Listeur_appelant->Rechercher_jaquettes_synopsis();
    41. }
    42.  
    43. void interface_impl::SLOT_actualiser_synopsis_jaquettes_AC()
    44. {
    45. Listeur_appelant->Utiliser_HTTP_AC();
    46. }
    47.  
    48. void interface_impl::SLOT_actualiser_synopsis_jaquettes_QLCS()
    49. {
    50. Listeur_appelant->Utiliser_HTTP_QLCS();
    51. }
    52.  
    53. void interface_impl::SLOT_actualiser_sysnopsis_jaquettes_VC()
    54. {
    55. Listeur_appelant->Utiliser_HTTP_VC();
    56. }
    57.  
    58. void interface_impl::SLOT_actualiser_jaquettes_MC()
    59. {
    60. Listeur_appelant->Utiliser_HTTP_MC();
    61. }
    62.  
    63. void interface_impl::SLOT_selection_changed()
    64. {
    65. cout << "\n\nSLOT_selection_changed" << endl;
    66.  
    67. liste_selection.clear();
    68. //liste_selection = tree->selectedItems();
    69.  
    70. // ----------------------
    71.  
    72. // QTreeWidgetItem *item;
    73. QString item_name;
    74. // QString item_path;
    75. // QDir mon_dossier_cible;
    76. // QFile mon_fichier_cible;
    77. // QUrl mon_url;
    78. // bool isDir;
    79. // bool isFile;
    80.  
    81. // item = liste_selection.at(0);
    82.  
    83. // item_name = item->text(0);
    84. // item_path = item->text(1);
    85.  
    86. item_name = ((tree->selectedItems()).at(0))->text(0);
    87.  
    88. tree->clear();
    89. //QTimer::singleShot(0, tree, SLOT(clear()));
    90.  
    91.  
    92. // mon_dossier_cible.setPath(item_path);
    93. // mon_fichier_cible.setFileName(item_path);
    94. // mon_url = QUrl::fromLocalFile(item_path);
    95. // isDir = mon_dossier_cible.exists();
    96. // isFile = mon_fichier_cible.exists();
    97. //
    98. // // L'objet sélectionné n'est pas un dossier
    99. // if (isDir == false)
    100. // {
    101. // // Par contre c'est bien un fichier, donc un film
    102. // if (isFile == true)
    103. // {
    104. // // On reconstruit le chemin d'accès au synopsis
    105. // QString acces_synopsys = "./Synopsis/";
    106. // acces_synopsys.append(item_name);
    107. // acces_synopsys.append(".html");
    108. //
    109. // // On crée l'URL qui sera utilisée par le QTextBroxser de synopsis
    110. // QUrl html_url;
    111. // html_url = QUrl::fromLocalFile(acces_synopsys);
    112. //
    113. // // On met à jour le QTextBrowser du synopsis
    114. // TB_Synopsis->setSource(html_url);
    115. //
    116. // // On reconstruit le nom complet de la jaquette (avec extension)
    117. // QString item_name_stored = "";
    118. // QDir dossier_synopsis("Jaquettes");
    119. // QFileInfoList synopsis_fileInfoList = dossier_synopsis.entryInfoList();
    120. // for(int i = 0; i < synopsis_fileInfoList.size(); ++i)
    121. // {
    122. // if(synopsis_fileInfoList.at(i).isFile()) // On peut avoir des DIR
    123. // {
    124. // if (synopsis_fileInfoList.at(i).baseName() == item_name) // Nom sans extension
    125. // {
    126. // item_name_stored = synopsis_fileInfoList.at(i).fileName(); // Nom avec extension
    127. // i = synopsis_fileInfoList.size(); // Fin de parcours
    128. // }
    129. // }
    130. // }
    131. //
    132. // if (item_name_stored == "") // La recherche n'a pas aboutit, ne devrait pas survenir
    133. // {
    134. // item_name_stored .append(item_name);
    135. // item_name_stored .append(".jpg");
    136. // }
    137. //
    138. // // On reconstruit le chemin d'accès à la jaquette
    139. // QString acces_jaquette = "./Jaquettes/";
    140. // acces_jaquette.append(item_name_stored);
    141. //
    142. // cout << "Accès jaquette : " << acces_jaquette.toStdString() << endl;
    143. //
    144. // // Affichage de l'image
    145. // int hauteur_jaquette = (tree->height()) / 2; // 50% de la hauteur de l'interface
    146. // QPixmap jaquette_pixmap;
    147. // jaquette_pixmap.load(acces_jaquette); // Chargement de l'image dans le QPixmap
    148. // jaquette_pixmap = jaquette_pixmap.scaledToHeight(hauteur_jaquette, Qt::SmoothTransformation); // Redimensionnement de l'image
    149. // label_jaquette->setPixmap(jaquette_pixmap); // Affichage du QPixmap (et donc de l'image) dans le QLabel de l'interface
    150. // }
    151. // }
    152. // else // L'objet sélectionné est un dossier
    153. // {
    154. // label_jaquette->clear();
    155. // TB_Synopsis->clear();
    156. // }
    157. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    Qt Code:
    1. QString interface_impl::get_filename_selection()
    2. {
    3. QString item_name;
    4.  
    5. item = liste_selection.at(0);
    6. item_name = item->text(0);
    7.  
    8. return item_name;
    9. }
    10.  
    11. QString interface_impl::get_filepath_selection()
    12. {
    13. QString item_path;
    14.  
    15. item = liste_selection.at(0);
    16. item_path = item->text(1);
    17.  
    18. return item_path;
    19. }
    20.  
    21. void interface_impl::SLOT_ouvrir_selection()
    22. {
    23. cout << "SLOT_ouvrir_dossier" << endl;
    24.  
    25. QString item_path;
    26. QDir mon_dossier_cible;
    27. QFile mon_fichier_cible;
    28. QUrl mon_url;
    29. bool isDir;
    30. bool isFile;
    31. QStringList liste_dossiers_ouverts;
    32.  
    33. for (int i=0; i<liste_selection.size(); i++)
    34. {
    35. item = liste_selection.at(i);
    36. item_path = item->text(1);
    37. mon_dossier_cible.setPath(item_path);
    38. mon_fichier_cible.setFileName(item_path);
    39. mon_url = QUrl::fromLocalFile(item_path);
    40. isDir = mon_dossier_cible.exists();
    41. isFile = mon_fichier_cible.exists();
    42.  
    43. cout << "\t Cible : \t" << (item->text(0)).toStdString() << endl;
    44. cout << "\t Acces : \t" << item_path.toStdString() << endl;
    45. cout << "\t isDir : \t" << isDir << endl;
    46. cout << "\t isFile : \t" << isFile << endl;
    47. cout << "\n" << endl;
    48.  
    49. // L'objet sélectionné est un dossier
    50. if (isDir == true)
    51. {
    52. if (liste_dossiers_ouverts.contains(item_path) == false) // Si ce dossier n'a pas encore été ouvert
    53. {
    54. liste_dossiers_ouverts.append(item_path);
    55. mon_url = QUrl::fromLocalFile(item_path);
    56. QDesktopServices::openUrl(mon_url);
    57. }
    58. }
    59.  
    60. // L'objet sélectionné n'est pas un dossier
    61. else
    62. {
    63. // Par contre c'est bien un fichier, donc un film
    64. if (isFile == true)
    65. {
    66. QStringList liste_item_path = item_path.split("/");
    67.  
    68. // Parcours de la liste sauf le dernier élément
    69. // Pour reconstruire le chemin d'accès au dossier parant
    70. QString item_path_to_parent = "";
    71. for (int j=0; j<liste_item_path.size()-1; j++)
    72. {
    73. item_path_to_parent.append(liste_item_path.at(j));
    74. item_path_to_parent.append("/");
    75. }
    76.  
    77. if (liste_dossiers_ouverts.contains(item_path_to_parent) == false) // Si ce dossier n'a pas encore été ouvert
    78. {
    79. liste_dossiers_ouverts.append(item_path_to_parent);
    80. mon_url = QUrl::fromLocalFile(item_path_to_parent);
    81. QDesktopServices::openUrl(mon_url);
    82. }
    83. }
    84. }
    85. }
    86. }
    87.  
    88. void interface_impl::SLOT_lancer_selection()
    89. {
    90. cout << "SLOT_lancer_selection" << endl;
    91.  
    92. QString item_path;
    93. QDir mon_dossier_cible;
    94. QFile mon_fichier_cible;
    95. QUrl mon_url;
    96. bool isDir;
    97. bool isFile;
    98. QStringList liste_films_lances;
    99.  
    100. for (int i=0; i<liste_selection.size(); i++)
    101. {
    102. item = liste_selection.at(i);
    103. item_path = item->text(1);
    104. mon_dossier_cible.setPath(item_path);
    105. mon_fichier_cible.setFileName(item_path);
    106. mon_url = QUrl::fromLocalFile(item_path);
    107. isDir = mon_dossier_cible.exists();
    108. isFile = mon_fichier_cible.exists();
    109.  
    110. cout << "\t Cible : \t" << (item->text(0)).toStdString() << endl;
    111. cout << "\t Acces : \t" << item_path.toStdString() << endl;
    112. cout << "\t isDir : \t" << isDir << endl;
    113. cout << "\t isFile : \t" << isFile << endl;
    114. cout << "\n" << endl;
    115.  
    116. // L'objet sélectionné est un dossier
    117. if (isDir == true)
    118. {
    119. // if (liste_films_lances.contains(item_path) == false) // Si ce film n'a pas encore été lancé
    120. // {
    121. // liste_films_lances.append(item_path);
    122. // mon_url = QUrl::fromLocalFile(item_path);
    123. // QDesktopServices::openUrl(mon_url);
    124. // }
    125. }
    126.  
    127. // L'objet sélectionné n'est pas un dossier
    128. else
    129. {
    130. // Par contre c'est bien un fichier, donc un film
    131. if (isFile == true)
    132. {
    133. if (liste_films_lances.contains(item_path) == false) // Si ce film n'a pas encore été lancé
    134. {
    135. liste_films_lances.append(item_path);
    136. mon_url = QUrl::fromLocalFile(item_path);
    137. QDesktopServices::openUrl(mon_url);
    138. }
    139. }
    140. }
    141. }
    142. }
    143.  
    144. void interface_impl::SLOT_lancer_le_film(QTreeWidgetItem *item, int column)
    145. {
    146. cout << "\n\n" << endl;
    147. cout << "SLOT_lancer_le_film" << endl;
    148. cout << "Film : \t\t" << (item->text(0)).toStdString() << endl;
    149. cout << "Acces : \t" << (item->text(1)).toStdString() << endl;
    150.  
    151. QUrl mon_url;
    152. mon_url = QUrl::fromLocalFile(item->text(1));
    153. QDesktopServices::openUrl(mon_url);
    154. }
    155.  
    156. void interface_impl::showEvent(QShowEvent *event)
    157. {
    158. cout << "showEvent" << endl;
    159.  
    160. // // Préparation de l'affichage taille "normale" (fenêtré)
    161. // int hauteur_ecran = (QApplication::desktop()->availableGeometry(0)).height();
    162. // int largeur_ecran = (QApplication::desktop()->availableGeometry(0)).width();
    163. // setGeometry(50, 50, largeur_ecran - 100, hauteur_ecran - 100);
    164. //
    165. // // Passage en plein écran
    166. showMaximized();
    167.  
    168. // Redimensionnement des colonnes de stableaux d'ajout / suppression de dossiers à scanner / ne pas scanner
    169. // int largeur_tableau = (tableau_scan->width()) - (((tableau_scan->width()) * 3) / 100);
    170. // int largeur_col_0 = (largeur_tableau * 10) / 100;
    171. // int largeur_col_1 = (largeur_tableau * 20) / 100;
    172. // int largeur_col_2 = largeur_tableau - (largeur_col_0 + largeur_col_1);
    173.  
    174. // tableau_scan->setColumnWidth(0, largeur_col_0);
    175. // tableau_scan->setColumnWidth(1, largeur_col_1);
    176. // tableau_scan->setColumnWidth(2, largeur_col_2);
    177. //
    178. // tableau_noscan->setColumnWidth(0, largeur_col_0);
    179. // tableau_noscan->setColumnWidth(1, largeur_col_1);
    180. // tableau_noscan->setColumnWidth(2, largeur_col_2);
    181.  
    182. // Masquage de la colonne des chemins d'accès
    183. tree->hideColumn(1);
    184. }
    185.  
    186. void interface_impl::resizer_tableaux()
    187. {
    188. tableau_scan->resizeColumnToContents(0);
    189. tableau_scan->resizeColumnToContents(1);
    190. tableau_scan->setColumnWidth(1, tableau_scan->columnWidth(1) + 5);
    191. // tableau_scan->setColumnWidth(2, tableau_scan->width() - tableau_scan->columnWidth(0) - tableau_scan->columnWidth(1));
    192.  
    193. tableau_noscan->resizeColumnToContents(0);
    194. tableau_noscan->resizeColumnToContents(1);
    195. tableau_noscan->setColumnWidth(1, tableau_noscan->columnWidth(1) + 5);//
    196. // tableau_noscan->setColumnWidth(2, tableau_noscan->width() - tableau_noscan->columnWidth(0) - tableau_noscan->columnWidth(1));
    197.  
    198. // cout << tableau_scan->width() << endl;
    199. // cout << tableau_scan->columnWidth(0) << endl;
    200. // cout << tableau_scan->columnWidth(1) << endl;
    201. // cout << tableau_scan->columnWidth(2) << endl;
    202. // cout << tableau_scan->columnWidth(3) << endl;
    203. // cout << tableau_scan->width() - 10 - tableau_scan->columnWidth(0) - tableau_scan->columnWidth(1) << endl;
    204.  
    205. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    Qt Code:
    1. void interface_impl::MAJ_listes_repertoires()
    2. {
    3. cout << "MAJ_listes_repertoires" << endl;
    4.  
    5. // Suppression du contenu des listes
    6. Listeur_appelant->vider_listes_repertoires();
    7.  
    8. // Variables temporaires
    9. QTableWidgetItem *QWI_element_courant;
    10. QString chemin_acces;
    11.  
    12. // Liste des répertoires à scanner
    13. for (int r = 0; r < tableau_scan->rowCount(); ++r)
    14. {
    15. // Récupération du chemin
    16. QWI_element_courant = tableau_scan->item(r, 0); // Checkbox
    17. if (QWI_element_courant->checkState() == Qt::Checked)
    18. {
    19. QWI_element_courant = tableau_scan->item(r, 2); // Chemin d'accès
    20. chemin_acces = "";
    21. chemin_acces = QWI_element_courant->text();
    22. if (chemin_acces != "")
    23. {
    24. // Ajout dans la liste
    25. Listeur_appelant->ajouter_repertoire_scan(chemin_acces);
    26. }
    27. }
    28. }
    29.  
    30. // Liste des répertoires à ne pas scanner
    31. for (int r = 0; r < tableau_noscan->rowCount(); ++r)
    32. {
    33. // Récupération du chemin
    34. QWI_element_courant = tableau_noscan->item(r, 0); // Checkbox
    35. if (QWI_element_courant->checkState() == Qt::Checked)
    36. {
    37. QWI_element_courant = tableau_noscan->item(r, 2); // Chemin d'accès
    38. chemin_acces = "";
    39. chemin_acces = QWI_element_courant->text();
    40. if (chemin_acces != "")
    41. {
    42. // Ajout dans la liste
    43. Listeur_appelant->ajouter_repertoire_noscan(chemin_acces);
    44. }
    45. }
    46. }
    47. }
    48.  
    49. void interface_impl::SLOT_ajouter_repertoire_scan()
    50. {
    51. cout << "SLOT_ajouter_repertoire_scan" << endl;
    52.  
    53. tableau_scan->setRowCount(tableau_scan->rowCount() + 1);
    54.  
    55. QTableWidgetItem *Item_2 = new QTableWidgetItem("");
    56.  
    57. QPushButton *bouton_rechercher = new QPushButton("Rechercher...");
    58. connect(bouton_rechercher, SIGNAL(clicked()), this, SLOT(SLOT_rechercher_repertoire_scan()));
    59.  
    60. Item_0->setCheckState(Qt::Unchecked);
    61. Item_0->setFlags(Item_0->flags() & (~(Qt::ItemIsEditable)));
    62. //Item_2->setFlags(Item_2->flags() & (Qt::ItemIsEditable));
    63.  
    64. tableau_scan->setItem(tableau_scan->rowCount()-1, 0, Item_0);
    65. tableau_scan->setCellWidget(tableau_scan->rowCount()-1, 1, bouton_rechercher);
    66. tableau_scan->setItem(tableau_scan->rowCount()-1, 2, Item_2);
    67. }
    68.  
    69. void interface_impl::SLOT_supprimer_repertoire_scan()
    70. {
    71. cout << "SLOT_supprimer_repertoire_scan" << endl;
    72.  
    73. // Recherche la ligne du tableau_scan sélectionnée
    74. QPushButton *QPB_element_courant;
    75. int row_number = -1;
    76. for (int r = 0; r < tableau_scan->rowCount(); ++r)
    77. {
    78. for (int c = 0; c < tableau_scan->columnCount(); ++c)
    79. {
    80. if (c == 0) // Item
    81. {
    82. if ((tableau_scan->item(r,c))->isSelected() == true)
    83. {
    84. row_number = r;
    85. }
    86. }
    87. else if (c == 1) // Boutons
    88. {
    89. QPB_element_courant = static_cast<QPushButton*>(tableau_scan->cellWidget(r, c));
    90. if (QPB_element_courant->hasFocus() == true)
    91. {
    92. row_number = r;
    93. }
    94. }
    95. else if (c == 2) // Item
    96. {
    97. if ((tableau_scan->item(r,c))->isSelected() == true)
    98. {
    99. row_number = r;
    100. }
    101. }
    102. }
    103. }
    104.  
    105. if (row_number == -1)
    106. {
    107. tableau_scan->removeRow(tableau_scan->rowCount()-1);
    108. }
    109. else
    110. {
    111. tableau_scan->removeRow(row_number);
    112. }
    113. }
    114.  
    115. void interface_impl::SLOT_rechercher_repertoire_scan()
    116. {
    117. cout << "SLOT_rechercher_repertoire_scan" << endl;
    118.  
    119. // Recherche la ligne du tableau_scan depuis laquelle le bouton a été cliqué
    120. QPushButton *QPB_element_courant;
    121. int row_number = -1;
    122. for (int r = 0; r < tableau_scan->rowCount(); ++r)
    123. {
    124. for (int c = 0; c < tableau_scan->columnCount(); ++c)
    125. {
    126. if (c == 0) // Item
    127. {
    128. if ((tableau_scan->item(r,c))->isSelected() == true)
    129. {
    130. row_number = r;
    131. }
    132. }
    133. else if (c == 1) // Boutons
    134. {
    135. QPB_element_courant = static_cast<QPushButton*>(tableau_scan->cellWidget(r, c));
    136. if (QPB_element_courant->hasFocus() == true)
    137. {
    138. row_number = r;
    139. }
    140. }
    141. else if (c == 2) // Item
    142. {
    143. if ((tableau_scan->item(r,c))->isSelected() == true)
    144. {
    145. row_number = r;
    146. }
    147. }
    148. }
    149. }
    150.  
    151. // Récupération du chemin affiché sur cette ligne du tableau_scan
    152. QTableWidgetItem *QWI_element_courant;
    153. QWI_element_courant = tableau_scan->item(row_number, 2);
    154. QString chemin_acces_affiche = QWI_element_courant->text();
    155.  
    156. // Fenêtre de recherche de répertoire
    157. QString titre = "Répertoire à scanner";
    158. QString chemin = "/home";
    159. if (chemin_acces_affiche != "")
    160. {
    161. chemin = chemin_acces_affiche;
    162. }
    163. QString chemin_acces_voulu = QFileDialog::getExistingDirectory(this, titre, chemin, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    164.  
    165. // Mise à jour du chemin affiché dans cette ligne du tableau_scan
    166. if (chemin_acces_voulu != "")
    167. {
    168. QWI_element_courant->setText(chemin_acces_voulu);
    169. }
    170. }
    171. void interface_impl::SLOT_ajouter_repertoire_noscan()
    172. {
    173. cout << "SLOT_ajouter_repertoire_noscan" << endl;
    174.  
    175. tableau_noscan->setRowCount(tableau_noscan->rowCount() + 1);
    176.  
    177. QTableWidgetItem *Item_2 = new QTableWidgetItem("");
    178.  
    179. QPushButton *bouton_rechercher = new QPushButton("Rechercher...");
    180. connect(bouton_rechercher, SIGNAL(clicked()), this, SLOT(SLOT_rechercher_repertoire_noscan()));
    181.  
    182. Item_0->setCheckState(Qt::Unchecked);
    183. Item_0->setFlags(Item_0->flags() & (~(Qt::ItemIsEditable)));
    184. //Item_2->setFlags(Item_2->flags() & (Qt::ItemIsEditable));
    185.  
    186. tableau_noscan->setItem(tableau_noscan->rowCount()-1, 0, Item_0);
    187. tableau_noscan->setCellWidget(tableau_noscan->rowCount()-1, 1, bouton_rechercher);
    188. tableau_noscan->setItem(tableau_noscan->rowCount()-1, 2, Item_2);
    189. }
    190.  
    191. void interface_impl::SLOT_supprimer_repertoire_noscan()
    192. {
    193. cout << "SLOT_supprimer_repertoire_noscan" << endl;
    194.  
    195. // Recherche la ligne du tableau_scan sélectionnée
    196. QPushButton *QPB_element_courant;
    197. int row_number = -1;
    198. for (int r = 0; r < tableau_noscan->rowCount(); ++r)
    199. {
    200. for (int c = 0; c < tableau_noscan->columnCount(); ++c)
    201. {
    202. if (c == 0) // Item
    203. {
    204. if ((tableau_noscan->item(r,c))->isSelected() == true)
    205. {
    206. row_number = r;
    207. }
    208. }
    209. else if (c == 1) // Boutons
    210. {
    211. QPB_element_courant = static_cast<QPushButton*>(tableau_noscan->cellWidget(r, c));
    212. if (QPB_element_courant->hasFocus() == true)
    213. {
    214. row_number = r;
    215. }
    216. }
    217. else if (c == 2) // Item
    218. {
    219. if ((tableau_noscan->item(r,c))->isSelected() == true)
    220. {
    221. row_number = r;
    222. }
    223. }
    224. }
    225. }
    226.  
    227. if (row_number == -1)
    228. {
    229. tableau_noscan->removeRow(tableau_noscan->rowCount()-1);
    230. }
    231. else
    232. {
    233. tableau_noscan->removeRow(row_number);
    234. }
    235. }
    236.  
    237. void interface_impl::SLOT_rechercher_repertoire_noscan()
    238. {
    239. cout << "SLOT_rechercher_repertoire_noscan" << endl;
    240.  
    241. // Recherche la ligne du tableau_noscan depuis laquelle le bouton a été cliqué
    242. QPushButton *QPB_element_courant;
    243. int row_number = -1;
    244. for (int r = 0; r < tableau_noscan->rowCount(); ++r)
    245. {
    246. for (int c = 0; c < tableau_noscan->columnCount(); ++c)
    247. {
    248. if (c == 0) // Item
    249. {
    250. if ((tableau_noscan->item(r,c))->isSelected() == true)
    251. {
    252. row_number = r;
    253. }
    254. }
    255. else if (c == 1) // Boutons
    256. {
    257. QPB_element_courant = static_cast<QPushButton*>(tableau_noscan->cellWidget(r, c));
    258. if (QPB_element_courant->hasFocus() == true)
    259. {
    260. row_number = r;
    261. }
    262. }
    263. else if (c == 2) // Item
    264. {
    265. if ((tableau_noscan->item(r,c))->isSelected() == true)
    266. {
    267. row_number = r;
    268. }
    269. }
    270. }
    271. }
    272.  
    273. // Récupération du chemin affiché sur cette ligne du tableau_scan
    274. QTableWidgetItem *QWI_element_courant;
    275. QWI_element_courant = tableau_noscan->item(row_number, 2);
    276. QString chemin_acces_affiche = QWI_element_courant->text();
    277.  
    278. // Fenêtre de recherche de répertoire
    279. QString titre = "Répertoire à scanner";
    280. QString chemin = "/home";
    281. if (chemin_acces_affiche != "")
    282. {
    283. chemin = chemin_acces_affiche;
    284. }
    285. QString chemin_acces_voulu = QFileDialog::getExistingDirectory(this, titre, chemin, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    286.  
    287. // Mise à jour du chemin affiché dans cette ligne du tableau_scan
    288. if (chemin_acces_voulu != "")
    289. {
    290. QWI_element_courant->setText(chemin_acces_voulu);
    291. }
    292. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Having trouble clearing a QTreeWidget.

    Rah no... There is too much code, I'm at the end of the project.
    Moreover, I should shouw you the Listeur class too, cause it adds QTreeWidgetItem* to the QTreeWidget (This is the main class that create theinterface and serach for the data that will fill the QTreeWidget... :s)

    I'll try to do something more clearly later, because it won't be helpfull if I give you all that code as is... With so poor comments

Similar Threads

  1. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.