Question

For a QFileDialog, is it possible to have either files or directories selectable, the choice being given to user on the same UI (like the way a user selects different filetypes amongst filters and the filelist updates accordingly)?

Was it helpful?

Solution

I have done some research and with some help from IRC ppl I found an easier solution. Basically adding a widget (checkbox, a suitable for this one) and connecting it to the file dialog does the work.

(It's actually someone else's answer which I have improved & furnished. Thanks to him ;). Posting answer here just for reference if someone else stumbles here).

from sys import argv

from PySide import QtGui, QtCore


class MyDialog(QtGui.QFileDialog):
    def __init__(self, parent=None):
        super (MyDialog, self).__init__()
        self.init_ui()

    def init_ui(self):
        cb = QtGui.QCheckBox('Select directory')
        cb.stateChanged.connect(self.toggle_files_folders)
        self.layout().addWidget(cb)

    def toggle_files_folders(self, state):
        if state == QtCore.Qt.Checked:
            self.setFileMode(self.Directory)
            self.setOption(self.ShowDirsOnly, True)
        else:
            self.setFileMode(self.AnyFile)
            self.setOption(self.ShowDirsOnly, False)
            self.setNameFilter('All files (*)')


def main():
    app = QtGui.QApplication(argv)
    dialog = MyDialog()
    dialog.show()
    raise SystemExit(app.exec_())


if __name__ == '__main__':
    main()

OTHER TIPS

Yes, it is. Here is one way:

In the header, declare your QFileDialog pointer:

class buggy : public QWidget
{
    Q_OBJECT
public:
    buggy(QWidget *parent = 0);
    QFileDialog *d;
public slots:
    void createDialog();
    void changeDialog();
};

In your implementation, set the QFileDialog::DontUseNativeDialog option (you must do this on Mac OS, but I haven't tested it elsewhere), then override the appropriate window flags to get your dialog to display as you like it.

Finally, add a button (or check box) that calls a function to change the file mode of your QFileDialog:

buggy::buggy(QWidget *){
    //Ignore this, as its just for example implementation
    this->setGeometry(0,0,200,100);
    QPushButton *button = new QPushButton(this);
    button->setGeometry(0,0,100,50);
    button->setText("Dialog");
    connect( button, SIGNAL(clicked()),this,SLOT(createDialog()));
    //Stop ignoring and initialize this variable
    d=NULL;
}
void buggy::createDialog(void){
      d = new QFileDialog(this);
      d->setOption(QFileDialog::DontUseNativeDialog);
      d->overrideWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::MacWindowToolBarButtonHint);
      QPushButton *b = new QPushButton(d);
      connect(b,SIGNAL(clicked()),this,SLOT(changeDialog()));
      b->setText("Dir Only");
      switch(d->exec()){
          default:
          break;
      }
      delete(d);
      d=NULL;
}

//FUNCTION:changeDialog(), called from the QFileDialog to switch the fileMode.

void buggy::changeDialog(){
    if(d != NULL){
    QPushButton *pb = (QPushButton*)d->childAt(5,5);
    if(pb->text().contains("Dir Only")){
        pb->setText("File Only");
        d->setFileMode(QFileDialog::Directory);
    }else{
        pb->setText("Dir Only");
        d->setFileMode(QFileDialog::ExistingFile);
    }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top