简体   繁体   中英

How can i disable or hide default cancel button in QFileDialog?

I'm trying to use QFileDialog as a widget, to use QFileDialog as a widget the final step for my is to disable the cancel button.

Have you an idea of how can i disable this button.

PS: I am using Qt 5.5.0

You should be able to access the various standard buttons via the QDialogButtonBox and, from there, do what you want with the Cancel button.

The following example code appears to works as expected...

QFileDialog fd;

/*
 * Find the QDialogButtonBox.
 */
if (auto *button_box = fd.findChild<QDialogButtonBox *>()) {

  /*
   * Now find the `Cancel' button...
   */
  if (auto *cancel_button = button_box->button(QDialogButtonBox::Cancel)) {

    /*
     * ...and remove it (or disable it if you prefer).
     */
    button_box->removeButton(cancel_button);
  }
}
fd.show();

The QFileDialog Class doesn't seem to have any option for this.

However, you could make your own file browser using QTreeModel and QTreeView (It's not too difficult).

There is a tutorial on how to do just that here .

It would take a while to type out all the code (sorry, I'm a slow typer), but this tutorial should allow you to have the flexibility you need to do what you want to do.

I understand that this answer isn't exactly what you asked, but I hope it is a good alternative.

EDIT: Accidentally pasted wrong link for QFileDialog Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM