PDA

View Full Version : QRubberBand doesn't show (X11/Linux)



QwertyManiac
16th October 2008, 19:55
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]:

from PyQt4.QtGui import QApplication, QRubberBand, QWidget
from PyQt4.QtCore import QPoint, QRect, QSize
from sys import argv

class mw(QWidget):
def __init__(self):
QWidget.__init__(self)

def mousePressEvent(self,event):
self.p_initial = event.globalPos()
self.p_rb = QRubberBand(QRubberBand.Rectangle, self)
self.p_rb.setGeometry(QRect(self.p_initial, QSize(0,0)))
self.p_rb.show()

def mouseMoveEvent(self,event):
if self.p_rb:
self.p_rb.setGeometry(QRect(self.p_initial, event.globalPos()).normalized())

def mouseReleaseEvent(self,event):
del self.p_rb
self.p_rb = None

if __name__=='__main__':
app = QApplication(argv)
w = mw()
w.show()
app.exec_()

Or, in C++ [Qt]:

#include <QApplication>
#include <QMouseEvent>
#include <QPoint>
#include <QRubberBand>
#include <QWidget>

class mw : public QWidget
{
protected:
void mousePressEvent(QMouseEvent *e)
{
p_initial = e->globalPos();
p_rb = new QRubberBand(QRubberBand::Rectangle, this);;
p_rb->setGeometry(QRect(p_initial, QSize(0,0)));
p_rb->show();
}

void mouseMoveEvent(QMouseEvent *e)
{
if (p_rb)
p_rb->setGeometry(QRect(p_initial, e->globalPos()).normalized());
}

void mouseReleaseEvent(QMouseEvent *)
{
delete p_rb;
p_rb = 0;
}

private:
QPoint p_initial;
QRubberBand *p_rb;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mw *w = new mw;
w -> show();
return a.exec();
}


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?

QwertyManiac
21st October 2008, 09:25
*Bump* Any ideas? Any at all?

jacek
29th October 2008, 22:58
I'm not sure why it works for C++, but you should use local coordinates instead of global ones.


...
self.p_initial = self.mapFromGlobal(event.globalPos())
...
pos = self.mapFromGlobal(event.globalPos())
self.p_rb.setGeometry(QRect(self.p_initial,pos).no rmalized())