Results 1 to 3 of 3

Thread: QRubberBand doesn't show (X11/Linux)

  1. #1
    Join Date
    Oct 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QRubberBand doesn't show (X11/Linux)

    I'm trying the following code for testing out a QRubberBand selection but nothing shows up when I press, hold and drag my mouse over the QWidget that appears.

    In Python [PyQt4]:
    Qt Code:
    1. from PyQt4.QtGui import QApplication, QRubberBand, QWidget
    2. from PyQt4.QtCore import QPoint, QRect, QSize
    3. from sys import argv
    4.  
    5. class mw(QWidget):
    6. def __init__(self):
    7. QWidget.__init__(self)
    8.  
    9. def mousePressEvent(self,event):
    10. self.p_initial = event.globalPos()
    11. self.p_rb = QRubberBand(QRubberBand.Rectangle, self)
    12. self.p_rb.setGeometry(QRect(self.p_initial, QSize(0,0)))
    13. self.p_rb.show()
    14.  
    15. def mouseMoveEvent(self,event):
    16. if self.p_rb:
    17. self.p_rb.setGeometry(QRect(self.p_initial, event.globalPos()).normalized())
    18.  
    19. def mouseReleaseEvent(self,event):
    20. del self.p_rb
    21. self.p_rb = None
    22.  
    23. if __name__=='__main__':
    24. app = QApplication(argv)
    25. w = mw()
    26. w.show()
    27. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    Or, in C++ [Qt]:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMouseEvent>
    3. #include <QPoint>
    4. #include <QRubberBand>
    5. #include <QWidget>
    6.  
    7. class mw : public QWidget
    8. {
    9. protected:
    10. void mousePressEvent(QMouseEvent *e)
    11. {
    12. p_initial = e->globalPos();
    13. p_rb = new QRubberBand(QRubberBand::Rectangle, this);;
    14. p_rb->setGeometry(QRect(p_initial, QSize(0,0)));
    15. p_rb->show();
    16. }
    17.  
    18. void mouseMoveEvent(QMouseEvent *e)
    19. {
    20. if (p_rb)
    21. p_rb->setGeometry(QRect(p_initial, e->globalPos()).normalized());
    22. }
    23.  
    24. void mouseReleaseEvent(QMouseEvent *)
    25. {
    26. delete p_rb;
    27. p_rb = 0;
    28. }
    29.  
    30. private:
    31. QPoint p_initial;
    32. QRubberBand *p_rb;
    33. };
    34.  
    35. int main(int argc, char *argv[])
    36. {
    37. QApplication a(argc, argv);
    38. mw *w = new mw;
    39. w -> show();
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Oddly, the C++ one seems to work fine, with the rectangle showing up (A little flashing occurs but thats fine.). Whats wrong with the code in PyQt4?

    Do I need to enable something else too?
    Last edited by QwertyManiac; 16th October 2008 at 21:00.

  2. #2
    Join Date
    Oct 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QRubberBand doesn't show (X11/Linux)

    *Bump* Any ideas? Any at all?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand doesn't show (X11/Linux)

    I'm not sure why it works for C++, but you should use local coordinates instead of global ones.

    Python Code:
    1. ...
    2. self.p_initial = self.mapFromGlobal(event.globalPos())
    3. ...
    4. pos = self.mapFromGlobal(event.globalPos())
    5. self.p_rb.setGeometry(QRect(self.p_initial,pos).normalized())
    To copy to clipboard, switch view to plain text mode 

Tags for this Thread

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.