Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700
My Carrier: Pelephone
Accepted Solution

Using QPainter on QImage

I am looking at ways of generating images at run-time. I have been informed that Cascades does not support user-defined fields (i.e., I would write a class implementing paint and layout logic). It was suggested that QPainter could be used to generate the image on a QImage and then convert the QImage to Image to be assigned to the image property of an ImageView.

 

In an attempt to do so, I have written code like this:

 

QVariant MyClass::image()
{
  QString str("Hi");


  QImage image(QSize(100,100), QImage::Format_RGB32);

  QPainter painter(&image);
  painter.fillRect(0, 0, 100, 100, Qt::white);

 

  painter.setPen(Qt::blue);
  painter.setFont(QFont("Arial", 30));

 

  //Causes Segmentation Fault!
  painter.drawText(0, 0, 100, 100, Qt::AlignCenter | Qt::AlignHCenter, str); 

 

  PixelBufferData pb(PixelBufferData::RGBX, 100, 100, 100, image.bits());

 

  QVariant var(QVariant::Image);
  var.setValue(Image(pb));

 

  return var;
}

 

Unfortunately, this generates a Segmentation Fault when it invokes drawText. Googling around reveals that others encounter this in the Qt world when using QPainter in the absence of a QApplication object.

 

So my question is: Can QPainter be used in a Cascades application? If so, are any samples available? If not, what would be the best way to generate images on the fly (e.g., a field which shows a bar chart based on data, etc.)

 

Thanks,

 

Jamie

 

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Using QPainter on QImage

First, you might want to read through http://supportforums.blackberry.com/t5/Cascades-Development/Cascades-Custom-Painting-amp-Alcoholism/...

 

There is no direct comparable to Canvas

QPainter is not fully supported.

I see you've already found PixelBufferData https://developer.blackberry.com/cascades/reference/bb__cascades__PixelBufferData.html

 

I'll play around with the flexibility of PixelBufferData.

In the meantime, have you discovered anything new?

 

Stuart

 

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Using QPainter on QImage

Have you investigated WebView with a JS canvas, or OpenGL?

 

Stuart

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Using QPainter on QImage

Stuart, I didn't think we had any WebKit support in Qt/Cascades yet, on the Alpha, nor in the KDAB Qt port for that matter.

What "WebView" are you proposing here that might be useful for this?

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Using QPainter on QImage

I was referring to:

https://developer.blackberry.com/cascades/reference/bb__cascades__WebView.html

 

I haven't tried this myself yet, so if you know more about it and its applicability, please let us know.

 

Stuart

Please use plain text.
Developer
jamiejulius
Posts: 49
Registered: ‎06-06-2012
My Device: Bold 9700
My Carrier: Pelephone

Re: Using QPainter on QImage

Thanks for the confirmation that QPainter is a "dead end" and for the WebView suggestion.

 

Glad to see that the need for custom painting has already been voiced clearly in other threads.

 

In probably every other framework I've seen in the past, the ability to paint is the foundation on which control classes are written. Just out of curiosity, how do the Cascade control classes implement their painting?

 

Jamie

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Using QPainter on QImage

I'm interested to hear about any successes with WebView.

I also am interested in how you fare if you pursue openGL, and PixmapBufferData.

 

As for internals, it's perhaps best to treat that as a black box. One of the goals is to ensure that graphics is always displayed in a timely fashion, so we can't assume the internals are simple or overly relevant for most of us.

 

Are you prepared to mark this thread closed by accepting a post as a solution, or is there more to explore here first?

 

Stuart

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Using QPainter on QImage

This isn't my code, but someone who ran it on an Alpha has confirmed it works in 10.0.4.197:

 

http://codepad.org/FSKmTKiV

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.