Results 1 to 10 of 10

Thread: QRubberBand class to draw a line

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default QRubberBand class to draw a line

    Hi how to use QRubberBand class to draw a line. According to docs, we can either draw a rectangle or a line. I'm able to draw a rectangle but not a line. Here is the sample code I have tried

    Qt Code:
    1. Myline::Myline( QWidget *parent, Qt::WFlags flags)
    2. : QWidget(parent, flags){
    3. RBand = 0;
    4. }
    5.  
    6. void Myline::mousePressEvent(QMouseEvent *event){
    7. first_pt = event->pos();
    8. last_pt = event->pos();
    9. if (!RBand)
    10. RBand = new QRubberBand(QRubberBand::Line, this);
    11. RBand->setGeometry(QRect(first_pt, last_pt));
    12. RBand->show();
    13. }
    14.  
    15. void Myline::mouseMoveEvent(QMouseEvent *event) {
    16. last_pt = event->pos();
    17. RBand->setGeometry(QRect(first_pt, last_pt));
    18. }
    19.  
    20. void Myline::mouseReleaseEvent(QMouseEvent *event) {
    21. last_pt = event->pos();
    22. RBand->hide();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by vermarajeev; 2nd April 2007 at 15:23.

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

    Default Re: QRubberBand class to draw a line

    What exactly happens? Do you see a rectangle?

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: QRubberBand class to draw a line

    Quote Originally Posted by wysota View Post
    What exactly happens? Do you see a rectangle?
    Yes I see a rectange...though I have given shape as a line.

  4. #4
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 11 Times in 11 Posts

    Default Re: QRubberBand class to draw a line

    Do you know, I suffered from a different problem where my QRubberBand drawn with QRubberBand:Rectangle always drew a filled rectangle, except when the widget style was set to "Plastique". So when I saw your post I looked up the documentation - I've replicated it below.

    enum QRubberBand::Shape
    This enum specifies what shape a QRubberBand should have. This is a drawing hint that is passed down to the style system, and can be interpreted by each QStyle.
    QRubberBand::Line (0)
    A QRubberBand can represent a vertical or horizontal line. Geometry is still given in rect() and the line will fill the given geometry on most styles.
    QRubberBand::Rectangle (1)
    A QRubberBand can represent a rectangle. Some styles will interpret this as a filled (often semi-transparent) rectangle, or a rectangular outline.

    Now from this, it looks to me that QRubberBand:Line is a drawing hint for how the rubber band box should be drawn - which seems a bit counter-intuitive with respect to naming. I've interpreted this one way and the OP another, and its not clear what the correct result is supposed to be - who would want just a vertical or a horizontal line? However, I'm going to try using "Line" in my code without the Plastique style and see if it gives the same result as he got (mine is overlaid onto an OpenCascade widget), because its the effect I actually wanted.
    It looks like the QRubberBand widget is designed to support a "window selection" UI metaphor and I suspect the OP wants a "dynamic line" response. That's probably better modelled with a click to start, click to end type interaction and a repaint of the window for each mouse move event with the new "line" added. Without seeing the actual paint event it is difficult to see how to optimise this (Bit-blits, XORs, layers, back buffers) etc. But I'm sure once we see this, the gurus will be able to figure out the best option

    Pete

  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QRubberBand class to draw a line

    What happens when you set height/width explicitly like this:

    RBand->setGeometry(first_pt.x(),0,1,parent->height());
    RBand->setGeometry(0,first_pt.y(),parent->width(),1);

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: QRubberBand class to draw a line

    If your line is horizontal or vertical you should use a QFrame instead (just like Qt Designer does) and if it not then you can always create a custom widget drawing a proper QLine on paintEvent... I guess it shouldn't be to hard.
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #7
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 11 Times in 11 Posts

    Default Re: QRubberBand class to draw a line

    Well I tried various combinations with some *very* wierd results - and my rubber band box is back to Rectangle with Plastique!

    Pete

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QRubberBand class to draw a line

    Quote Originally Posted by pdolbey View Post
    Well I tried various combinations with some *very* wierd results - and my rubber band box is back to Rectangle with Plastique!

    Pete
    This is strange. I am using platique and exactly the code lines I gave you. Works like a charm. Tested on Qt 4.2.3 + Qt 4.3.0beta on Linux and Qt 4.2.3 on Windows XP. However I am not sure about the style on Windows. Never bothered to look.
    .

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: QRubberBand class to draw a line

    Quote Originally Posted by Kumosan View Post
    What happens when you set height/width explicitly like this:

    RBand->setGeometry(first_pt.x(),0,1,parent->height());
    RBand->setGeometry(0,first_pt.y(),parent->width(),1);
    Can we see your code????

    If your line is horizontal or vertical you should use a QFrame instead (just like Qt Designer does) and if it not then you can always create a custom widget drawing a proper QLine on paintEvent... I guess it shouldn't be to hard.
    I have already done that just wanted to see if I can achieve the same result using QRubberBand class.
    Last edited by vermarajeev; 3rd April 2007 at 05:55.

  10. #10
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QRubberBand class to draw a line

    Quote Originally Posted by vermarajeev View Post
    Can we see your code????
    Sure.


    http://spectrascan.svn.sourceforge.n...pp?view=markup

Similar Threads

  1. QTextStream : Remove a Line?
    By kaydknight in forum Qt Programming
    Replies: 7
    Last Post: 31st January 2011, 19:15
  2. Draw rubberband line
    By Pang in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2007, 00:09
  3. Find and line endings
    By mikeh in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2006, 11:16
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  5. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 19:49

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.