Results 1 to 19 of 19

Thread: Drawing problem

  1. #1
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Drawing problem

    Hi all,

    I am using Qt 4 on linux. I am trying to support drag and drop to a triangle that is drawn using QPainter. I am using a QImage and drawing the triangle in it. But while dragging
    triangle the problem is we can drag the triangle even pressing the surrouding area. Can
    some body explain how to solve this problem? So that we can drag the triangle only when
    the mouse is only on triangle.

    I am attaching the source files too.
    Qt Code:
    1. //Here is the drawing stuff
    2. QImage image(90,90, QImage::Format_ARGB32_Premultiplied);
    3. image.fill(qRgba(0, 0, 0, 0));
    4.  
    5. QPainter pain;
    6. pain.begin(&image); //begin on image
    7.  
    8. pain.setRenderHint(QPainter::Antialiasing);
    9. pain.setRenderHint(QPainter::SmoothPixmapTransform);
    10. Qt::BrushStyle style = Qt::LinearGradientPattern;
    11.  
    12. QLinearGradient linearGradient(0, 0, 100, 100);
    13. linearGradient.setColorAt(0.0, Qt::white);
    14. linearGradient.setColorAt(0.2, Qt::green);
    15. linearGradient.setColorAt(1.0, Qt::black);
    16.  
    17. QPen pen;
    18. pen.setStyle(Qt::DotLine);
    19. pen.setColor(QColor( 0,255, 0));
    20. pain.setPen(QPen(Qt::black,0, Qt::SolidLine,Qt::FlatCap, Qt::MiterJoin));
    21. pain.setBrush(linearGradient);
    22. QPointF p1(80,40);
    23. QPointF p2(0,80);
    24. QPointF p3(80,80);
    25.  
    26. pain.drawPolygon(QPolygonF() << p1 << p2 << p3);
    27.  
    28.  
    29. setPixmap(QPixmap::fromImage(image));
    To copy to clipboard, switch view to plain text mode 




    Thanks in advance,
    prasanna Bhat
    --
    Attached Files Attached Files
    Last edited by jacek; 28th January 2007 at 11:12. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    That's a pure mathematical problem. You have to calculate whether the pressed point is inside the triangle. You could make use of QPainterPath::contains().

    Qt Code:
    1. void SomeWidget::mousePressEvent(QMouseEvent* event)
    2. {
    3. QPointF p1(80,40);
    4. QPointF p2(0,80);
    5. QPointF p3(80,80);
    6.  
    7. path << p1 << p2 << p3;
    8. if (path.contains(event->pos())
    9. {
    10. // is inside the triangle
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: Oh, and to make it more generic.. I'm not sure what would be a good way. If the triangle is filled with a color distinct from the background color, maybe simply accessing the corresponding pixel in the image and comparing it to the background would do the trick. Otherwise it starts to sound like a really complex task requiring identifying of the drawn shape in the image.
    Last edited by jpn; 22nd January 2007 at 10:39. Reason: updated contents
    J-P Nurmi

  3. #3
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: Drawing problem

    For a more generic solution (irregular shapes with many colors, for instance a person in a photograph) you can use a second image with the same dimensions with a shadow map, basically with every pixel that is part of a specific selection painted in a uniform color. When the drag starts, simple use the reported coordinates in your shadow map to look up the color (you could possibly have several distinct shapes and colors defined there) to identify which object to drag and drop.

    Andreas

  4. #4
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    QPainterPath path;
    path << p1 << p2 << p3;

    This stuff is not compiling it gives following error ??

    DragLabel::mousePressEvent(QMouseEvent*)':
    draglabel.cpp:49: parse error before `<<' token

    Is somthing wrong here??

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    Forgot #include <QPainterPath>?

    EDIT: Oops, sorry the piece of code was written on the fly. I meant:
    Qt Code:
    1. path.addPolygon(QPolygonF() << p1 << p2 << p3);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 22nd January 2007 at 13:07.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    boss_bhat (22nd January 2007)

  7. #6
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    Hi all,

    I am little bit of struggling here, can some body explain how to rotate the triangle in this sample on right mouse click, here is the code I have written in the :mousePressEvent of
    DragLabel class,

    Qt Code:
    1. QImage tempImage = (*pixmap()).toImage();
    2. if(event->button()==Qt::RightButton)
    3. {
    4. //rotate abt 60 degrees
    5. QMatrix matrix;
    6. matrix.rotate(60);
    7. tempImage = tempImage.transformed(matrix);
    8. QPainter painter;
    9. painter.begin(&tempImage);
    10. painter.fillRect(tempImage.rect(), QColor(127, 127, 127, 127));
    11. painter.end();
    12. drag->setPixmap(QPixmap::fromImage(tempImage));
    13. show();
    14. }
    15. else{
    16. hide();
    17.  
    18. if (drag->start(Qt::MoveAction) == Qt::MoveAction)
    19. close();
    20. else
    21. show();
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    It doesnt work, for multiple right clicks, Can somebody explain what is the right way to implement this

    Thanks in advance,
    Prasanna Bhat
    Last edited by jacek; 28th January 2007 at 11:11. Reason: missing [code] tags

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    As mentioned in some previous thread, I suggest you to store the original image. Well, check out the example.
    Attached Files Attached Files
    J-P Nurmi

  9. #8
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    hi all,

    I am bit struck here, the problem is when I use multiple triangles, assume one triangle overlaps the another say like in the picture, the lower triangle wont display properly as it's clipped by the QImage dimension of the upper triangle. Even I can't access the lower triangle exposed with drag and drop because the dimension of QImage of the top triangle is overlapping and even though the unpainted area is disabled I cant access exposed area of the lower triangle, Can some body explain how to solve this problem.

    Thanks in advance,
    Prasanna Bhat
    Attached Images Attached Images

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    Open up the triangle image in your favourite image editor application and make the white background transparent.
    J-P Nurmi

  11. #10
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    But I am drawing the triangle programatically

  12. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    Ah ..well, maybe you could fill the image with a transparent color after constructing it but before painting the triangle.
    J-P Nurmi

  13. #12
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    I tried,

    QImage image(80,80, QImage::Format_ARGB32_Premultiplied);
    //image.fill(qRgba(0, 0, 0, 0)); this also doesnt work
    image.fill(Qt::transparent);
    it didnt work, I am using Using Qt version 4.0.1 is there any difference?

  14. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    Could you test this?
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by jpn; 24th January 2007 at 17:43. Reason: attached a screenshot
    J-P Nurmi

  15. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing problem

    Just a thought....
    wont all this drawing be more easier if using Graphics View Framework ?? I mean making the triangle as QGraphicsItem ??

    just curious....what is the need that you are using QLabel ??

  16. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing problem

    Quote Originally Posted by aamer4yu View Post
    Just a thought....
    wont all this drawing be more easier if using Graphics View Framework ?? I mean making the triangle as QGraphicsItem ??

    just curious....what is the need that you are using QLabel ??
    Indeed, but GV was introduced in Qt 4.2 and he's using 4.0. Anyway, updating to a recent version would be highly recommended.
    J-P Nurmi

  17. The following user says thank you to jpn for this useful post:

    boss_bhat (25th January 2007)

  18. #16
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    I will try to upgrade to recent version of Qt!! By the way does QGraphicsItem support drag n drop??

    Thanks guyz

  19. #17
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing problem

    sure it does... its inbuilt !!
    u dont even need to write the drag & drop code unless u want to move items from one view to another...

    check the 40000 chips program in Qt Demo -> Demonstrations
    i bet u will be tempted to workin in the GV model

    check the attachment... so simple work
    Attached Files Attached Files
    Last edited by aamer4yu; 25th January 2007 at 11:11. Reason: added attachment

  20. #18
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing problem

    solved overlapping problem

    subclassing QPolygonGraphicsItem will work.
    Last edited by boss_bhat; 28th January 2007 at 11:16. Reason: missed passing polygonF to QPolygonGraphicsItem

  21. #19
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Drawing problem

    The mistake in the previous case was wrong boundingRect().
    Change it to return QRectF(0, 0, 80,80) and also defining member shape() as follows it works perfectly !!!

    Qt Code:
    1. QPainterPath Shapes::shape() const
    2. {
    3. path.moveTo(40,0);
    4. path.lineTo(0,80);
    5. path.lineTo(80,80);
    6. path.lineTo(40,0);
    7. return path;
    8. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. QPainterPath drawing problem
    By shad in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2007, 13:30
  2. qevent problem
    By amulya in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2006, 11:51
  3. drawing problem
    By boss_bhat in forum Newbie
    Replies: 1
    Last Post: 20th September 2006, 10:49
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.