Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iconfont does not work as expected. #48

Closed
ITSweets opened this issue Apr 4, 2022 · 19 comments
Closed

iconfont does not work as expected. #48

ITSweets opened this issue Apr 4, 2022 · 19 comments

Comments

@ITSweets
Copy link

ITSweets commented Apr 4, 2022

Describe the bug
iconfont doesn't work as expected in qml.

Expected behavior
Hi, just woke up and saw a message about the uic release. Exciting. So I opened another project but it has nothing to do with uic.
The problem is that I use FontLoader to load the iconfont icon font under the classpath in qml, and the interface shows garbled characters after the reference. Not as expected. I'm fine in Qt. What's happening here? Even writing an absolute path doesn't work as expected.
Please test it: use FontLoader to load iconfont font in Qml, then use font in Lable [e.g: \ue685]

e.g:

 FontLoader
 {
     name: "iconfont"
     source: "classpath:/assets/font/iconfont.ttf"
 }

use:

 Label
 {
     anchors.centerIn: parent
     text: "\ue698"
     font.family: "iconfont"
     font.pixelSize: 20
 }

iconfont official website: https://www.iconfont.cn/ You need to log in with your GitHub account, and then select the font to download.

Screenshots
If applicable, add screenshots to help explain your problem.

System (please complete the following information):

  • OS: Windows 10 20H2
  • Java version Oracle 11
  • QtJambi version 5.15.5
  • Qt version 5.15.2

Additional context
Add any other context about the problem here.

@omix
Copy link
Contributor

omix commented Apr 4, 2022

URLs pointing to classpath resources start with "file:///:classpath:/".

@ITSweets
Copy link
Author

ITSweets commented Apr 4, 2022

Ok, I've followed your instructions on the knee and it's already working as expected. It's strange that some other resources, such as images that only write classpath:... are also ok. With this issue resolved.
MediaPlayer in qml also doesn't work as expected:

code:

 MediaPlayer
 {
     id: app_music
     source: "file:///:classpath:/assets/sound/app.mp3"
 }

and in the main form windows

 Component.onCompleted:
 {
     app_music.play()

 }

to play, but there is no sound. What is the situation?
My coordinates exist in qtjambi-multimedia and
qtjambi-multimedia-native-windows-x64

@omix
Copy link
Contributor

omix commented Apr 4, 2022

It depends if the property is of type file or url. Classpath resources are internally managed by file engine. Thus, specifying a file (i.e. QFile) "classpath:/resource" works. When using QIcon in QML you typically specify file instead of url.

@omix
Copy link
Contributor

omix commented Apr 4, 2022

Concerning MediaPlayer:
Do you have any QML error code? Maybe the library has not been loaded properly.

@ITSweets
Copy link
Author

ITSweets commented Apr 4, 2022

There is no error message. Might be an IDEA problem. I don't know how to configure the output information. Like QtCreator, if a header file is missing, it will print on the console when compiling or running. IDEA doesn't seem to be able to do it, can you do it with the Eclipse you used before? If you can, please tell me how to configure it.

  1. How to load the library? multimedia coordinates are imported. Is there anything else that needs to be initialized or configured? Or do you test it?

@omix
Copy link
Contributor

omix commented Apr 4, 2022

How do you load QML? With QQmlApplicationEngine?

@ITSweets
Copy link
Author

ITSweets commented Apr 4, 2022

public class Application
{
public static void main(String[] args)
{

    QGuiApplication.initialize(args);

    var engine = new QQmlApplicationEngine();
    engine.load(QUrl.fromClassPath("qml/main.qml"));

    QGuiApplication.exec();
    QGuiApplication.shutdown();


}

}

It is written as in Qt.

@omix
Copy link
Contributor

omix commented Apr 4, 2022

Are there warnings?

engine.warnings.connect(w->System.out.println(w));

...to be put before engine.load().

@omix
Copy link
Contributor

omix commented Apr 4, 2022

I can confirm that I am able to hear music from a local file path but not from a classpath.
I'll try to find out why.

@omix
Copy link
Contributor

omix commented Apr 4, 2022

I suggest to use metaplayer's setSourceDevice method with QFile as device pointing to classpath resource.
I will have to adapt QtJambi to this specific application field to let mediaplayer work with classpath urls.
Next release...

@omix
Copy link
Contributor

omix commented Apr 4, 2022

Here is a solution:

for(QObject root : engine.rootObjects()) {
    QMediaPlayer player = root.findChild(QMediaPlayer.class, "", Qt.FindChildOption.FindChildrenRecursively);
    assert player!=null;
    QFile file = new QFile("classpath:/<path_to>.mp3");
    file.open(QIODevice.OpenModeFlag.ReadOnly);
    player.setSourceDevice(file);
    break;
}

@omix
Copy link
Contributor

omix commented Apr 4, 2022

Ok, so far I can say, that QMediaPlayer on Windows will not work with classpath source. It directly requests Windows to open the given URL. Windows does not look for classpath resources. The only way to use classpath resources is by using source device as shown above.

@omix
Copy link
Contributor

omix commented Apr 5, 2022

I was able to solve this issue.
In the next release you will be able to load classpath resources in the same manner as described for native Qt:

engine.load(new QUrl("qrc:qml/main.qml"));

Also in QML:

MediaPlayer {
     source: "qrc:/assets/sound/app.mp3"
}

The keyword classpath is no longer required for classpath resources:

QIcon icon = new QIcon(":/path/to/icons/icon.png");

@ITSweets
Copy link
Author

ITSweets commented Apr 5, 2022

OK, that's really good news. It's perfect to be able to solve this problem. The qrc keyword appears in your sample code, is he a wrapper for the classpth keyword?

@omix
Copy link
Contributor

omix commented Apr 5, 2022

No, that's Qt specific. If you write C++ code with Qt you can address resources embedded in your binary with a filepath ":/path" (see https://doc.qt.io/qt-6/resources.html).

//C++ example:
QFileInfo info(":resource/content.data");
QIcon icon(":images/icon.png");

In the context of QML you need to address these components via URL. Therefore, Qt allows you to use the "qrc" URL schema:

//C++ example:
QUrl url("qrc://resource/content.data");

Up to now neither ":/" files nor "qrc:/" URLs can be used to address classpath resources in QtJambi.
Next QtJambi version will also accept ":/" files and "qrc:/" URLs.

@ITSweets
Copy link
Author

Hi. Just saw that you responded to another issue. Is MediaPlayer in QML being fixed?

@omix
Copy link
Contributor

omix commented Apr 16, 2022

No, you will have to wait for next QtJambi release. I already fixed the solution but did not yet publish it. Sorry.

@omix
Copy link
Contributor

omix commented Apr 30, 2022

solved

@omix omix closed this as completed Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants