PDA

View Full Version : How to Click on a Pixmap?



LaTj
6th November 2013, 23:56
I recently found a way to highlight a pixmap, now I need to highlight while the mouse cursor is positioned on it. Also, I need to click on these pixmap images.
My problem is that I have no idea how to do this. Some people suggest to subclass QLabel. If someone could lead me to the right direction please.

Here is my code



#include <QtWidgets>
#include <string>

using std::string;

#include "canvas.h"
#include "player.h"
#include "computer.h"

Canvas::Canvas(QWidget *parent) : QWidget(parent)
{
cardTable = new QListView;

startButton = new QPushButton("Start");
startButton->setFixedWidth(100);

connect(startButton, SIGNAL(clicked()), this, SLOT(startClicked()));

QVBoxLayout *verticalLayout = new QVBoxLayout;
verticalLayout->addWidget(cardTable);
verticalLayout->addWidget(startButton);

QGridLayout *mainLayout = new QGridLayout;
mainLayout->addLayout(verticalLayout, 0, 0, 1, 1);

setLayout(mainLayout);

setUpComputerIcons();
setUpDeckIcons();

myGame = new Game(this);
}

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

}

wagmare
7th November 2013, 03:51
reimplement hoverEnterEvent( http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qgraphicsitem.html#hoverEnterEvent of Canvas class for highlighting mouse pos

and for mousePress reimplement mousePressEvent
http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qgraphicsitem.html#mousePressEvent

and dont forget to use setAcceptHoverEvents

LaTj
8th November 2013, 03:48
How would I do that, my problem is that I don't know how to use event handlers. Do I have to make a different class for event handlers?

How would I do that, my problem is that I don't know how to use event handlers. Do I have to make a different class for event handlers?

stampede
8th November 2013, 06:36
Using Inheritance And Virtual Functions (http://www.flipcode.com/archives/Using_Inheritance_And_Virtual_Functions.shtml)

LaTj
11th November 2013, 22:18
Do I still have to use connect, signals, and slots to emit these functions?

stampede
11th November 2013, 22:41
No, you don't "emit" those functions. They will be called automatically in response to events if you subclass the widget correctly.
There is nothing special going on, if you know how inheritance and virtual functions work, then just apply what you know to Qt classes here.

wagmare
12th November 2013, 04:16
look the basic example http://harmattan-dev.nokia.com/docs/library/html/qt4/widgets-shapedclock.html .. check just how they enable mousePress and mouseMove ..

LaTj
14th November 2013, 22:58
Thanks for the example and the advice. It takes me awhile since I don't work on this everyday.