Yoann
Yoann

Reputation: 42

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

Upvotes: 0

Views: 1077

Answers (2)

SPEEECH
SPEEECH

Reputation: 11

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

Upvotes: 1

G.M.
G.M.

Reputation: 12898

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();

Upvotes: 3

Related Questions