Make QListWidget only show 1 item
I have created a class that inherits the QListWidget and is meant to be a stack of cards.
I have overloaded the drag and drop functions to allow a card to be dragged to the table (another object of my program) and now I stumble on another problem.
The QListWidget shows all my items (mainly because I add them to the GUI from the beginning).
So this is how it goes: in my mainwindow I initialise my CardPile object and fill it up with a shuffled vector of cards.
Now I want my QListWidget to show only one (but its showing a grid with all my cards).
On a drop I remove the item from my QListWidget. But I have no idea if I were to add and remove 1 card at a time (so it only shows 1 card ofcourse) in my code.
public:
TileStack(QWidget *parent = 0);
void addCard(QPixmap pixmap, QPoint location);
QPixmap showCard();
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void startDrag(Qt::DropActions supportedActions); //in this function I remove the current item
These are the functions in my CardPile : QListWidget.
so:
void TileStack::startDrag(Qt::DropActions /*supportedActions*/)
{
QListWidgetItem *item = currentItem();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
QPixmap pixmap = qVariantValue<QPixmap>(item->data(Qt::UserRole));
QPoint location = item->data(Qt::UserRole+1).toPoint();
dataStream << pixmap << location;
QMimeData *mimeData = new QMimeData;
mimeData->setData("card", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
drag->setPixmap(pixmap);
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
delete takeItem(row(item));
//should I also make the add to the ne开发者_如何学JAVAxt item here? and how exactly should I put it here?
}
Because I currently have my vector of shuffled cards in the mainwindow (where I add all the cards in a forloop).
Or should I make a signal and slot that connect between mainwindow and CardPile - so when
delete takeItem(row(item));
is called I emit a signal that says to add the next card to the list?
Thanks for feedback
You can use a QStackWiget
(the first line of the desription is exactly what you want to ahieve) instead of a QListWidget.
精彩评论