简体   繁体   English

在 QFileDialog 中禁用文件名框

[英]Disable file name box in QFileDialog

I used a QFileDialog to open a browser.我使用 QFileDialog 打开浏览器。 Here is my code :这是我的代码

QString filePath = QFileDialog::getSaveFileName(this,
                                               "Export Xml", "PluginPythonQt",
                                                "Xml files (*.xml)");

When excute it will show a dialog like this:执行时会显示如下对话框:

在此处输入图像描述

I want to disable the "File name:" box in the picture or prevent user to enter a new name.我想禁用图片中的“文件名:”框或阻止用户输入新名称。 How can i do that?我怎样才能做到这一点? Thanks.谢谢。

I believe you can't achieve this — save dialog is about choosing name besides the choosing where to save it. 我相信您无法实现这一点-保存对话框除了选择保存位置之外,还涉及选择名称。 Of course, you might just ignore what user typed and force your name when he hits OK but it will just make the user angry. 当然,您可能会忽略用户键入的内容,并在他单击OK时强行输入您的名字,但这只会使用户生气。

Better way, in my opinion, is to use QFileDialog::getExistingDirectory which will allow the user to choose where to save the file but won't allow him to choose the file name. 我认为更好的方法是使用QFileDialog::getExistingDirectory ,它将允许用户选择保存文件的位置,但不允许他选择文件名。 It will be fair, at least. 至少这是公平的。

Similar question was answered in https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box .https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box中回答了类似的问题。

In general, you can hide any element in any widget if you dig a bit into widget's source code to find element's name, when you have a name, you can find the corresponding element via findChild<QWidget *>(elementName) .通常,如果您深入研究小部件的源代码以查找元素的名称,则可以隐藏任何小部件中的任何元素,当您有名称时,您可以通过findChild<QWidget *>(elementName)找到相应的元素。 Usually if you check QSomeWidget.h (Qt is open source.) you can find element names very easily as they are typically listed as the widgets members, To hide both labels, fileEdit, ComboBox and even buttons: you can use this code:通常,如果您检查 QSomeWidget.h(Qt 是开源的。),您可以很容易地找到元素名称,因为它们通常被列为小部件成员,要隐藏两个标签、fileEdit、ComboBox 甚至按钮:您可以使用此代码:

QFileDialog fileDialog = new QFileDialog;

QWidget * fileNameEdit = fileDialog->findChild<QWidget *>("fileNameEdit");
Q_ASSERT(fileNameEdit);
fileNameEdit->setVisible(false);

QWidget * fileNameLabel = fileDialog->findChild<QWidget *>("fileNameLabel");
fileNameLabel->setVisible(false);

QWidget * fileTypeCombo = fileDialog->findChild<QWidget *>("fileTypeCombo");
Q_ASSERT(fileTypeCombo);
fileTypeCombo->setVisible(false);
    
QWidget * fileTypeLabel = fileDialog->findChild<QWidget *>("fileTypeLabel");
fileTypeLabel->setVisible(false);

QWidget * fileButtonBox = fileDialog->findChild<QWidget *>("buttonBox");
fileButtonBox->setVisible(false);

Note that even though buttons are hidden, typing Enter on keyboard (or double clicking) would trigger Open button, and dialog might disappear if you haven't done anything in Accept method.请注意,即使按钮被隐藏,在键盘上键入 Enter(或双击)也会触发“打开”按钮,如果您没有在 Accept 方法中执行任何操作,对话框可能会消失。 So it would also be a good idea to handle state of that button as well if you really wish buttons to be hidden as well.因此,如果您真的希望按钮也被隐藏,那么处理该按钮的 state 也是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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