PDA

View Full Version : How to Highlight a PixMap?



LaTj
2nd November 2013, 01:11
I am trying to make a simple card game, and I want to highlight some card pixmap images as my mouse hovers over it. Then I want to click on that card. But, lets take it one thing at a time. How would I highlight those cards, I really do not have any clue?

By the way, I use QLabel to make the pixmaps.

Here is the code, displaying my constructor and the function where I map the images.



#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()));
connect(cardTable, SIGNAL(clicked(QModelIndex)), this, SLOT(currentCard(const QModelIndex &)));

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

}

anda_skoa
2nd November 2013, 10:31
You could change the pixmap to a highlighted version, either by creating the highlighted pixmap from the original and using QPainter and some effect to create a new one or having two image files.

Or you could try playing with the QPalette of the QLabels

Cheers,
_

LaTj
5th November 2013, 00:23
Thanks I used QGraphicsColorizeEffect::setColor(QPalette::Highli ght) for the QLabels;

and I added a QRect outline to get the effect I wanted;