2
votes

How to change the size of a button in the QFileDialog? I tried the below code; it compiles but the application exits unexpectedly if I try to load the file dialogue.

Please let me know what is going wrong. I am new to QT. :(

        QFileDialog *fdiag = new QFileDialog();
        QGridLayout *glayout = static_cast <QGridLayout*>(fdiag->layout());

        QLayoutItem *li = glayout->itemAtPosition(3,3);

        QRect buttonRect = li->geometry() ;
        int newHeight = buttonRect.height() + 20;
        int newWidth = buttonRect.height() + 20;
        buttonRect.setHeight(newHeight);
        buttonRect.setWidth(newWidth);
        li->setGeometry(buttonRect);

        fdiag->resize(720,480);
        fdiag->setWindowTitle("Media Folder");

        fdiag->exec();
2

2 Answers

4
votes

You could just use a stylesheet:

QFileDialog *fdiag = new QFileDialog;
fdiag->setStyleSheet("QPushButton{min-height: 40px; min-width: 200px;}");

Or if you want to resize a specific button only:

QDialogButtonBox *box = fdiag->findChild<QDialogButtonBox*>();
if(box)
{
    QPushButton *button = box->button(QDialogButtonBox::Open);
    if(button)
    {
        button->setMinimumHeight(40);
        button->setMinimumWidth(200);
    }
}
0
votes

As i've got this problem recently, with Qt5 - for some reasons in Qt4 it worked - you need to make sure, that Qt returns not Null from layout().

You need to initialize the System dialog with the Option QFileDialog::DontUseNativeDialog, e.g. with dlg->setOption(QFileDialog::DontUseNativeDialog, true);