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 sys import argv
def __init__(self):
def mousePressEvent(self,event):
self.p_initial = event.globalPos()
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__':
w = mw()
w.show()
app.exec_()
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_()
To copy to clipboard, switch view to plain text mode
Or, in C++ [Qt]:
#include <QApplication>
#include <QMouseEvent>
#include <QPoint>
#include <QRubberBand>
#include <QWidget>
{
protected:
{
p_initial = e->globalPos();
p_rb->show();
}
{
if (p_rb)
p_rb
->setGeometry
(QRect(p_initial, e
->globalPos
()).
normalized());
}
{
delete p_rb;
p_rb = 0;
}
private:
};
int main(int argc, char *argv[])
{
mw *w = new mw;
w -> show();
return a.exec();
}
#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();
}
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?
Bookmarks