我已经创建了一个继承 QListWidget 的类,它是一堆卡片。
我重载了拖放功能以允许将卡片拖到表格(我程序的另一个对象)上,现在我遇到了另一个问题。
QListWidget 显示我所有的项目(主要是因为我从一开始就将它们添加到 GUI)。
事情是这样的: 在我的主窗口中,我初始化我的 CardPile 对象并用洗牌后的卡片 vector 填充它。
现在我希望我的 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?
}
因为我目前在主窗口中有我的洗牌 vector (我在 forloop 中添加所有牌)。
或者我应该制作一个连接主窗口和 CardPile 的信号和插槽 - 那么什么时候
delete takeItem(row(item));
被称为我发出一个信号,说要将下一张牌添加到列表中?
感谢反馈
最佳答案
您可以使用 QStackWiget
(描述的第一行正是您想要实现的)而不是 QListWidget。
关于c++ - 使 QListWidget 只显示 1 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4319150/