Hi guys,
Well, I have some objects on QFrame in form of rectangles( either one or more ). I have options to copy and paste the objects. I say CTRL+C to copy the object and say CTRL+V to paste. While moving the mouse after pressing CTRL+V, I want the rectangular object be seen whereever I move the mouse ( giving a feeling to user that something is about to paste inside QFrame).
In qt3 I could do this with no efforts as there was RasterOp and painting could be done from anywhere but in Qt4 I'm having problem to achieve the same result.

I have written a sample program which will help to get what I mean.

In the sample program, Click on paste button, a rectangle should appear at the tip of mouse pointer. Now if I move mouse, the rectangle should move along with the mouse pointer. I tried but could not succeed.

Here qApp->enter_loop() is QT3 support class as I couldnt find the alternative in Qt4.

Qt Code:
  1. RubberBand::RubberBand(QWidget *parent, Qt::WFlags flags)
  2. : QWidget(parent, flags){
  3. setAutoFillBackground( true );
  4. QPalette palette;
  5. palette.setColor(backgroundRole(), QColor(Qt::white) );
  6. setPalette(palette);
  7.  
  8. QPushButton* btnPaste = new QPushButton("Paste", this);
  9. btnPaste->setGeometry( 250, 250, 30, 40 );
  10. connect( btnPaste, SIGNAL( clicked() ), this, SLOT( onPasteBtn() ) );
  11. m_Rubberband = 0;
  12. }
  13.  
  14. RubberBand::~RubberBand()
  15. {}
  16.  
  17. void RubberBand::onPasteBtn(){
  18. qApp->installEventFilter( this );
  19. qApp->enter_loop();
  20. }
  21.  
  22. bool RubberBand::eventFilter( QObject *obj, QEvent *e ){
  23. if ( e->type() == QEvent::KeyPress && ((QKeyEvent *)e)->key() == Qt::Key_Escape ){
  24. // remove this event filter and exit the current event loop
  25. qApp->removeEventFilter( this );
  26. qApp->exit_loop();
  27. delete m_Rubberband;
  28. m_Rubberband = 0;
  29. return TRUE;
  30. }
  31. if( e->type() == QEvent::MouseMove ){
  32. m_pasteRect.setTop( ((QMouseEvent *)e)->pos().x() );
  33. m_pasteRect.setLeft( ((QMouseEvent *)e)->pos().y() );
  34.  
  35. m_pasteRect.setBottom( ((QMouseEvent *)e)->pos().x() + 50 );
  36. m_pasteRect.setRight( ((QMouseEvent *)e)->pos().y() + 50 );
  37.  
  38. if( !m_Rubberband )
  39. m_Rubberband = new QRubberBand(QRubberBand::Rectangle, this);
  40.  
  41. //qDebug() << ((QMouseEvent *)e)->pos().x() << ((QMouseEvent *)e)->pos().y();
  42.  
  43. m_Rubberband->setGeometry( m_pasteRect );
  44. m_Rubberband->show();
  45.  
  46. return TRUE;
  47. }
  48. else if( e->type() == QEvent::MouseButtonRelease ){
  49. // remove this event filter and exit the current event loop
  50. qApp->removeEventFilter( this );
  51. qApp->exit_loop();
  52. delete m_Rubberband;
  53. m_Rubberband = 0;
  54. return TRUE; // eat event
  55. }
  56. return TRUE; // block standard event processing
  57. }
To copy to clipboard, switch view to plain text mode 

Thanks