Creating a Custom FileOpen Dialog
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.
Code:
setWindowTitle("Add Clip");
this->addWidget(trackNumberLineEdit);
this->addWidget(startIndexLineEdit);
}
Is this the right way to do this? If so, how is it done?
Cheers,
Chris
Re: Creating a Custom FileOpen Dialog
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...html#post78422
Re: Creating a Custom FileOpen Dialog
I tried the following:
Code:
MyFileDialog::MyFileDialog()
{
QGridLayout* mainLayout
= dynamic_cast<QGridLayout
*>
(layout
());
if ( ! mainLayout ) {
assert(0); // in case of future changes
} else {
// add some widgets
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
Re: Creating a Custom FileOpen Dialog
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.
Re: Creating a Custom FileOpen Dialog
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.
Re: Creating a Custom FileOpen Dialog
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.
Re: Creating a Custom FileOpen Dialog
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.
Re: Creating a Custom FileOpen Dialog
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.
Re: Creating a Custom FileOpen Dialog
Quote:
Originally Posted by
mvuori
I tried both of the methods in the thread above however neither worked.
In the case of the second method
Code:
void FileDialog::addCheckBoxIn()
{
Q_ASSERT(box);
Q_ASSERT(l);
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?
Re: Creating a Custom FileOpen Dialog
you should not pass this as a parent to hbox layout.. see the comment in your code below
Quote:
Originally Posted by
cpsmusic
I tried the following:
however I'm getting the following error:
QLayout: Attempting to add QLayout "" to QFileDialog "QFileDialog", which already has a layout
Re: Creating a Custom FileOpen Dialog
I tried what was suggested. The code now runs but there are no additional widgets added to the dialog.
Re: Creating a Custom FileOpen Dialog
provide a minimal compilable example reproducing the problem
Re: Creating a Custom FileOpen Dialog
Quote:
Originally Posted by
cpsmusic
I tried what was suggested. The code now runs but there are no additional widgets added to the dialog.
Are you using QFileDialog::DontUseNativeDialog ?
Re: Creating a Custom FileOpen Dialog
I am now!
Problem solved - thanks.
Re: Creating a Custom FileOpen Dialog
Show please the final version of the code