
Originally Posted by
georgie
i never thought about it like that....how would you be able to register a click within a region - like if there was just one big widget but i wanted to click on button number 5, how would i know exactly where the user had clicked? I was using the mousePressEvent of each individual widget....
// now calculate which button was clicked (if any)
// for example (simplified problem):
// X X X X X
// X X X X
// X X X X X
// X X X X
// each X is 20x20px
// row:
int row = clicked.y()/20;
// col:
int col = clicked.x()/20;
// check if position occupied and ignore otherwise:
if(row % 2 && !(col%2)) return; // odd numbered row and even numbered column (starting from 0)
if(!(row%2) && col%2) return; // even numbered row and odd numbered column
// now it should be easy to map [row x column] to a button, for example:
QPoint buttoncoords
(row
/2, col
/2);
emit buttonClicked(buttoncoords);
}
void MyWidget::mousePressEvent(QMouseEvent *e){
QPoint clicked = e->pos();
// now calculate which button was clicked (if any)
// for example (simplified problem):
// X X X X X
// X X X X
// X X X X X
// X X X X
// each X is 20x20px
// row:
int row = clicked.y()/20;
// col:
int col = clicked.x()/20;
// check if position occupied and ignore otherwise:
if(row % 2 && !(col%2)) return; // odd numbered row and even numbered column (starting from 0)
if(!(row%2) && col%2) return; // even numbered row and odd numbered column
// now it should be easy to map [row x column] to a button, for example:
QPoint buttoncoords(row/2, col/2);
emit buttonClicked(buttoncoords);
}
To copy to clipboard, switch view to plain text mode
keeping the gradient as a "has a" member does speed stuff up quite a bit, but the first time i set the colour at 0.2, this cannot be changed in subsequent repaints (i.e. if i deselect something, it remains its selected colour....e.g. if it was the selected one, but then it becomes only the centre of a hex, instead of going from yellow to cyan it stays yellow for the remainder)
there doesn't seem to be an "unsetColorAt()" function, so i think it is necessary to keep initializing a new grad....which is a bummer cause it made it so much better
So create a new one if it changes. Most of the time it won't change.
if(buttonStateChanged){
grad.setColorAt(0.1, Qt::white);
grad.setColorAt(0.2, ...);
grad.setColorAt(..., ...);
}
drawButton(&p);
}
void MyButton::paintEvent(QPaintEvent *e){
if(buttonStateChanged){
grad = QRadialGradient (15, 15, 35, 0, 0);
grad.setColorAt(0.1, Qt::white);
grad.setColorAt(0.2, ...);
grad.setColorAt(..., ...);
}
QPainter p(this);
drawButton(&p);
}
To copy to clipboard, switch view to plain text mode
or even predefine gradients:
class MyButton : public ... {
//...
private:
//...
};
//...
//...
class MyButton : public ... {
//...
private:
static QRadialGradient normalGradient;
static QRadialGradient selectedGradient;
static QRadialGradient someotherGradient;
//...
};
//...
QRadialGradient MyButton::normalGradient = QRadialGradient(15, 15, 35, 0, 0);
//...
To copy to clipboard, switch view to plain text mode
Bookmarks