Results 1 to 9 of 9

Thread: Customizing QFileDialog

  1. #1
    Join Date
    Oct 2009
    Location
    NJ, USA
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Customizing QFileDialog

    I am trying to customize QFileDialog, so that custom information is dispayed in 'Type' column for certain files and directories. I've started by subclassing QFileSystemModel and redefining type() function. So, for certain file extensions it would return custom type. Now I want to use this class as model for QFileDialog and I see only one possibility. Namely, use proxy model. So, I have my custom file dialog class, which is derived from QFileDialog that has this in a constructor:

    Qt Code:
    1. MyFileDialog::MyFileDialog
    2. (
    3. const QList<QUrl> &a_urls,
    4. QWidget *parent,
    5. const QString &caption,
    6. const QString &directory,
    7. const QString &filter
    8. )
    9. : QFileDialog(parent,caption,directory,filter)
    10. {
    11. setSidebarUrls(a_urls);
    12. QListView *sidebar = this->findChild<QListView *>("sidebar");
    13. m_proxy = new QSortFilterProxyModel();
    14. QFileDialog::setProxyModel(m_proxy);
    15. m_data = new MyFileSystemModel();
    16. QString rootPath = this->directory().path();
    17. m_data->setRootPath(rootPath);
    18. m_proxy->setSourceModel(m_data);
    To copy to clipboard, switch view to plain text mode 

    Now I am getting memory access error when file dialog appears and I try to select anything there.

    I also see another problem here. If I comment out last line (m_proxy->setSourceModel(m_data)) everything works except for sidebar. I suspect there may be a bug in Qt's qfiledialog.cpp setProxyModel() function. While proxyModel is set as model for list and tree views

    Qt Code:
    1. if (proxyModel != 0) {
    2. proxyModel->setParent(this);
    3. d->proxyModel = proxyModel;
    4. proxyModel->setSourceModel(d->model);
    5. d->qFileDialogUi->listView->setModel(d->proxyModel);
    6. d->qFileDialogUi->treeView->setModel(d->proxyModel);
    7. connect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
    8. this, SLOT(_q_rowsInserted(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 

    it is not done for sidebar.

    So, I have two questions here: 1) what am I doing wrong here or is this a bug in Qt? and 2) is there a better way to get custom file dialog working besides rewriting it from scratch?

    Any advise would be greatly appreciated. Thanks.

  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: Customizing QFileDialog

    It's not a bug. The file dialog is simply not meant to be subclassed that way.
    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.


  3. #3
    Join Date
    Oct 2009
    Location
    NJ, USA
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customizing QFileDialog

    Quote Originally Posted by wysota View Post
    It's not a bug. The file dialog is simply not meant to be subclassed that way.
    Ok... What would be a right way to customize file dialog then?

    Yet even if I don't subclass QFileDialog using setProxyModel() will probably cause sidebar to fail.

    Thanks.

  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: Customizing QFileDialog

    I wouldn't say there is any right way to do it. Especially that in 90% of the cases native dialogs are used and not the dialog created by 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.


  5. The following user says thank you to wysota for this useful post:

    ScabrusMantra (20th October 2009)

  6. #5
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customizing QFileDialog

    Is it possible to customize QFileDialog in the way that there is no way to leave a particular (specified) subdirectory? The reason is, I wouldn't like the user to choose the files from ANY directory but from the given ones.

    Thanks in advance

  7. #6
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Customizing QFileDialog

    I guess you could read this thread : http://www.qtcentre.org/threads/2817...ific-directory

  8. #7
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customizing QFileDialog

    Oh, thanks, I guess, this should work!!

  9. #8
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customizing QFileDialog

    Well, this works only partially. Namely, e.g. I wanna stay in the directory /tmp. And click to Up-icon in the QFileDialog window. This generates the signal directoryEntered(const QString), which I can catch up and move back to /tmp. However, the path "/" remains in the history. And then backButton from the QFileDialogPrivate can be used and does not generate the signal. In this way, I can traverse the entire /-tree.

    Would appreciate any comments

    P.S. clearing histroy also didn't help.

    P.P.S changeEvent also doesn't react to back- and forward-buttons. However, it does work on go-up-button
    Last edited by UrfinJuice; 22nd July 2011 at 10:59.

  10. #9
    Join Date
    Mar 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customizing QFileDialog

    Ok, just to tell how I solved the problem:

    In the file qfiledialog.cpp in functions
    void QFileDialogPrivate::_q_navigateForward()
    and
    void QFileDialogPrivate::_q_navigateBackward()
    after
    q->setDirectory(nextHistory);
    and
    q->setDirectory(previousHistory);

    I added
    emit q->directoryEntered(nextHistory);
    and
    emit q->directoryEntered(previousHistory);
    respectively.

    Recompile -> runs

    However, I'm not sure, if it's a bug or a feature

Similar Threads

  1. QFileDialog - will be closed after selecting a file
    By skyperhh in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 17:49
  2. subclass of QFileDialog
    By afail in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 18:09
  3. QFileDialog in Qt designer
    By tpf80 in forum Qt Tools
    Replies: 1
    Last Post: 17th May 2007, 01:41
  4. QFileDialog Mac / PC
    By hey in forum Qt Programming
    Replies: 3
    Last Post: 26th April 2007, 19:23
  5. copy file/s from QFileDialog
    By raphaelf in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 15:26

Tags for this Thread

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.