Adding pushButton on QFileDialog
-
wrote on 9 Jan 2017, 06:42 last edited by
Hi
I tried to add a pushButton on QFileDialog byclass FileDialog : public QFileDialog { QPushButton *pb; public: FileDialog(QWidget *parent) : QFileDialog(parent){} void addWidget() { QGridLayout* mainLayout = dynamic_cast <QGridLayout*>(this->layout()); if( !mainLayout){ qDebug()<<"mainLayout is NULL"; }else{ QHBoxLayout *hbl =new QHBoxLayout(0); pb =new QPushButton(QString("My checkbox")); hbl->addWidget(pb); int num_rows = mainLayout->rowCount(); qDebug()<<"num_rows: "<<num_rows; mainLayout->addLayout(hbl, num_rows, 0, 1, -1); } } }; When I executed FileDialog *fd=new FileDialog(this); fd->addWidget();
I got the result,
"mainLayout is NULL"
It means I could not get the QGridLayout* by this->layout().
Is it a bug? I use Qt 5.6.2 in windows7. -
wrote on 9 Jan 2017, 07:18 last edited by
Try to use http://doc.qt.io/qt-5/qobject.html#qobject_cast . Or just pass layout pointer as argument in your function.
-
Hi
I tried to add a pushButton on QFileDialog byclass FileDialog : public QFileDialog { QPushButton *pb; public: FileDialog(QWidget *parent) : QFileDialog(parent){} void addWidget() { QGridLayout* mainLayout = dynamic_cast <QGridLayout*>(this->layout()); if( !mainLayout){ qDebug()<<"mainLayout is NULL"; }else{ QHBoxLayout *hbl =new QHBoxLayout(0); pb =new QPushButton(QString("My checkbox")); hbl->addWidget(pb); int num_rows = mainLayout->rowCount(); qDebug()<<"num_rows: "<<num_rows; mainLayout->addLayout(hbl, num_rows, 0, 1, -1); } } }; When I executed FileDialog *fd=new FileDialog(this); fd->addWidget();
I got the result,
"mainLayout is NULL"
It means I could not get the QGridLayout* by this->layout().
Is it a bug? I use Qt 5.6.2 in windows7.@samdol I guess the layout in a QFileDialog is not QGridLayout, that's why the cast fails.
-
wrote on 9 Jan 2017, 11:23 last edited by mtrch 1 Sept 2017, 11:24
This is because QFileDialog uses native file dialog by default, you just need to turn off this behavior:
FileDialog(QWidget *parent) : QFileDialog(parent) { setOption(QFileDialog::DontUseNativeDialog); }
-
This is because QFileDialog uses native file dialog by default, you just need to turn off this behavior:
FileDialog(QWidget *parent) : QFileDialog(parent) { setOption(QFileDialog::DontUseNativeDialog); }
-
wrote on 10 Jan 2017, 09:35 last edited by m.sue 1 Oct 2017, 09:52
getOpenFileName
is a static function. It does not care about any changes you made tofd
.You will have to do everything of the static function yourself:
fd->setWindowTitle("Open Image"); fd->setAcceptMode(QFileDialog::AcceptOpen); fd->setDirectory("/home/jana"); fd->setFileMode(QFileDialog::ExistingFile); fd->setViewMode(QFileDialog::Detail); fd->setNameFilter(tr("Image Files (*.png *.jpg *.bmp)")); if (!fd->exec()) //cancelled return; fileName=fd->selectedFiles().first();
-Michael.
-
getOpenFileName
is a static function. It does not care about any changes you made tofd
.You will have to do everything of the static function yourself:
fd->setWindowTitle("Open Image"); fd->setAcceptMode(QFileDialog::AcceptOpen); fd->setDirectory("/home/jana"); fd->setFileMode(QFileDialog::ExistingFile); fd->setViewMode(QFileDialog::Detail); fd->setNameFilter(tr("Image Files (*.png *.jpg *.bmp)")); if (!fd->exec()) //cancelled return; fileName=fd->selectedFiles().first();
-Michael.
-
wrote on 10 Jan 2017, 13:03 last edited by m.sue 1 Oct 2017, 13:03
"style": IIRC the static function uses the native dialog, by default. If you mean that, then no (see above).
2/8