Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: setRasterOp Equivalent

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

    Default setRasterOp Equivalent

    Hi guys,

    What is the equivalent of setRasterOp( NotXorROP ) in Qt4?? I cant see any alternative defined in qtAssistance.

    Thanks

  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: setRasterOp Equivalent

    I think currently there is no equivalent. Use of such thing would be highly limited as Qt4 forbids painting outside a paint event.

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

    Default Re: setRasterOp Equivalent

    Quote Originally Posted by wysota View Post
    I think currently there is no equivalent. Use of such thing would be highly limited as Qt4 forbids painting outside a paint event.
    Then I'll have tough time porting to qt4 . In my application there are many places where I have used setRasterOp, so how should I solve the problem????

    Qt4 forbids painting outside a paint event
    If qt4 doesnt support setRasterOp, what about projects whose functionality is completely based on QPainter and setRasterOp. It will be a tough time for guy like me who is given tasks to port to qt4.

    Is there a way I can solve above problem of setRasterOp with minimun code changes???. Without this my application looks very odd....

    Please help!!!!!

  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: setRasterOp Equivalent

    You only have to modify your paintEvent code to apply the changes you'd do using the raster operation.

  5. #5
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,313
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setRasterOp Equivalent

    Raster operations are history with Qt4. They don't fit into the graphic pipeline of Qt4, so say good bye to them.

    But what did you do with the raster operations ?
    The most common use case was the XOR mode to implement rubberbands, crosshairs ... without having to repaint the content below. With Qt4 you always have to repaint the content below - and yes this is sometimes too slow for interactive operations, f.e for a remote X session over a line with a limited bandwidth.

    You might have a look at http://qwt.sourceforge.net/class_qwt_picker.html. QwtPicker is implemented as a masked widget, that displays a rubberband on top of your widget. Using it you can implement rubberband supported selections in a couple of lines.

    Uwe

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

    Default Re: setRasterOp Equivalent

    Here is my sample code. How do I solve the setRasterOp problem

    //.h
    Qt Code:
    1. class setRasterOpTest : public QFrame
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. setRasterOpTest(QWidget *parent = 0, Qt::WFlags flags = 0);
    7. ~setRasterOpTest();
    8. private:
    9. Ui::setRasterOpTestClass ui;
    10. QPoint firstPoint;
    11. QPoint oldPoint;
    12. bool prevRubberband;
    13. protected:
    14. void mousePressEvent( QMouseEvent *e );
    15. void mouseReleaseEvent( QMouseEvent *e );
    16. void mouseMoveEvent( QMouseEvent *e );
    17. void paintEvent( QPaintEvent *e );
    18. };
    To copy to clipboard, switch view to plain text mode 

    //.cpp
    Qt Code:
    1. #include "setrasteroptest.h"
    2. #include <QMouseEvent>
    3. #include <QPainter>
    4.  
    5. //Program to draw a straight line using Qt4
    6. //constructor
    7. setRasterOpTest::setRasterOpTest(QWidget *parent, Qt::WFlags flags)
    8. : QFrame(parent, flags)
    9. {
    10. ui.setupUi(this);
    11. QColor color( Qt::white );
    12. QPalette palette;
    13. palette.setColor(backgroundRole(), color);
    14. setPalette(palette);
    15. setMouseTracking(true);
    16. }
    17.  
    18. setRasterOpTest::~setRasterOpTest()
    19. {}
    20.  
    21. //Press mouse and remember firstPoint and oldPoint
    22. void setRasterOpTest::mousePressEvent( QMouseEvent *e )
    23. {
    24. if (e->button() == Qt::LeftButton) {
    25. firstPoint = e->pos();
    26. oldPoint = e->pos();
    27. }
    28. }
    29.  
    30. //release mouse function
    31. //If mouse is released on left button pressed
    32. //draw a line in red color to whever the mouse is released
    33. void setRasterOpTest::mouseReleaseEvent( QMouseEvent *e )
    34. {
    35. if (e->button() == Qt::LeftButton ) {
    36. QPainter paint;
    37. paint.begin( this );
    38. paint.setPen(QPen(Qt::red, 2));
    39. oldPoint = e->pos();
    40. paint.drawLine(firstPoint, oldPoint);
    41. paint.end();
    42. update();
    43. }
    44. }
    45.  
    46. //mouse move event
    47. //Perform rubber banding
    48. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e )
    49. {
    50. //draw the line in xor mode (rubberbanding)
    51. QPainter painter;
    52. painter.begin( this );
    53. painter.setPen(QPen(Qt::black, 2));
    54.  
    55. //setRasterOp not defined in Qt4.
    56. //painter.setRasterOp (NotXorROP);
    57. // first draw over the previously drawn line and
    58. // erase it (in NotXorROP mode)
    59. if(prevRubberband){
    60. painter.drawLine(firstPoint, oldPoint );
    61. }
    62. oldPoint = e->pos();
    63. painter.drawLine( firstPoint, oldPoint );
    64. painter.end();
    65. prevRubberband = true;
    66. update();
    67. }
    68. void setRasterOpTest::paintEvent( QPaintEvent *e )
    69. {
    70. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your understanding

  7. #7
    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: setRasterOp Equivalent

    I think the code is broken as calling update() from mouseMove when using a raster op would not make any sense.... Anyway:

    Qt Code:
    1. void setRasterOpTest::mousePressEvent *e){
    2. m_firstpt = e->pos();
    3. m_lastpt = e->pos();
    4. }
    5. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e )
    6. {
    7. m_lastpt = e->pos();
    8. update();
    9. }
    10. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    11. m_firstpt = QPoint();
    12. m_lastpt = QPoint();
    13. update();
    14. }
    15. void setRasterOpTest::paintEvent(QPaintEvent *e){
    16. //... (do regular stuff here)
    17. if(!m_firstpt.isNull()){
    18. painter.save();
    19. painter.setPen(Qt::black);
    20. painter.drawLine(m_firstpt, m_lastpt);
    21. painter.restore();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: setRasterOp Equivalent

    Wow!!!

    The code written has reduced a lot and very efficient too. Now I'm getting why trolltech decided to make such a change. Awesome!!!!

    I have one doubt from above code. When I draw a line it disappears once the mouse it released. What I want is, the line has to appear after the mouse is released such that I can draw two or more lines.

    I tried setting some boolean values, but didnt help.

    Thanks!

  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: setRasterOp Equivalent

    Change the two QPoint members into a list or vector of points and don't null out the vector on mouse release. Then in the paint event iterate over the list and draw all the points.

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

    Default Re: setRasterOp Equivalent

    Quote Originally Posted by wysota View Post
    Change the two QPoint members into a list or vector of points and don't null out the vector on mouse release. Then in the paint event iterate over the list and draw all the points.
    SOmething like this. But unlucky

    Qt Code:
    1. void setRasterOpTest::mousePressEvent( QMouseEvent *e ){
    2. m_vector<<e->pos();
    3. }
    4.  
    5. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e ){
    6. m_vector<<e->pos();
    7. update();
    8. }
    9.  
    10. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    11. m_vector<<e->pos();
    12. update();
    13. }
    14.  
    15. void setRasterOpTest::paintEvent(QPaintEvent *e){
    16. //... (do regular stuff here)
    17.  
    18. if( m_vector.count() <= 0 )
    19. return;
    20.  
    21. QPainter painter;
    22. painter.begin(this);
    23.  
    24. painter.setPen( Qt::black );
    25. painter.drawLines ( m_vector );
    26. painter.end();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Also let me explain, I dont want a continuous flow of line as in scribble example but only straight line. I start from a point and when I release the mouse a straight line should be drawn. Then again I click on some other point and when I release the mouse another straight line should be drawn.

    Thanks for your understanding
    Last edited by vermarajeev; 29th March 2007 at 13:36.

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

    Default Re: setRasterOp Equivalent

    Hi wysota!,
    Can you please help me with this???

    Waiting for a reply!!!!!!

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

    Default Re: setRasterOp Equivalent

    The below solution draws only a single line. How to draw two or more lines???

    Qt Code:
    1. void setRasterOpTest::mousePressEvent( QMouseEvent *e ){
    2. if (e->button() == Qt::LeftButton) {
    3. m_firstpt = e->pos();
    4. m_lastpt = e->pos();
    5. }
    6. }
    7.  
    8. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e ){
    9. if ((e->buttons() & Qt::LeftButton) ){
    10. m_lastpt = e->pos();
    11. update();
    12. }
    13. }
    14.  
    15. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    16. if (e->button() == Qt::LeftButton ) {
    17. m_lastpt = e->pos();
    18. update();
    19. }
    20. }
    21.  
    22. void setRasterOpTest::paintEvent(QPaintEvent *e){
    23. QPainter painter(this);
    24. painter.setPen( Qt::black );
    25. painter.drawLine(m_firstpt, m_lastpt);
    26. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: setRasterOp Equivalent

    One more solution, Now this draws so many lines.. I'm trying and let you know if I get it...
    Here is the program...

    Qt Code:
    1. void setRasterOpTest::mousePressEvent( QMouseEvent *e ){
    2. if (e->button() == Qt::LeftButton) {
    3. m_firstpt = e->pos();
    4. m_lastpt = e->pos();
    5. }
    6. }
    7.  
    8. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e ){
    9. if ((e->buttons() & Qt::LeftButton) ){
    10. m_lastpt = e->pos();
    11. update();
    12. }
    13. }
    14.  
    15. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    16. if (e->button() == Qt::LeftButton ) {
    17. m_lastpt = e->pos();
    18. update();
    19. }
    20. }
    21.  
    22. void setRasterOpTest::paintEvent(QPaintEvent *e){
    23. QPainter painter(this);
    24. painter.setPen( Qt::black );
    25. m_vectorLine << QLine( m_firstpt, m_lastpt );
    26. painter.drawLines( m_vectorLine );
    27. }
    To copy to clipboard, switch view to plain text mode 

    If you get it before please let me know...
    Thanks

  14. #14
    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: setRasterOp Equivalent

    Sorry, I've been unavailable.

    Maybe you should change your concept a little and make it so that you click and drag with left mouse button and when you want to finish a line segment, you click with right mouse button.

    It would look more or less like so:
    Qt Code:
    1. void setRasterOpTest::mousePressEvent *e){
    2. if(e->button()==Qt::LeftButton){
    3. m_list.clear();
    4. m_list << e->pos();
    5. m_lastpt = e->pos();
    6. } else if(e->button()==Qt::RightButton){
    7. m_list << e->pos();
    8. m_lastpt = e->pos();
    9. }
    10. }
    11. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e )
    12. {
    13. m_lastpt = e->pos();
    14. update();
    15. }
    16. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    17. if(e->button()==Qt::LeftButton()){
    18. if(m_list.size()==1 && (e->pos()-m_list[0]).manhattanLength()<5){
    19. m_list.clear();
    20. update();
    21. return;
    22. }
    23. m_list << e->pos();
    24. m_lastpt = QPoint();
    25. update();
    26. }
    27. }
    28. void setRasterOpTest::paintEvent(QPaintEvent *e){
    29. //... (do regular stuff here)
    30. if(!m_list.isEmpty()){
    31. painter.save();
    32. painter.setPen(Qt::black);
    33. painter.drawLines(m_list);
    34. painter.setPen(Qt::red);
    35. painter.drawLine(m_list[m_list.size()-1], m_lastpt);
    36. painter.restore();
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 


    EDIT: A complete working example is attached.
    Attached Files Attached Files
    Last edited by wysota; 30th March 2007 at 11:16.

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

    Default Re: setRasterOp Equivalent

    Hi wysota,
    My application is something like QPainter in MS where you can draw different kind of molecules (in chemistry term). The product is now to be ported to qt4. If I change the functionality (which is not a good idea) of drawing operations, I'll have tough time with my client.

    The above sample program is the first step (where I should be able to draw two or more straight lines) to move forward.

    I think you can understand my problem so please help me to achieve the same functionality as setRasterOp in qt3 performs.

    Waiting eagerly for a reply....

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

    Default Re: setRasterOp Equivalent

    How about Uwe's idea...Will that help????

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

    Default Re: setRasterOp Equivalent

    One more thing...
    I checked the output for example you have given. It is not that I want.

    Your case:
    You draw a line with left mouse button pressed, it draws a red line, then you press the right mouse button, It draws a continous line.

    My case:
    You click at a point start dragging and release the mouse button. A straight line should be drawn. Then again if I mousePress at some other point another line has to be drawn with the previous one shown.


    Hope I'm clear!!

    Thanks for your understanding
    Attached Files Attached Files
    Last edited by vermarajeev; 30th March 2007 at 11:55.

  18. #18
    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: setRasterOp Equivalent

    Oh... I see... well, this is even easier. Just make a vector of QLine and add a line composed of m_firstPt and m_lastPt on mouse release and don't clear the vector on mouse press. The rest should remain the same as my first example.

    Uwe was speaking about more or less the same thing [qtclass]QRubberBand[qtclass] does. It won't be useful in your case.

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

    Default Re: setRasterOp Equivalent

    And here is the code that I want....

    Qt Code:
    1. #include "setrasteroptest.h"
    2. #include <QMouseEvent>
    3. #include <QPainter>
    4.  
    5. //Program to draw a straight line using Qt4
    6. //constructor
    7. setRasterOpTest::setRasterOpTest(QWidget *parent, Qt::WFlags flags)
    8. : QFrame(parent, flags)
    9. {
    10. ui.setupUi(this);
    11. QColor color( Qt::white );
    12. QPalette palette;
    13. palette.setColor(backgroundRole(), color);
    14. setPalette(palette);
    15. setMouseTracking(true);
    16. m_bflag = false;
    17. }
    18.  
    19. setRasterOpTest::~setRasterOpTest()
    20. {}
    21.  
    22. void setRasterOpTest::mousePressEvent( QMouseEvent *e ){
    23. if (e->button() == Qt::LeftButton){
    24. m_firstpt = e->pos();
    25. m_lastpt = e->pos();
    26. }
    27. }
    28.  
    29. void setRasterOpTest::mouseMoveEvent( QMouseEvent *e ){
    30. if ((e->buttons() & Qt::LeftButton) ){
    31. m_lastpt = e->pos();
    32. m_bflag = true;
    33. update();
    34. }
    35. }
    36.  
    37. void setRasterOpTest::mouseReleaseEvent(QMouseEvent *e){
    38. if (e->button() == Qt::LeftButton ) {
    39. m_lastpt = e->pos();
    40. m_vectorLine << QLine( m_firstpt, m_lastpt );
    41. update();
    42. }
    43. }
    44.  
    45. void setRasterOpTest::paintEvent(QPaintEvent *e){
    46. QPainter painter(this);
    47. painter.setPen( Qt::black );
    48. painter.drawLines( m_vectorLine );
    49.  
    50. if( m_bflag ){
    51. painter.drawLine( m_firstpt, m_lastpt );
    52. m_bflag = false;
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

    Thanks wysota!!!!

  20. #20
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,313
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setRasterOp Equivalent

    I don't know the details of your applications, but in general there are two types of lines. One that is supporting an interaction ( what is often called a rubberband ), the other one is a permanent line - the result of the interaction. Raster operations (XOR mode) are only useful for the first type of line (dragging), so I´m pretty sure, that your code is about rubberbanding - even if you are not aware about this yet.

    The trick about painting in XOR mode is, that painting the same thing twice, erases the first painting. This makes it possible to drag an object, without having to repaint the content of the widget. With Qt4 you always (!) have to repaint the content to erase the previous position of the line - and in many environments, you will see that the dragged object can't follow mouse movements in time.

    Implementing rubberbands (dragging) without raster operations leads to very different code with worse results. This is something you and your client have to accept. The only choice you have is if you want to merge the painting code of rubberband and content (that´s what the Qt widgets do), or if you isolate the painting code of the rubberband to an masked 2. widget, where changes of the mask lead to paint events for the content widget. The main advantage of the second implementation is that you can implement it once and can use it with any type of content widget - that´s what f.e. QwtPicker is about.

    Uwe

    By the way: QRubberBand is a styled line/rect, but absolutely no replacement for the XOR mode.

Similar Threads

  1. Replies: 2
    Last Post: 23rd April 2006, 01:02
  2. what's the equivalent with DPtoLP in windows
    By cocalele in forum Qt Programming
    Replies: 6
    Last Post: 1st March 2006, 06:57
  3. make install equivalent in qmake ...
    By momesana in forum Newbie
    Replies: 3
    Last Post: 20th February 2006, 21:46
  4. PostMessage() across process boundaries equivalent
    By TheKedge in forum Qt Programming
    Replies: 9
    Last Post: 27th January 2006, 00:02

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.