Using QTimer and mouseMoveEvent
I am trying to use a QTimer with a mouseMoveEvent to highlight some cards. I do not know what I am doing wrong. If someone can give me some useful hints.
Code:
//In Constructor
connect(timer, SIGNAL(timeout()), this, SLOT(highlightCard()));
timer->start(500);
{
if (event
->type
() == QEvent::MouseMove) {
x_pos = event->pos().x();
y_pos = event->pos().y();
}
}
void Canvas::highlightCard()
{
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(this);
for (int i = 0; i < MAXCARDS; i++)
{
if (cardIconPlayer
[i
]->pos
() == QPoint(x_pos, y_pos
)) cardIconPlayer[i]->setGraphicsEffect(effect);
}
}
Re: Using QTimer and mouseMoveEvent
What do you expect to happen?
Do you manage to move the mouse to exactly one of the cardIconPlayer positions in the 500ms after creating the widget?
Cheers,
_
Re: Using QTimer and mouseMoveEvent
I am a newbie, so I erroneously thought this code will highlight the card. So yes that is what I thought would happen. If you can point out what I can do to highlight those cardIconPlayer's with other methods, corrections, or further my studies in a certain topic then I would greatly appreciate it.
I know C++ but not Qt, so this is interesting to me but really new.
Re: Using QTimer and mouseMoveEvent
Your highlight routine is called once every 500 ms and will do something if the last mouse move event over this widget recorded a position that precisely matches the position of one of your cardIconPlayer objects (whatever they are). That's a precise match on a single pixel position not an area: unlikely to occur, I think you'll agree.
What type are the cardIconPlayer objects?
Re: Using QTimer and mouseMoveEvent
cardIconPlayer is a QLabel and this is how I set up the icons using pixmap.
Code:
void Canvas::setUpPlayerIcons(string *pString)
{
string str;
for (int i = 0; i < MAXCARDS; i++)
{
shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(10);
cardIconPlayer
[i
] = new QLabel(cardTable
);
str = pString[i];
iconSize = qpx.size();
iconSize.scale(190, 276, Qt::KeepAspectRatio);
QPixmap scaledImage
= qpx.
scaled(iconSize, Qt
::KeepAspectRatio, Qt
::SmoothTransformation);
cardIconPlayer[i]->setPixmap(scaledImage);
cardIconPlayer[i]->move(20 + 60 * i, 350);
cardIconPlayer[i]->setGraphicsEffect(shadow);
//playerPixmaps.push_back(scaledImage);
}
}
Just to get this straight. Are you saying I should use QRect instead of QPoint.
Re: Using QTimer and mouseMoveEvent
I would be inclined to make the QLabels look after their own highlighting when the mouse is over them.
Code:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QGraphicsDropShadowEffect>
{
Q_OBJECT
public:
{
pixmap.
fill(QColor("mediumseagreen"));
setPixmap(pixmap);
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(10);
shadow
->setColor
(QColor("orchid"));
setGraphicsEffect(shadow);
}
protected:
void enterEvent
(QEvent *event
) {
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(10);
shadow
->setColor
(QColor("lightsalmon"));
setGraphicsEffect(shadow);
}
void leaveEvent
(QEvent *event
) {
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(10);
shadow
->setColor
(QColor("orchid"));
setGraphicsEffect(shadow);
}
};
{
Q_OBJECT
public:
{
layout->addWidget(new Label);
layout->addWidget(new Label);
}
};
int main(int argc, char **argv)
{
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
Re: Using QTimer and mouseMoveEvent
Thank you very much, this was more than thankful. Sorry for the late reply