Results 1 to 11 of 11

Thread: foreground problem

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default foreground problem

    Hi all,
    I have a QGraphicsView inherited class who has a background, mouse overrided to allow pan on right click.
    The Drag mode is set to RubberDrag to allow selection using rectangle on left click.
    Mouse wheel is overrided to allow zoom who is just a change of Transform.
    The problem is on the foreground, when I paint a text the size change because of zoom.
    Another problem is this foreground is painted multiple time, normally it should just be painted on the bottom right of the window.
    For example, the problem is visible when a selection rectangle is added using left click.
    Here the code of the foreground :
    Qt Code:
    1. void CEditorNodeView::drawForeground( QPainter* painter, const QRectF& rect )
    2. {
    3. QFont Font( "Arial", 20 );
    4. QFontMetrics FontMetrics( Font );
    5. painter->setPen( QColor( 128, 128, 128, 128 ) );
    6. painter->setFont( Font );
    7. const int X = rect.right();
    8. const int Y = rect.bottom();
    9. const int Width = FontMetrics.width( m_Name );
    10. const int Height = FontMetrics.height();
    11. painter->drawText( X - Width - 5, Y - Height, Width, Height, Qt::AlignCenter, m_Name );
    12. }
    To copy to clipboard, switch view to plain text mode 
    If I add update() at the end of the function the problem is not visible but that mean that need update all the viewport always.
    To use this hack NoViewportUpdate need to be set too to not update multiple time.
    One solution for this problem ?
    Thanks for the help

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

    Default Re: foreground problem

    What exactly is the problem? If you mean that the text size changes, this is because the foreground is drawn is scene coordinates. If you want the size to remain fixed then either invert the world transformation of the view or simply reimplement paintEvent for the view, call the base class implementation and then do your own drawing in widget coordinates.
    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.


  3. #3
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: foreground problem

    Using :
    painter->setTransform( painter->transform() * transform().inverted() );
    The scaling is the same but translation is bad then when zoom, surely that end to identity matrix.
    About the prob of paint multiple time, here a screenshot who shows the problem (using drawForeground) :
    http://uppix.com/f-ForegroundProble533820680015e692.png
    Adding update() at the end of drawForeground only the bottom-right text is visible who is the correct result.

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

    Default Re: foreground problem

    I still fail to understand what is the problem you are having. If you have only one view on the scene then an answer could be to add another item to the scene with ItemIgnoresTransformations flag set.
    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.


  5. #5
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: foreground problem

    The problem is only this one is wanted :
    http://uppix.com/f-ForegroundProble53383cd00015e6e0.png
    drawForeground should paint only one I guess, so have 2 painted is bad behavior.

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

    Default Re: foreground problem

    And what is the code responsible for painting that? Could you post a minimal compilable example reproducing the problem?
    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.


  7. #7
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: foreground problem

    The text is painted on the bottom-right, use left click to have the selection rectangle.
    You will see the the text is painted on the bottom of the rectangle and stay at the end.
    The same problem is visible when you move the cursor on QGraphicsItem of the scene.
    Add update() at the end of drawForeground and you will see the problem will go away.
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5.  
    6. class CustomGraphicsView : public QGraphicsView
    7. {
    8. public:
    9.  
    10. CustomGraphicsView(QGraphicsScene* Scene, QWidget* Parent = 0) :
    11. QGraphicsView(Scene, Parent)
    12. {
    13. setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    14. setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    15. setRenderHint( QPainter::Antialiasing );
    16. setDragMode( DragMode::RubberBandDrag );
    17. }
    18.  
    19. void drawForeground( QPainter* painter, const QRectF& rect )
    20. {
    21. QFont Font( "Arial", 20 );
    22. QFontMetrics FontMetrics( Font );
    23. painter->setPen( QColor( 128, 128, 128, 128 ) );
    24. painter->setFont( Font );
    25. const int X = rect.right();
    26. const int Y = rect.bottom();
    27. const int Width = FontMetrics.width( "Name" );
    28. const int Height = FontMetrics.height();
    29. painter->drawText( X - Width - 5, Y - Height, Width, Height, Qt::AlignCenter, "Name" );
    30. }
    31. };
    32.  
    33. class MainWindow : public QMainWindow
    34. {
    35. public:
    36.  
    37. MainWindow(QWidget* parent = 0) :
    38. QMainWindow(parent)
    39. {
    40. QGraphicsScene* Scene = new QGraphicsScene(this);
    41. Scene->setSceneRect(-DBL_MAX * 0.5f, -DBL_MAX * 0.5f, +DBL_MAX, +DBL_MAX);
    42. setCentralWidget(new CustomGraphicsView(Scene, this));
    43. }
    44. };
    45.  
    46. int main(int argc, char *argv[])
    47. {
    48. QApplication a(argc, argv);
    49. MainWindow w;
    50. w.show();
    51. return a.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: foreground problem

    This is my code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5. #include <limits.h>
    6. #include <values.h>
    7.  
    8. class CustomGraphicsView : public QGraphicsView
    9. {
    10. public:
    11.  
    12. CustomGraphicsView(QGraphicsScene* Scene, QWidget* Parent = 0) :
    13. QGraphicsView(Scene, Parent)
    14. {
    15. setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    16. setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    17. setRenderHint( QPainter::Antialiasing );
    18. setDragMode( QGraphicsView::RubberBandDrag );
    19. }
    20.  
    21. void drawForeground( QPainter* painter, const QRectF& rect )
    22. {
    23. QFont Font( "Arial", 20 );
    24. QFontMetrics FontMetrics( Font );
    25. painter->setPen( QColor( 128, 128, 128, 128 ) );
    26. painter->setFont( Font );
    27. const int X = rect.right();
    28. const int Y = rect.bottom();
    29. const int Width = FontMetrics.width( "Name" );
    30. const int Height = FontMetrics.height();
    31. painter->drawText( X - Width - 5, Y - Height, Width, Height, Qt::AlignCenter, "Name" );
    32. }
    33. };
    34.  
    35. class MainWindow : public QMainWindow
    36. {
    37. public:
    38.  
    39. MainWindow(QWidget* parent = 0) :
    40. QMainWindow(parent)
    41. {
    42. QGraphicsScene* Scene = new QGraphicsScene(this);
    43. Scene->setSceneRect(-DBL_MAX * 0.5f, -DBL_MAX * 0.5f, +DBL_MAX, +DBL_MAX);
    44. setCentralWidget(new CustomGraphicsView(Scene, this));
    45. }
    46. };
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication a(argc, argv);
    51. MainWindow w;
    52. w.show();
    53. return a.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    And this is the result I get:

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


  9. #9
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: foreground problem

    Yea the code you use is the same I showed, the problem is when you click left to show the rectangle of selection or cursor on item.
    The problem is that will repaint "NAME" on all that, the wanted behavior is to only have it on bottom right.
    The problem was solved by J-P Nurmi on Qt BugReports :
    But still the problem of scaling of font when zooming.
    Qt Code:
    1. void drawForeground( QPainter* painter, const QRectF& )
    2. {
    3. QFont Font( "Arial", 20 );
    4. QFontMetrics FontMetrics( Font );
    5. painter->setPen( QColor( 128, 128, 128, 128 ) );
    6. painter->setFont( Font );
    7. QRectF r = rect();
    8. const int X = r.right()- 20;
    9. const int Y = r.bottom() - 20;
    10. const int Width = FontMetrics.width( "Name" );
    11. const int Height = FontMetrics.height();
    12. painter->drawText( QRectF(mapToScene(X - Width - 5, Y - Height), QSizeF(Width, Height)), Qt::AlignCenter, "Name" );
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Alundra; 1st April 2014 at 14:09.

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

    Default Re: foreground problem

    The problem with your code is that you are not even checking where you are supposed to draw. The rect you get is not the whole scene rect but rather the rect that graphics view asks you to redraw. Regarding font scaling, if you manage to demonstrate the problem to us using code, maybe we can provide a fix. Currently the code you have does not take zooming into account, so the larger the zoom, the larger the text you'll get. As I said before, you'd have to invert the transform so that the original matrix is used or reimplement paintEvent and draw in widget coordinates:

    Qt Code:
    1. class GV : public QGraphicsView {
    2. public:
    3. GV(QWidget *parent = 0) : QGraphicsView(parent) {}
    4. void setName(const QString &n) { m_name = n; update(); }
    5. protected:
    6. void paintEvent(QPaintEvent *pe) {
    7. QGraphicsView::paintEvent(pe);
    8. QPainter p(viewport());
    9. QFont f("Arial", 20);
    10. p.setFont(f);
    11. p.drawText(viewport()->rect(), Qt::AlignRight|Qt::AlignBottom, m_name);
    12. }
    13. private:
    14. QString m_name;
    15. };
    To copy to clipboard, switch view to plain text mode 
    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.


  11. #11
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: foreground problem

    It's weird that using inverted matrix I don't have the good result but using ResetTransform that works :
    Qt Code:
    1. void CEditorNodeView::drawForeground( QPainter* painter, const QRectF& )
    2. {
    3. painter->resetTransform();
    4. const QFont Font( "Arial", 20 );
    5. painter->setPen( QColor( 128, 128, 128, 128 ) );
    6. painter->setFont( Font );
    7. painter->drawText(viewport()->rect(), Qt::AlignRight | Qt::AlignBottom, "ANIMATION");
    8. }
    To copy to clipboard, switch view to plain text mode 
    That end to the same as paintEvent like that, maybe the paintEvent method is faster because no reset needed ?
    I have to do :
    Qt Code:
    1. const QRectF r = rect();
    2. const int X = r.right() - 4;
    3. const int Y = r.bottom();
    To copy to clipboard, switch view to plain text mode 
    I have to offset it to show in good position, maybe a question of border ? A way exist to do that auto ?
    Last edited by Alundra; 1st April 2014 at 15:29.

Similar Threads

  1. Qpainter foreground
    By hassinoss in forum Qt Programming
    Replies: 8
    Last Post: 13th March 2014, 11:32
  2. GraphicsView Foreground
    By augusbas in forum Qt Programming
    Replies: 11
    Last Post: 23rd June 2011, 12:06
  3. QGraphicsView Draw Foreground
    By augusbas in forum Qt Programming
    Replies: 4
    Last Post: 22nd June 2011, 13:01
  4. How to keep window in foreground
    By szicsi in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2008, 14:40
  5. Painting: How to control what's in the foreground?
    By Mister_Crac in forum Qt Programming
    Replies: 16
    Last Post: 8th May 2007, 13:00

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
  •  
Qt is a trademark of The Qt Company.