Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to pass a Drawable to a QML element?
Forum Updated to NodeBB v4.3 + New Features

How to pass a Drawable to a QML element?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
2 Posts 1 Posters 308 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a.burks
    wrote on 18 Jan 2020, 17:18 last edited by
    #1

    The requirement is to show Android app icons for a launcher. I tried the following JNI approach: First I transfer the Drawable to a Bitmap and from a Bitmapto a base64 encoded string:

    public static String drawableToBase64 (Drawable drawable) {
        Bitmap bitmap = null;
    
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                bitmap = bitmapDrawable.getBitmap();
            }
        }
    
        if (bitmap == null) {
            if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
                bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
            } else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
        }
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
    
        return Base64.encodeToString(imageBytes, Base64.DEFAULT);
    }
    

    In my QML file, I use the following paramter for the icon of a Button

    Button {
        icon.source: "data:image/png;base64," + base64string
        icon.width: parent.width * 0.35
        icon.height: parent.width * 0.35
        display: AbstractButton.TextUnderIcon
        ...
    }
    

    Some icons are working. For the most icons, I just can see a white square. I'm afraid converting images is a complex topic. Has someone a solution?

    Maybe I can write the Drawable into a temp directory and read them with a URL in the QML code.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      a.burks
      wrote on 19 Jan 2020, 21:24 last edited by
      #2

      I have the solution. I was only a single line. Everything was correct. I just had to add the following line to for the button:

      icon.color: "transparent"
      

      Crazy.

      1 Reply Last reply
      0

      2/2

      19 Jan 2020, 21:24

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved