PDA

View Full Version : Keep pixels Highlighted wherever cursor moved after entering the widget.



learner_qt
8th February 2013, 06:09
I want to highlight all the pixels where cursor moved after entering the widget and before leaving it.
On reimplementing the paintEvent and using drawPoint, I am able to display the current cursor position. But I dont want old points to disappear. How do i keep all previous points also highlighted???
I am not successful in using drawPath and drawPoints for this. :(




Lines::Lines(QWidget *parent) :
QWidget(parent),
ui(new Ui::Lines)
{
ui->setupUi(this);
}


void Lines::paintEvent(QPaintEvent *e)
{
QPainter qp(this);
drawLines(&qp);
}


void Lines::drawLines(QPainter *qp)
{
QPen pen2(Qt::red, 8, Qt::SolidLine);
qp->setPen(pen2);

int x;
int y;

QPoint p = QCursor::pos();
x = p.x();
y= p.y();

qp->drawPoint(p);
this->update();
}

ChrisW67
8th February 2013, 06:45
Why do you expect it to work? You are not doing anything to record the pixels that need to be drawn on each update. Exactly how you do that will depend on what else is drawn in the widget. See the Scribble Example in the docs for how you might approach this for a blank image.

BTW, as the docs say:


Note: Generally, you should refrain from calling update() or repaint() inside a paintEvent(). For example, calling update() or repaint() on children inside a paintevent() results in undefined behavior; the child may or may not get a paint event.

learner_qt
8th February 2013, 07:34
I dont expect this to work. This is just the code to track current position of cursor.
I am stuck with how to record so many pixels and how to pass them as parameters. :(
and if i dont update, only the first cursor position is highlighted, after that nothing would happen.