Reputation: 647
Lets assume we have grid layout like this:
this->layout = new QGridLayout;
ClickableLabel *lab1 = new ClickableLabel();
this->layout->addWidget(lab1, 0, 0);
ClickableLabel *lab2 = new ClickableLabel();
this->layout->addWidget(lab2, 1, 0);
Is it possible for these two ClickableLabels to overlap each other? I am making a card game and I need the cards(labels) to overlap like in Solitaire. Now my question is: Do I need to use another layout for this? or I can do it with some trick like setGeometry
or move
.(none of it worked for me)
Upvotes: 1
Views: 1425
Reputation: 2398
Please, don't do like that.
If you are creating a card game, then you need to create cards, not buttons, also you cannot put them in layouts. The problem is that the layout management system from Qt is ancient and it doesn't really takes into account items in the monitor as first classes citzens - it arrange them in the layout, that's it.
If you are into card games and Qt, you have two easy choices:
1 - Old Style (and the one I actually know how to teach) - QGraphivsView
Create your card class based on QGraphicsRectItem, and override the mousePress and mouseRelease events to allow clicking and dragging.
class Card : public QObject, public QGraphicsRectItem {
Q_OBJECT
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *ev)
{
clicked();
startDrag = true;
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *ev)
{
if(!startDrag)
return;
setPos(ev->pos());
startDrag = false;
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *ev)
{
if (!startDrag)
return();
setPos(ev->pos());
}
signals:
void clicked();
};
then also create a new class based on QGraphivsView that will be your board, and arrange the cards based on your disposal algorithm.
2 - The style that I cannot teach: QML.
Upvotes: 1
Reputation: 12879
Assuming the overlap is fairly static you can easily simulate it by having each ClickableLabel
span multiple rows and/or columns...
this->layout = new QGridLayout;
ClickableLabel *lab1 = new ClickableLabel();
this->layout->addWidget(lab1, 0, 0, 2/* Row span */, 1/* Column span */);
ClickableLabel *lab2 = new ClickableLabel();
this->layout->addWidget(lab2, 1, 0, 2/* Row span */, 1/* Column span */);
In the example above lab1
will occupy rows 0 and 1 and lab2
will occupy rows 1 and 2. Hence they will overlap in row 1 with the z-order being dictated by the order in which they are added.
Upvotes: 2
Reputation: 5750
I'm using QMainWindow for my application and I render a whole bunch of widgets which are defined in XML files, the location of the widgets is specified in relative co-ordinates. The QMainWindow is set-up for the entire display.
Upvotes: 1