user11498510
user11498510

Reputation:

QFileDialog not showing Fonts folder

I want to Browse the Fonts folder in c:\Windows and get the filepath of the selected font.

But when the Dialog Box opens it does not show the Font folder in Windows.

Void SumFont::FontChange()
{
    QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),
        "/home",
        tr("Fonts (*.ttf)"));

    QlineEditSetFont->setText(filePath);
    isChanged = true;
    stdstrLocation = filePath.toStdString();
    this->isChanged = true; 
}

Upvotes: 2

Views: 226

Answers (1)

VVV
VVV

Reputation: 519

On Windows the fonts folder, apart from being system protected in other ways, has a special font display mode set in desktop.ini by this line: CLSID={BD84B380-8CA2-1069-AB1D-08000948F534}. That makes the AppID {50d69d24-961d-4828-9d1c-5f4717f226d1} being responsible for displaying this folder in all the system browsers. Unfortunately, the Windows-native file dialog in Qt can't display the font folder as a proper font viewer (for reasons unknown to me). There are two ways to work around this problem I'm aware of though.

First is messing with Windows itself - just rename the desktop.ini file in the fonts folder to something like desktop.ini.bak. You'll need administrator privileges for that, so do it from cmd or PowerShell started "as administrator". Also UAC must probably be turned off. After that the fonts folder will become a regular folder, and QFileDialog will be able to display it as any other folder. This is really not recommended in production, but I don't know why you need to access the fonts folder directly anyway.

Second method is not using the Windows-native file dialog.

QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),
        QStandardPaths::standardLocations(QStandardPaths::FontsLocation)[0],
        tr("Fonts (*.ttf);;Everything (*.*)"), nullptr,
        QFileDialog::DontUseNativeDialog);

The code above will open the Qt fallback dialog, which doesn’t care about desktop.ini.

Anyway, be careful when directly accessing the fonts folder. Especially if you plan to put files in it. Windows may have some hooks for updating the font cache that may not work correctly this way.

Upvotes: 2

Related Questions