PDA

View Full Version : Using QTimer and mouseMoveEvent



LaTj
20th November 2013, 01:20
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.



//In Constructor
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(highlightCard()));
timer->start(500);

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
x_pos = event->pos().x();
y_pos = event->pos().y();
}
}

void Canvas::highlightCard()
{
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(this);
effect->setColor(QPalette::Highlight);

for (int i = 0; i < MAXCARDS; i++)
{
if (cardIconPlayer[i]->pos() == QPoint(x_pos, y_pos))
cardIconPlayer[i]->setGraphicsEffect(effect);
}
}

anda_skoa
20th November 2013, 10:12
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,
_

LaTj
20th November 2013, 23:08
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.

ChrisW67
21st November 2013, 03:15
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?

LaTj
22nd November 2013, 00:53
cardIconPlayer is a QLabel and this is how I set up the icons using pixmap.



void Canvas::setUpPlayerIcons(string *pString)
{
QPixmap qpx;
QSize iconSize;
string str;

for (int i = 0; i < MAXCARDS; i++)
{
shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(10);

cardIconPlayer[i] = new QLabel(cardTable);
str = pString[i];
qpx = QPixmap(str.c_str());
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.

ChrisW67
22nd November 2013, 02:13
I would be inclined to make the QLabels look after their own highlighting when the mouse is over them.



#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QGraphicsDropShadowEffect>

class Label: public QLabel
{
Q_OBJECT
public:
Label(QWidget *p = 0): QLabel(p)
{
QPixmap pixmap(200, 200);
pixmap.fill(QColor("mediumseagreen"));
setPixmap(pixmap);
setFrameStyle(QFrame::Box);

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);
}
};

class Widget: public QWidget
{
Q_OBJECT
public:
Widget(QWidget *p = 0): QWidget(p)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new Label);
layout->addWidget(new Label);
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#include "main.moc"

LaTj
3rd December 2013, 23:18
Thank you very much, this was more than thankful. Sorry for the late reply