1

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?

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
Beond
  • 13
  • 4

1 Answers1

0

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.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Thanks for help :) I was very close to this solution, but now I have problem with setting this check box at the lower right corner, under "Type of file" label. Because when i use a layout without QFileDialog::DontUseNativeDialog my program crashes and quits. What should i do to move it? I haven't find solution yet. – Beond Mar 21 '18 at 19:19
  • Hey@Beond, I updated the answer, I hope the addendum solves your issue. – p-a-o-l-o Mar 22 '18 at 09:25
  • It added checkbox, but with distorted graphics -> https://i.imgur.com/9Scs3AZ.png How can I fix it? And if I would like to create this widget at the left corner under label the way would be the same? Because with positioning widget I have some problems. Thanks very much for help – Beond Mar 22 '18 at 10:08
  • About bad rendering: try setting some height to the checkbox (e.g. `checkbox->setFixedHeight(48);`. About putting the check box on the left side, you have to subclass `QLabel` instead of `QPushButton`, the strategy is pretty much the same ... – p-a-o-l-o Mar 22 '18 at 11:44
  • Yes I know it's the same strategy, but don't know why I couldnt add this. I will try again, thanks very much for help :) – Beond Mar 22 '18 at 11:49
  • I can't set in left corner that it doesn't distorted graphics, `checkbox->setFixedHeight()` doesn't help. Is any other way to add this? – Beond Mar 22 '18 at 14:14