PDA

View Full Version : Align text's in Qpainter



sanku
14th April 2010, 10:23
Hi

Sorry for the silly question if its, but I am really new to Qt Programming,
Below I am attaching a code in which I am drawing text using drawtext giving x and y co-ordinates for the Qpainter to paint, since these co-ordinates are taken from some other file in which the x and y values may be +ve or -ve, if negative values comes then its out of focus so I used some offset to transform (temporary one) as co-ordinates changes for different file it works fine but overwrites.

At present problem that I am facing is the distance between 2 text co-ordinates is small like 6pixels which overwrites on each other, how can I scale up co-ordinates depending on the widget area or is it there a way where I can look for bounding condition of the each text. There are other issues that text length may vary extra.
If someone give the guild lines as how to go about it, that would be great.
As this will be later done in graphicsscene because there will be mouseevent for each text item etc, at present I am not sure as how thing work around graphicsscene so testing in QPainter.




class DrawText(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setGeometry(100,100,500,500)
self.setWindowTitle("Draw Text")
def paintEvent(self,event):
paint = QtGui.QPainter()
paint.begin(self)
paint.setBrush(QtGui.QBrush(QtGui.QColor("blue")))
paint.setPen(QtGui.QColor(100,20,0))
paint.setFont(QtGui.QFont('Decorative',6))

pos_transform = QtGui.QTransform()
pos_transform.translate(600, 500)
paint.setTransform(pos_transform)


paint.drawText(-525,-470,"1st line")
paint.drawText(-519,-464,"Second line is longer")
paint.drawText(-508,-475,"3rd line")
paint.end()

app = QtGui.QApplication(sys.argv)
dt = DrawText()
dt.show()
app.exec_()

wysota
14th April 2010, 11:21
At present problem that I am facing is the distance between 2 text co-ordinates is small like 6pixels which overwrites on each other, how can I scale up co-ordinates depending on the widget area
You can use QPainter::setViewport() and QPainter::setWindow() to scale coordinates.


or is it there a way where I can look for bounding condition of the each text.
Yes, you can do that. QPainter::drawText() has variants that store the bounding rect of the text in a variable you pass to them (look for the "boundingRect" parameter of drawText()). There is also QFontMetrics that can tell you the bounding rectangle of a piece of text.


As this will be later done in graphicsscene because there will be mouseevent for each text item etc,
You don't need GraphicsView for that, you can do it with plain widgets as well.

at present I am not sure as how thing work around graphicsscene so testing in QPainter.
The two technologies differ greatly when it comes to dealing with components (especially handling coordinates) so I suggest you choose one of them at the beggining and stick with it.

sanku
15th April 2010, 20:35
Thank you wysota for replying, If you could explain with small piece of code it would be clear to be,
I have qt 4.3.4 installed on my system I tried something like this in the code


paint.setFont(QtGui.QFont('Decorative',7))
paint.setWindow(QtCore.QRect(-20,-20,30,50))
paint.setViewport(-20,-20,70,100)
ss=paint.boundingRect(-10,20,30,40,1,'ABCD')
ss1=paint.boundingRect(10,25,40,50,1,'EFGHI')
rr = paint.drawText(ss,1,'ABCD')
rr1 = paint.drawText(ss1,1,'EFGHI')


Since one of co-ordinates are negative, I have set setWindow to negative which displayed the text but still the overlapping of text on each other exist, if I decrease the width and height to 10,10 instead of 30,50 in setWindow the text overlaps but coordinates changes but along with this, the text font sizes also increase.

How can I make font size to be same as before, but both the text to be layout with some visible distance.

2.But I wanted to use graphicsScrene because these text will be enable to events (mouseevent or doubleclick) to edit some of the feature related to text. can we do the same using QPainter?

Thank you
harsha

wysota
15th April 2010, 20:48
I don't know what the values represent so it is hard for me to suggest anything... To be honest I'm not sure what you want to obtain. Maybe something like this (C++, sorry, my Python is a bit rusty...) would be ok?


#include <QtGui>


class Widget : public QWidget {
public:
protected:
void paintEvent(QPaintEvent *pe){
QPainter p(this);
QFontMetrics fm = fontMetrics();
int lh = fm.lineSpacing();
int currentHeight = lh;
p.drawText(10, currentHeight, "Some text");
currentHeight+=lh;
p.drawText(10, currentHeight, "Some other text");
currentHeight+=lh;
p.drawText(10, currentHeight, "And some more text...");
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}

As I said, GraphicsView and regular custom widget based approach differ greatly. Pick one (depending on what you want to obtain) and stick with it as switching from one to the other will require throwing all code you have written into /dev/null.

sanku
16th April 2010, 11:22
Thank you for the reply, yes some what the same behviour is what I wanted.

I have read text, x and y co-ordinates from the file, which needs to put up on QPainter.
Assume
paint.drawText(-525,-470,"1rw2co")
paint.drawText(-519,-474,"2rw2co")
paint.drawText(-508,-475,"2rw3co")
paint.drawText(-519,-478,"3rw2co")
paint.drawText(-530,-470,"1rw1co")
paint.drawText(-513,-470,"1rw3co")
paint.drawText(-508,-470,"qrw4co")
paint.drawText(-508,-478,"3rw3co")
paint.drawText(-530,-478,"3rw1co")
paint.drawText(-530,-474,"2rw1co")

rw = row and co= column and I am applying a transform since its -ve value

1st row has 4 column, 2nd and 3rd row has 3 columns

The way I though will work out is sort out all text first by y-axis and then with in the same y-axis, sort x-axis
So if we create a matrix then
coordinates , Type of space
(0 ,0),no space
(0, 1),width space
(0, 2),width space
(0, X),width space

(1 ,0),height space
(1, 1),height & width space
(1, 2),height & width space
(1, X),height & width space

Is there any other automatic way of doing this?

Hope I have not confused

Thanks

wysota
16th April 2010, 11:36
I have completely no idea what you want to do. You have some coordinates you wish to preserve but at the same time you want to have different coordinates so that they don't overlap, there is some matrix and some spaces... Doesn't make much sense...

sanku
20th April 2010, 12:02
Sorry for the late reply,
yes this is what I am looking for. There should be some scaling happening where all the co-ordinates equally gets some spaces so that there is no overlap. I tried searching if there is any readily avaiable for doing this which I did not find (scale function from Qtransform scaled the font size also), so I am trying to write a small code where I am trying to calculate the scaling factor for x and y co-ordinates to apply to them.

sanku
23rd April 2010, 06:35
Hi
I wanted to know if there is way to scale the co-ordinates but at the same time fix the font size.
I read few posting regarding this topic but I could not get through this.

Thanking you

wysota
23rd April 2010, 09:29
I wanted to know if there is way to scale the co-ordinates but at the same time fix the font size.
Not automatically. You'd have to do the calculations manually but bear in mind QWidget operates in integers. You could do all that with Graphics View though.