Results 1 to 15 of 15

Thread: Creating a Custom FileOpen Dialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default 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.

    Qt Code:
    1. MyFileDialog::MyFileDialog() : QFileDialog() {
    2.  
    3. setWindowTitle("Add Clip");
    4.  
    5. QLineEdit *trackNumberLineEdit = new QLineEdit;
    6. QLineEdit *startIndexLineEdit = new QLineEdit;
    7.  
    8. this->addWidget(trackNumberLineEdit);
    9. this->addWidget(startIndexLineEdit);
    10. }
    To copy to clipboard, switch view to plain text mode 

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

    Cheers,

    Chris

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default 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

  3. #3
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Creating a Custom FileOpen Dialog

    I tried the following:

    Qt Code:
    1. MyFileDialog::MyFileDialog()
    2. {
    3.  
    4. QGridLayout* mainLayout = dynamic_cast<QGridLayout*>(layout());
    5.  
    6. if ( ! mainLayout ) {
    7. assert(0); // in case of future changes
    8. } else {
    9.  
    10. QHBoxLayout *hbl = new QHBoxLayout(this);
    11.  
    12. // add some widgets
    13. m_cb = new QCheckBox("My Checkbox");
    14. hbl->addWidget(m_cb);
    15.  
    16. int numRows = mainLayout->rowCount();
    17.  
    18. // add the new layout to the bottom of mainLayout
    19. // and span all columns
    20. mainLayout->addLayout( hbl, numRows,0,1,-1);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    however I'm getting the following error:

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

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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 View Post
    I tried the following:

    Qt Code:
    1. QHBoxLayout *hbl = new QHBoxLayout(this);//REMOVE "this" just use - new QHBoxLayout();
    To copy to clipboard, switch view to plain text mode 

    however I'm getting the following error:

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

  10. #10
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default 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.

  11. #11
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a Custom FileOpen Dialog

    provide a minimal compilable example reproducing the problem

  12. The following user says thank you to nish for this useful post:

    cpsmusic (6th July 2011)

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a Custom FileOpen Dialog

    Quote Originally Posted by cpsmusic View Post
    I tried what was suggested. The code now runs but there are no additional widgets added to the dialog.
    Are you using QFileDialog::DontUseNativeDialog ?

  14. The following user says thank you to squidge for this useful post:

    cpsmusic (6th July 2011)

  15. #13
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Creating a Custom FileOpen Dialog

    I am now!

    Problem solved - thanks.

  16. #14
    Join Date
    Nov 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Custom FileOpen Dialog

    Show please the final version of the code

  17. #15
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Creating a Custom FileOpen Dialog

    Quote Originally Posted by mvuori View Post
    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
    I tried both of the methods in the thread above however neither worked.

    In the case of the second method

    Qt Code:
    1. void FileDialog::addCheckBoxIn()
    2. {
    3. QDialogButtonBox *box = qFindChild<QDialogButtonBox*>(this);
    4. Q_ASSERT(box);
    5. QBoxLayout *l = qFindChild<QBoxLayout*>(box);
    6. Q_ASSERT(l);
    7. QCheckBox *toProj = new QCheckBox("To Project:", box);
    8. toProj->setChecked(true);
    9. l->insertWidget(0, toProj);
    10. }
    To copy to clipboard, switch view to plain text mode 

    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?

Similar Threads

  1. Program crashes on creating new dialog
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 11:52
  2. Replies: 17
    Last Post: 2nd June 2009, 10:18
  3. Replies: 9
    Last Post: 13th August 2008, 18:07
  4. Creating custom slots
    By harb37 in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2008, 18:10
  5. Replies: 3
    Last Post: 18th October 2007, 08:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.