Results 1 to 10 of 10

Thread: QFrame inserting text

  1. #1
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default QFrame inserting text

    Hi

    I have a problem with inserting text into a Qframe.
    I have a Qframe where I draw some graphics. I want to
    insert some text into the graphics depending on their coordination.
    I dont know if it is possible to insert text into the qframe.

    Does someone have any idea's?

    Thanks in advance

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame inserting text

    How do you draw the graphics? In a paintEvent?
    Then you can use the QPainter::drawText functions.

    Anyway, why don't you use a graphics view?

  3. #3
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default Re: QFrame inserting text

    Quote Originally Posted by marcel View Post
    How do you draw the graphics? In a paintEvent?
    Then you can use the QPainter::drawText functions.

    Anyway, why don't you use a graphics view?

    yes in a paintevent
    thanks for the tip

    Anyway, why don't you use a graphics view?

    Nou the person who begun with this project had already chosen for qframe and we use qt3 but in future we will use qt4 and its going to be easier i hope.

  4. #4
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default Re: QFrame inserting text

    he

    i am back with my unsloved problem.

    look at this code

    Qt Code:
    1. int x = e->x();
    2. int y = e->y();
    3. text = QString("test: %1").arg(test->ID());
    4. QPainter* p = new QPainter;
    5. p->setBackgroundColor(white);
    6. p->drawText(x, y, text);
    To copy to clipboard, switch view to plain text mode 

    i do not need to draw the text in my paintEvent, i am doing this in the mousereleaseEvent. when i debug i get the correct information but it is not drawn in the frame. What am i doing wrong?

    thanks in advance

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame inserting text

    In Qt4 all widget painting must be done inside a paint event.

    In your code you don't initialize the painter wit ha paint device( e.g. this ). If you did initialized it with 'this', then it would have crashed.

    In mouse press/release, set the data that has to be painted somewhere in your class and call update(). In paint event check that data and paint it.

  6. #6
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default Re: QFrame inserting text

    he guys,

    Please can someone take a look at my problem?
    I am so stucked in this paint thing(i am new to qt).
    I have a situation which is related to mouseReleaseEvent and paintEvent. On right clicking on a graphic line on the screen, i have to draw then some text on that point.
    So i have here my functions:
    Qt Code:
    1. void testClass::mouseReleaseEvent(QMouseEvent *e )
    2. {
    3.  
    4. // myCode;
    5.  
    6. }
    7.  
    8. void testClass::paintEvent(QPaintEvent *e)
    9. {
    10. // myCode;
    11. Qpainter Paint(this);
    12. paintText(Paint, e);
    13. }
    To copy to clipboard, switch view to plain text mode 

    In the below function i try to right click on the chosen point in the screen and then get the text with ID() and draw it on that point. It compiles well but the there is going to be no draw on the screen, i dont see the text.

    Qt Code:
    1. void testClass::paintText (QPainter &painter, QEvent* e)
    2. {
    3.  
    4. me = (QMouseEvent*) e;
    5.  
    6. if (me->button() == Qt::RightButton)
    7. {
    8. int x = me->x();
    9. int y = me->y();
    10.  
    11. QString text;
    12. FPoint P = DevToWin(x,y);
    13.  
    14. Test *pTest = mp->Find(mi, P, mp->Sometthing->GetFilter());
    15. text = QString("%1").arg(pTest->ID());
    16.  
    17. if (pTest)
    18. {
    19. painter.drawText(x, y, text);
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Is there another way to do it?
    Can someone please help me?

    Thanks in advance!

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame inserting text

    This is not the way to do it...
    You can't be sure if a paintEvent coincides with a mouse click.

    Do what I asked you the first time.
    In a mouse press/release store the mouse position and the text you need to draw in a structure or a few variables. At the end of your mouse event, call an update(). This will post a paint event in the event queue and will be executed next time the event loop is gaining control.

    In paint event, draw the text at the mouse position specified in the vars/structure you set in the mouse event.

  8. The following user says thank you to marcel for this useful post:

    Pharell (5th October 2007)

  9. #8
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default Re: QFrame inserting text

    Quote Originally Posted by marcel View Post
    This is not the way to do it...
    You can't be sure if a paintEvent coincides with a mouse click.

    Do what I asked you the first time.
    In a mouse press/release store the mouse position and the text you need to draw in a structure or a few variables. At the end of your mouse event, call an update(). This will post a paint event in the event queue and will be executed next time the event loop is gaining control.

    In paint event, draw the text at the mouse position specified in the vars/structure you set in the mouse event.
    hey brother

    thanks 1000 times, it works perfectly now

  10. #9
    Join Date
    Sep 2007
    Posts
    22
    Thanks
    1

    Default Re: QFrame inserting text

    Hey guys,

    @Marcel

    I am back with another question. Now that i can draw the text on the given position, i see that when i scroll in the view, the text moves too. I just want to draw the text on the given position. Another problem is that when i choose to draw in another position that the first drawn text vanish from the screen. I can draw one text at a time. Can someone plz tell what am I doing wrong?

    thanks in advance!

  11. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame inserting text

    For the scrolling issue. Do you add the frame in a scroll area? The this is normal.
    To get the expected behavior then you either change your approach( QFrame, scroll area etc) and use a graphics view, or create a transparent overlay widget with the same size as the scroll area's viewport and draw the text on that widget.

    If you'd use a graphics view then you could draw the text in drawForeground and it will ignore any transformations/scrolling.

    As for the second issue, you need to store the strings in a list. Each time you need to draw a new string append it to the list and in paint event draw all the strings in the list. You will also need a list of coordinates, for each text.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  4. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02
  5. Problem with inserting text into QTextEdit
    By xorrr in forum Qt Programming
    Replies: 0
    Last Post: 6th February 2006, 11:45

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.