c++checkboxqfiledialogqt3

How to add checkbox to QFileDialog window in QT3?


I have a problem with adding an extra checkbox to QFileDialog window. I'm talking about the same window which is opened during using QFileDialog::getSaveFileName.

I know how to do this in QT5, I need to make a subclass and set option QFileDialog::DontUseNativeDialog.

How can I do this in QT3?

Because there I don't have QFileDialog::DontUseNativeDialog. In QT3 there are fewer methods to choose.

Is there a similar method or maybe completely different approach to adding this checkbox?


Solution

  • While the static methods (like getSaveFileName) use native dialogs, you can still subclass QFileDialog and use addWidgets method to add any widget underneath the file types combo box, i.e. at the bottom of the dialog.

    This is a very simple example, with a checkbox:

    #include <qfiledialog.h>
    #include <qcheckbox.h>
    class FileDialog : public QFileDialog
    {
    public:
      FileDialog() : QFileDialog(0)
      {
            QCheckBox* checkbox = new QCheckBox(this);
            checkbox->setText("Check me!!!");
            addWidgets( 0, checkbox, 0 );
      }
    };
    

    You can test it in a main:

    #include <qapplication.h>
    int main(int argc, char * argv[])
    {
      QApplication a(argc, argv);
    
      FileDialog d;    
      d.exec();
    
      return a.exec();
    }
    

    To add a check box in the rightmost position, below the Cancel button, one could try to subclass a QPushButton (which is the expected type), lay out a check box in it, override the paintEvent with an empty implementation, so the push button won't be drawn at all (but the check box will).

    #include <qcheckbox.h>
    #include <qpushbutton.h>
    #include <qlayout.h>
    class CheckBox : public QPushButton
    {
      QCheckBox* checkbox;
    public:
      CheckBox(QWidget * parent) : QPushButton(parent)
      {
        QGridLayout * box = new QGridLayout(this);
        checkbox = new QCheckBox(this);
        checkbox->setText("Check this!!!");
        box->addWidget(checkbox, 0, 0, Qt::AlignRight);
      }
      void paintEvent ( QPaintEvent * ){}
    };
    

    This way the check box can be added as the third argument of addWidgets:

    class FileDialog : public QFileDialog
    {
    public:
      FileDialog() : QFileDialog(0)
      {
        addWidgets( 0, 0, new CheckBox(this));
      }
    };
    

    and show up at the bottom right corner of the dialog box.