PDA

View Full Version : Creating a Custom FileOpen Dialog



cpsmusic
3rd July 2011, 14:11
Hi,

In the app I'm developing I need a custom FileOpen dialog. The dialog is basically the same as the standard QFileDialog however it needs two additional QLineEdits.

I've tried creating my own FileDialog by extending QFileDialog however I can't figure out how to add two additional widgets to it.

I tried the following however it obviously won't work as there is no "addWidget" method available.



MyFileDialog::MyFileDialog() : QFileDialog() {

setWindowTitle("Add Clip");

QLineEdit *trackNumberLineEdit = new QLineEdit;
QLineEdit *startIndexLineEdit = new QLineEdit;

this->addWidget(trackNumberLineEdit);
this->addWidget(startIndexLineEdit);
}


Is this the right way to do this? If so, how is it done?

Cheers,

Chris

mvuori
3rd July 2011, 15:34
What you need to do is extract the layout of the QFileDialog and add the new widgets into the layout.

See: http://www.qtforum.org/article/20841/how-to-add-a-qwidget-in-qfiledialog.html#post78422

cpsmusic
3rd July 2011, 17:00
I tried the following:



MyFileDialog::MyFileDialog()
{

QGridLayout* mainLayout = dynamic_cast<QGridLayout*>(layout());

if ( ! mainLayout ) {
assert(0); // in case of future changes
} else {

QHBoxLayout *hbl = new QHBoxLayout(this);

// add some widgets
m_cb = new QCheckBox("My Checkbox");
hbl->addWidget(m_cb);

int numRows = mainLayout->rowCount();

// add the new layout to the bottom of mainLayout
// and span all columns
mainLayout->addLayout( hbl, numRows,0,1,-1);
}
}


however I'm getting the following error:

QLayout: Attempting to add QLayout "" to QFileDialog "QFileDialog", which already has a layout

SixDegrees
3rd July 2011, 18:03
I'm not certain what's wrong with your code, but I wanted to add: the inability to add custom widgets, particularly previews, to the QFileDialog is a very serious shortcoming. This was simple to do in earlier versions of Qt, but the capability was removed in Qt 4 and has never returned.

The suggestion to hack on the existing, undocumented layout to ram one's own widgets into the dialog is ridiculous. Yes, it can be done, but it risks completely breaking with each new release.

Whoever is in charge of Qt knows that this is a major flaw - they have been notified of it many times - yet nothing is being done about it. It is long past time to fix this and to fix it the right way.

squidge
3rd July 2011, 18:34
I think the problem is that they want to use native platform dialogs as much as possible, but once you do that you lose all the Qt functionality in such dialogs so then you are in platform specific land.

Designing your own dialog that looks correct for every version of every OS supported is a massive task. Look how many different official versions of the Windows File Open dialog there is for example.

SixDegrees
3rd July 2011, 22:33
True. But every platform I'm aware of provides the ability to add arbitrary widgets to their file dialogs. This is a crucial ability of such dialogs, and its absence in Qt is a detriment, and a huge step backward from previous versions.

squidge
3rd July 2011, 23:22
Indeed, but I don't think the dialogs themselves accept Qt widgets.

I thought previous versions of Qt which did support this behavior supported it through implementing the entire dialog themselves rather than using the native OS dialogs, and as such looked the same regardless of OS.

ChrisW67
4th July 2011, 00:13
I thought you are able to force the non-native dialog by setting option QFileDialog::DontUseNativeDialog. In this case the Qt layout mechanisms should be available regardless of platform but at the cost of non-native look.

cpsmusic
4th July 2011, 15:45
What you need to do is extract the layout of the QFileDialog and add the new widgets into the layout.

See: http://www.qtforum.org/article/20841/how-to-add-a-qwidget-in-qfiledialog.html#post78422

I tried both of the methods in the thread above however neither worked.

In the case of the second method



void FileDialog::addCheckBoxIn()
{
QDialogButtonBox *box = qFindChild<QDialogButtonBox*>(this);
Q_ASSERT(box);
QBoxLayout *l = qFindChild<QBoxLayout*>(box);
Q_ASSERT(l);
QCheckBox *toProj = new QCheckBox("To Project:", box);
toProj->setChecked(true);
l->insertWidget(0, toProj);
}



the box has a value of 0x0 after it's constructed so the program crashes.

Any other suggestions as to how to create a custom FileOpen dialog?

nish
4th July 2011, 16:55
you should not pass this as a parent to hbox layout.. see the comment in your code below

I tried the following:




QHBoxLayout *hbl = new QHBoxLayout(this);//REMOVE "this" just use - new QHBoxLayout();



however I'm getting the following error:

QLayout: Attempting to add QLayout "" to QFileDialog "QFileDialog", which already has a layout

cpsmusic
5th July 2011, 11:41
I tried what was suggested. The code now runs but there are no additional widgets added to the dialog.

nish
5th July 2011, 14:15
provide a minimal compilable example reproducing the problem

squidge
5th July 2011, 14:25
I tried what was suggested. The code now runs but there are no additional widgets added to the dialog.

Are you using QFileDialog::DontUseNativeDialog ?

cpsmusic
6th July 2011, 12:16
I am now!

Problem solved - thanks.

Bleach
31st March 2014, 17:34
Show please the final version of the code