Results 1 to 9 of 9

Thread: Align text's in Qpainter

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Align text's in Qpainter

    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.


    Qt Code:
    1. class DrawText(QtGui.QWidget):
    2. def __init__(self,parent=None):
    3. QtGui.QWidget.__init__(self,parent)
    4. self.setGeometry(100,100,500,500)
    5. self.setWindowTitle("Draw Text")
    6. def paintEvent(self,event):
    7. paint = QtGui.QPainter()
    8. paint.begin(self)
    9. paint.setBrush(QtGui.QBrush(QtGui.QColor("blue")))
    10. paint.setPen(QtGui.QColor(100,20,0))
    11. paint.setFont(QtGui.QFont('Decorative',6))
    12.  
    13. pos_transform = QtGui.QTransform()
    14. pos_transform.translate(600, 500)
    15. paint.setTransform(pos_transform)
    16.  
    17.  
    18. paint.drawText(-525,-470,"1st line")
    19. paint.drawText(-519,-464,"Second line is longer")
    20. paint.drawText(-508,-475,"3rd line")
    21. paint.end()
    22.  
    23. app = QtGui.QApplication(sys.argv)
    24. dt = DrawText()
    25. dt.show()
    26. app.exec_()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Align text's in Qpainter

    Quote Originally Posted by sanku View Post
    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.

  3. #3
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Re: Align text's in Qpainter

    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
    Qt Code:
    1. paint.setFont(QtGui.QFont('Decorative',7))
    2. paint.setWindow(QtCore.QRect(-20,-20,30,50))
    3. paint.setViewport(-20,-20,70,100)
    4. ss=paint.boundingRect(-10,20,30,40,1,'ABCD')
    5. ss1=paint.boundingRect(10,25,40,50,1,'EFGHI')
    6. rr = paint.drawText(ss,1,'ABCD')
    7. rr1 = paint.drawText(ss1,1,'EFGHI')
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Align text's in Qpainter

    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?

    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class Widget : public QWidget {
    5. public:
    6. protected:
    7. void paintEvent(QPaintEvent *pe){
    8. QPainter p(this);
    9. QFontMetrics fm = fontMetrics();
    10. int lh = fm.lineSpacing();
    11. int currentHeight = lh;
    12. p.drawText(10, currentHeight, "Some text");
    13. currentHeight+=lh;
    14. p.drawText(10, currentHeight, "Some other text");
    15. currentHeight+=lh;
    16. p.drawText(10, currentHeight, "And some more text...");
    17. }
    18. };
    19.  
    20. int main(int argc, char **argv){
    21. QApplication app(argc, argv);
    22. Widget w;
    23. w.show();
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Align text's in Qpainter

    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
    Last edited by sanku; 16th April 2010 at 11:32. Reason: reformatted to look better

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Align text's in Qpainter

    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...

  7. #7
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Align text's in Qpainter

    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.

  8. #8
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Align text's in Qpainter

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Align text's in Qpainter

    Quote Originally Posted by sanku View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QPainter::drawText, draw big text
    By franco.amato in forum Newbie
    Replies: 3
    Last Post: 18th March 2010, 08:32
  2. Positioning Rich Text when using QPainter
    By millsks in forum Qt Programming
    Replies: 8
    Last Post: 23rd June 2009, 07:21
  3. Text rotation in QPainter
    By acpRobert in forum Newbie
    Replies: 1
    Last Post: 29th April 2009, 21:07
  4. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 19:59
  5. Rich text with QPainter?
    By sarefo in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2008, 14:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.