Results 1 to 10 of 10

Thread: open (and delete) a file with a button on qtreeview

  1. #1
    Join Date
    Aug 2011
    Location
    Rimini, Italy
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default open (and delete) a file with a button on qtreeview

    Hi I have this problem:
    I need to open (and delete) a file (it's displayed on a QTreeView) with a button.
    At the moment I can only do this operation with a double click, but is not enough.

    This is my header:

    Qt Code:
    1. #ifndef EDITOR_H
    2. #define EDITOR_H
    3.  
    4. #include <QDialog>
    5. #include <QtCore>
    6. #include <QtGui>
    7. #include <QFileSystemModel>
    8. #include <QCoreApplication>
    9. #include <QFile>
    10.  
    11. class QString;
    12.  
    13. namespace Ui {
    14. class editor;
    15. }
    16.  
    17. class editor : public QDialog
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit editor(QWidget *parent = 0);
    23. ~editor();
    24.  
    25. private slots:
    26. void on_vistaLista_doubleClicked(const QModelIndex &index);
    27. void getDetails(const QModelIndex &index);
    28.  
    29. void on_cancellaFileEditor_clicked();
    30.  
    31. private:
    32. Ui::editor *ui;
    33. QFileSystemModel *model;
    34.  
    35. };
    36.  
    37. #endif // EDITOR_H
    To copy to clipboard, switch view to plain text mode 

    and the program....


    Qt Code:
    1. #include "editor.h"
    2. #include "ui_editor.h"
    3.  
    4. editor::editor(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::editor)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QStringList lista;
    11. QString directory = qApp->applicationDirPath() + "/download";
    12.  
    13.  
    14. model = new QFileSystemModel(this);
    15. model->setFilter(QDir::Files);
    16.  
    17.  
    18. ui->vistaLista->setModel(model);
    19. ui->vistaLista->setRootIndex(model->setRootPath(directory));
    20. connect(ui->vistaLista, SIGNAL(clicked(QModelIndex)), this, SLOT(getDetails(QModelIndex)));
    21.  
    22. }
    23.  
    24. editor::~editor()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void editor::on_vistaLista_doubleClicked(const QModelIndex &index)
    30. {
    31. QString url = model->fileInfo(index).filePath();
    32. QDesktopServices::openUrl(QUrl::fromLocalFile(url));
    33. }
    34.  
    35. void editor::getDetails(const QModelIndex &index)
    36. {
    37. QString filename=model->fileInfo(index).filePath();
    38. ui->labelTemporaneaEditor->setText(filename);
    39. }
    40.  
    41.  
    42.  
    43. void editor::on_cancellaFileEditor_clicked()
    44. {
    45.  
    46. //????????????????????????????????? ARGHHHHHH ??????????????????
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

    Please help me!!!! Thanks!
    Last edited by high_flyer; 30th August 2011 at 14:39. Reason: code tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: open (and delete) a file with a button on qtreeview

    on_cancellaFileEditor_clicked() is, I guess, where you want to delete a file... and also the only part of your program you don't show us. What are you doing in that routine? What are you expecting to happen? What is actually happenning?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Aug 2011
    Location
    Rimini, Italy
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: open (and delete) a file with a button on qtreeview

    Sorry, but yesterday it was late at night (and my english is rusty!).

    Yes, on_cancellaFileEditor_clicked() is the slot where i want to delete the file with QFile::remove(const QString &fileName), and I want another slot like on_openFileEditor_clicked() where open file with a default program using QDesktopServicesenUrl(QUrl::fromLocalFile(url)).
    I've try get the path of the selected file from the model with getDetails(const QModelIndex &index), it works but only, obviusly, when I click over the file in the TreeView, not if i use the arrow key...and I need that function....
    ...I hope that I made ​​it clear...

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: open (and delete) a file with a button on qtreeview

    But on what level is your problem?
    Is your slot getting called?
    Or is the problem getting the file location?
    Please explain what the PROBLEM is.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2011
    Location
    Rimini, Italy
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: open (and delete) a file with a button on qtreeview

    Ok.

    The problems are:

    1) I've get the file location connecting the signal clicked(QModelIndex) issued by the QtreeView to getDetails, but is not enough. I need to get this information each time the file is selected in the list, regardless of what the select (mouse click or arrow keys). There is a different signal to send?

    2) Newly found information (file location), how can I share it with the solts of the buttons?

    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: open (and delete) a file with a button on qtreeview

    1. just use itemPressed().
    2. use a member?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    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: open (and delete) a file with a button on qtreeview

    I think something like currentIndexChanged() will be better. It should be available via QItemSelectionModel of the view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Aug 2011
    Location
    Rimini, Italy
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: open (and delete) a file with a button on qtreeview

    Great! currentChanged() works fine thanks.
    ItemPressed() works like click(), thanks anyway.

    Now remains the second problem. I've tried in many ways, but I can't pass the string filename from getDetails to the other slots (open files and delete files).
    Probably I think in the wrong way....any idea(s)?

  9. #9
    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: open (and delete) a file with a button on qtreeview

    Try explaining better what you want and what the problem is. It seems the problem is related to application design and not to using Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Aug 2011
    Location
    Rimini, Italy
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: open (and delete) a file with a button on qtreeview

    Quote Originally Posted by wysota View Post
    Try explaining better what you want and what the problem is. It seems the problem is related to application design and not to using Qt.
    Yes, the problem was in the architecture...now works perfectly.

    The solution was in currentIndex() ... nearest currentChanged() .
    This is the solution:

    Qt Code:
    1. #include "editor.h"
    2. #include "ui_editor.h"
    3.  
    4. editor::editor(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::editor)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QStringList lista;
    11. QString directory = qApp->applicationDirPath() + "/download";
    12.  
    13. model = new QFileSystemModel(this);
    14. model->setFilter(QDir::Files);
    15.  
    16. itemModel = new QItemSelectionModel(model);
    17.  
    18. ui->vistaLista->setModel(model);
    19. ui->vistaLista->setSelectionModel(itemModel);
    20. ui->vistaLista->setRootIndex(model->setRootPath(directory));
    21.  
    22. connect(ui->bottoneCancellaFileEditor, SIGNAL(clicked()), this, SLOT(on_cancellaFileEditor_clicked()));
    23. connect(ui->bottoneApriEditor, SIGNAL(clicked()), this, SLOT(on_bottoneApriEditor_clicked()));
    24. }
    25.  
    26. editor::~editor()
    27. {
    28. delete ui;
    29. }
    30.  
    31. void editor::on_vistaLista_doubleClicked(const QModelIndex &index)
    32. {
    33. QString url = model->fileInfo(index).filePath();
    34. QDesktopServices::openUrl(QUrl::fromLocalFile(url));
    35. }
    36.  
    37. void editor::on_cancellaFileEditor_clicked()
    38. {
    39. QString nomeFile=model->fileInfo(itemModel->currentIndex()).filePath();
    40. QFile::remove(nomeFile);
    41. }
    42.  
    43. void editor::on_bottoneApriEditor_clicked()
    44. {
    45. QString nomeFile=model->fileInfo(itemModel->currentIndex()).filePath();
    46. QDesktopServices::openUrl(QUrl::fromLocalFile(nomeFile));
    47. }
    To copy to clipboard, switch view to plain text mode 

    Thanks to all, are a valuable resource for those who are beginning (in QT and c++) like me.

  11. The following user says thank you to maxcrive for this useful post:

    iskenderoguz (8th January 2016)

Similar Threads

  1. Replies: 3
    Last Post: 11th June 2012, 15:21
  2. Replies: 2
    Last Post: 10th November 2009, 06:17
  3. How to open any file on button click???
    By r3aktor in forum Newbie
    Replies: 5
    Last Post: 8th July 2009, 08:54
  4. Replies: 10
    Last Post: 6th July 2008, 09:46
  5. Replies: 3
    Last Post: 13th May 2007, 20:55

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.