Any idea on how to erase a portion of a bitmap just like Android's PorterDuff Mode?
I am creating an application like Paint, and I don't know how to erase the drawings I have written using the pen.
Any idea regarding this one?
Thank you!
I suggest you use the QPainter class which can perform various drawing operations on a QBitmap (more precisely: it draws on a QPaintDevice, from which QBitmap derives).
Among the various operations of the painter, there is QPainter::eraseRect() which can erase a portion of a QBitmap.
This is the way you use it:
QBitmap b;
QPainter p( &b );
p.eraseRect( x, y, w, h ); // With x, y, w and h defining the portion
// of your bitmap you want to erase
Related
I'm trying to use QPainter to draw items onto a QImage , but since I can't predict the exact size of this QImage , I can't use QImage::save() , it always tell me:
QPainter::begin: Paint device returned engine == 0, type: 3
But if I specify the image height and width when declaring this QImage , it works smoothly:
QImage output = QImage (500 , 500 , QImage::Format_ARGB32);
QImage, QPixmap, etc. require data to be allocated before drawing can begin. Using the default constructor of QImage does not allocate any memory, so image.isNull() == true. So when you call QPainter::begin() (probably indirectly by creating one with the QImage as the paint device) it can't draw into any memory because it isn't there.
From the QPainter::begin() docs:
QPixmap image(0, 0);
painter->begin(&image); // impossible - image.isNull() == true;
So you have to come up with a size before drawing. In your situation, the best thing to do would be to decide on a maximum size (or calculate one if feasible), and then once you do know the exact size - crop the image.
Alternatively, you can draw on a QGraphicsScene which will expand automatically as you add items on it, then save only the painted area given by QGraphicsScene::itemsBoundingRect():
QGraphicsScene scene;
scene.addItem(...);
QImage image(scene.itemsBoundingRect().size(), QImage::Format_ARGB32);
QPainter painter(&image);
scene.render(&painter, image.rect(), scene.itemsBoundingRect());
I have an x, y, z axis in the lower left-hand corner to give the user an idea of orientation. I want to label these axes with the appropriate x, y, and z labels.
I know that I don't want to use Glut to accomplish this since it's old and outdated, and was wondering if I could leverage QPainter to help label these axes.
NOTE: I've seen this, but found it too much: http://nehe.gamedev.net/tutorial/bitmap_fonts/17002/
You can use QPainter to draw text on any paint device. A QGLWidget is such a paint device; QPainter will then use OpenGL to draw the text (or whatever you want to draw). Guessing you are using a QGLWidget to draw your scene, you can just put the QPainter stuff at the end of your paintEvent:
MyGLWidget::paintEvent(QPaintEvent *event)
{
// draw OpenGL scene
// ...
// draw labels:
QPainter p(this);
p.drawText(..., ..., "X");
p.drawText(..., ..., "Y");
p.drawText(..., ..., "Z");
p.end();
}
Also have a look at the Qt OpenGL Overpainting Example.
Because (you said) you don't have access to QPainter for some reason, you need some kind of font rendering library for OpenGL. You could use FTGL or you can make your own renderer using freetype 2. Starting with Qt version 4.8.1 it should be possible to access individual glyphs using QRawFont (without having to directly deal with freetype), which can make implementing your own font rendering routines even easier.
If your object is QPaintDevice or QGLWidget, then use QPainter. It might be slower than traditional texture font, but it'll save you (from) another coding headache.
Is there an easy way to get rid of tiling when using a QBrush with texture?
QImage* texture = CreateQImage(); // create texture
QBrush* brush = new QBrush(*texture); // create texture brush
QPainter* painter = CreateQPainter(); // create painter
painter->fillRectangle(0, 0, 500, 500, *brush);
Suppose we have a QImage texture with size of 20x20 pixels. The code above will tile this texture all across the rectangle being filled. Is there an easy way to draw only a single instance of this texture? The QBrush usage is crucial.
Theoretically, I could reload every fill and draw method of the QPainter that takes a QBrush as input and use a QPainter.drawImage() method, but I think there must be a simplier way.
Thanks, Tony.
I don't think there is (see Qt::BrushStyle - the only style with a texture tiles it), and it wouldn't really make sens IMO. If you just want one image, use the drawImage functions as you've stated.
(One of the problems with not tiling is: what do you fill the rest of the rectangle with? Nothing? Some default background color? Some other QBrush attribute?)
I'm using Qt 4.7 QPainter to draw some polygons, etc into a widget. I am hoping to alter the coordinate system so that (0,0) is at the center of my widget, and the x/y axis behave in a standard "Cartesian" way (ie. y increases going "up" and decreases going "down"). In other words, I want the coordinates to be "math"-like not "computer graphics"-like, if you know what I mean. :-)
I'm trying to do this using setViewport() and setWindow() rather than do the math myself, as it would be nice to be able to just call the draw methods directly with my coordinates.
Here's what I've got so far:
// Setup coordinates
double screenWidth = width();
double screenHeight = height();
double windowWidth = 100.0;
double windowHeight = (screenHeight / screenWidth) * windowWidth;
painter.setViewport(0, 0, screenWidth, screenHeight);
painter.setWindow(-(windowWidth / 2.0), -(windowHeight / 2.0), windowWidth, windowHeight);
// Draw stuff
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawRect(-10, -10, 20, 20);
Now this works just fine, in that it draws a nice blue square in the middle of the screen. The problem is, I have to say that the upper left corner is (-10, -10). I'd like to be able to make it (-10, 10), as that is what it would be in Cartesian coords.
I tried messing with setWindow/setViewport to get this "y-axis flip", but to no avail. This seems like a really easy/basic thing to do, but after scouring the Qt docs and the web, I can't figure it out!
Thanks,
Chris
Use class QMatrix. It specifies 2D transformations. QMatrix is set to QPainter.
But remember, in your case, if you convert your widget's coords to Cartesian coords, you will have to put first point at (-10,-10) (not at (-10,10) as you did mentioned) to draw a rect, which has center at (0,0), because Y-axis now grows up and X-Axis now grows right.
All you need is to transform your coord system this way:
translate origin from (0,0) to the middle of the widget:
scale Y-axis by -1 factor:
Here is the code, typed in paintEvent() function of a widget:
QPainter pn( this );
int w_2 = width() / 2;
int h_2 = height() / 2;
{ // X- and Y-Axis drawing
pn.setPen( Qt::blue );
pn.drawLine( 0, h_2, width(), h_2); // X-Axis
pn.drawLine( w_2, 0 , w_2, height() ); // Y-Axis
}
QMatrix m;
m.translate( w_2, h_2 );
m.scale( 1, -1 );
pn.setMatrix( m );
pn.setPen( Qt::NoPen );
pn.setBrush( QBrush( Qt::blue, Qt::Dense4Pattern ) );
pn.drawRect( -10, -10, 20, 20 );
result:
update apr 07, 2014
This question was asked a long time ago and many things have changed since. For those asking themselves the same question today (beginnings of 2014) then my personal answer is that since Qt 4.3 it is possible to solve problem with text flipping more easier.
You are right. Text also gets filpped because it is drawn with the same painter. You can draw text at the end, when all flipped drawings are done, if it is possible. This method is not convinient because of new calculations of texts position. Also you will need to drop settings for painter.
Now I would recommend you to use QGraphicsView, because of huge support of 2D painting. Also for each QGraphicsItem ItemIgnoresTransformations flag can be set, which allows it to ignore inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the graphics view is transformed
The above answer will also flip text, "p" will be "b". To avoid that you have to flip back the y-axis before text is drawn, and you have to change sign on y-coord for the text position when you draw it. This is a little bit ugly I think, or is there a better way?
As stated above, drawing text also appears flipped upside down. There is an easy solution to it, see below. We will temporary disable the world transform for the text drawing. Note that text is not scaled anymore.
in your painting code we want to draw text on coordinate QPointF P;
Painter pn( this );
// calculate the point with the transform
QPointF p = pm.transform().map(P);
// Disable Transform temporary
pn.setWorldMatrixEnabled(false);
// draw it ordinary, no scaling etc
pn.drawText(p, QString("HI FRIENDS!"));
// Enable the transform again
pn.setWorldMatrixEnabled(true);
I needed to flip the y-axis in order to paint lines and polygons using Qt from points defined in Java coordinates. I imagine others will need to do this in porting from Java to Qt coordinate systems. The discussion above was helpful. My solution was:
painter.translate(0,height());
painter.scale(1.0, -1.0);
and then proceed to draw the lines and polygons.
i am not really newbie in Qt, but there are a few things i don't know...
I am programming in Python, but feel free to post your answers in ANY language.
So, i have a few QGraphicsItem (s), positioned inside a QGraphicsScene, the scene is viewed with a normal QGraphicsView. Everything is normal.
My scene is very large, 10,000 x 10,000 pixels, all graphic items are scattered around.
For example :
# Creating Scene object.
scene = QtGui.QGraphicsScene()
scene.setSceneRect(0, 0, 10000, 10000)
# Creating View object.
view = QtGui.QGraphicsView()
view.setScene(scene)
# Adding some objects to scene.
# scene.addItem(...)
# ...
# The list of items.
items = scene.items()
# This is how i center on item.
view.centerOn(some_item)
view.fitInView(some_item, Qt.KeepAspectRatio)
My question is, how can i center the view on every item, using something similar to centerOn, but smoothly ?
Currently, centerOn goes FAST on next item, i want to move it slooowly, maybe using QPropertyAnimation with easing curve ?
I tried to move the view to the next item using view.translate(1, 1) in a big cicle, but the movement is too fast, just like centerOn.
I tried to put some waiting with time.sleep(0.01) between the translating, but the windows blocks untill the cicle exists... so it's bad.
Thank you very much !
I once used a QTimeLine (with EaseInOutCurve), connected it to a slot, and then used that value to translate the view rect, like this:
const QRectF r = ...calculate rect translated by timeline value and trajectory...
view->setSceneRect( r );
view->fitInView( r, Qt::KeepAspectRatio );
For the trajectory I used a QLineF with start and end position for the smooth scrolling.
Then one can use the value emitted by timeline nicely with QLineF::pointAt().
In my case I needed to set both SceneRect and fitInView to make it behave like I wanted.
I solved my problem by placing a high value on setSceneRect. Then I centralize the scene on an object or position.
example:
this->ui->graphicsView->setSceneRect (0,0,100000000, 100000000);
this->ui->graphicsView->centerOn(500,1030);
With the setSceneRect size GraphicsView does not work right.