使 QListWidget 只显示 1 项

发布于 2024-10-05 06:11:01 字数 1810 浏览 0 评论 0原文

我创建了一个继承 QListWidget 的类,它是一堆卡片。

我已经重载了拖放功能,以允许将卡片拖到桌子上(我的程序的另一个对象),现在我偶然发现了另一个问题。

QListWidget 显示了我的所有项目(主要是因为我从一开始就将它们添加到 GUI 中)。

事情是这样的: 在我的主窗口中,我初始化 CardPile 对象并用洗牌的卡片向量填充它。

现在我希望我的 QListWidget 仅显示一个(但它显示包含我所有卡片的网格)。

在放置时,我从 QListWidget 中删除该项目。但我不知道我是否要在代码中一次添加和删除一张卡(所以它当然只显示一张卡)。

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

这些是我的 CardPile 中的函数:QListWidget。

所以:

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 next item here? and how exactly should I put it     here?
}

因为我目前在主窗口中有我的洗牌向量(我在 forloop 中添加所有牌)。

或者我应该在主窗口和 CardPile 之间创建一个连接的信号和插槽 - 所以当

delete takeItem(row(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 next 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2024-10-12 06:11:01

您可以使用 QStackWiget (第一行描述正是您想要的)而不是 QListWidget。

You can use a QStackWiget (the first line of the desription is exactly what you want to ahieve) instead of a QListWidget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文