QGraphicsPixmapItem未在QGraphicsView中显示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QGraphicsPixmapItem未在QGraphicsView中显示相关的知识,希望对你有一定的参考价值。
我将QGraphicsView(类Display)子类化,并从QTableView实现拖放。 QGraphicsScene中有预先存在的项目,而scene() - > itemsBoundingRect覆盖整个地球。如果我人为地将QGraphicsPixmapItem的boundingRect设置为QGraphicsView的viewPort(),我看不到我在QGraphicsPixmapItem中设置的QPixmap并添加到scene()中。如果我在fitInView中调整scene() - > itemsBoundingRect,那么pixmap就会被放到整个世界。我希望它放在我的视口上,但基本上fitInView viewPort不会显示该项目,即使根据调试它是可见的而且是notObscured()。
void Display::dragEnterEvent(QDragEnterEvent* event)
{
event->acceptProposedAction();
update();
}
void Display::dragMoveEvent(QDragMoveEvent* event){
event->acceptProposedAction();
update();
}
void TableView::mouseMoveEvent(QMouseEvent *event)
{
QDrag* drag = new QDrag(this);
QMimeData *mime = new QMimeData();
drag->setMimeData(mime);
QImage image("/Users/sb/Downloads/puffin.jpeg");
mime->setImageData(image);
drag->setPixmap(QPixmap::fromImage(image).scaled(10,10));
drag->exec();
QTableView::mouseMoveEvent(event);
}
编辑试试@Eyllanesc的建议
void Display::dropEvent(QDropEvent* event)
{
setCacheMode(QGraphicsView::CacheBackground);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
if(event->mimeData()->hasImage())
{
pixMap = qvariant_cast<QPixmap>(event->mimeData()->imageData());
pixMap = pixMap.scaledToHeight(10);
pixMap = pixMap.scaledToWidth(10);
bool h = pixMap.isNull(); //returns false
posn = mapToScene(event->pos());
QRectF ff = mapToScene(viewport()->geometry()).boundingRect();
bool ps = ff.contains(posn); //returns true
item = new QGraphicsPixmapItem(pixMap);
scene()->addItem(item);
item->setPos( posn);
item->setZValue(100);
item->setVisible(true);
item->setOpacity(1.0);
item->update();
scene()->update();
auto lView = qobject_cast<QGraphicsView*>(this);
lView->fitInView(ff, Qt::KeepAspectRatio);// no pixmap is shown
invalidateScene(ff);
bool i = item->isObscured(); //returns false
repaint();
update();
}
event->acceptProposedAction();
}
-
答案
在下一部分中,我将向您展示如何实现从QTableView到QGraphicsView的拖放操作的实例:
#include <QApplication>
#include <QDrag>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QMimeData>
#include <QMouseEvent>
#include <QTableView>
#include <QGraphicsPixmapItem>
#include <random>
#include <QStandardItemModel>
static std::random_device rd;
static std::mt19937 rng(rd());
class TableView: public QTableView{
public:
using QTableView::QTableView;
protected:
void mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton)
dragStartPosition = event->pos();
}
void mouseMoveEvent(QMouseEvent *event){
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - dragStartPosition).manhattanLength()
< QApplication::startDragDistance())
return;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
// create image
QImage image(100, 100, QImage::Format_RGB32);
std::uniform_int_distribution<int> uni(0, 255);
image.fill(QColor(uni(rng), uni(rng), uni(rng)));
mimeData->setImageData(image);
drag->setPixmap(QPixmap::fromImage(image).scaled(10,10));
drag->setMimeData(mimeData);
drag->exec();
QTableView::mouseMoveEvent(event);
}
private:
QPoint dragStartPosition;
};
class Display: public QGraphicsView{
public:
using QGraphicsView::QGraphicsView;
protected:
void dragMoveEvent(QDragMoveEvent *event){
if(event->mimeData()->hasFormat("application/x-qt-image"))
event->acceptProposedAction();
}
void dropEvent(QDropEvent *event){
if (event->source() == this)
return;
QPixmap pixmap = qvariant_cast<QPixmap>(event->mimeData()->imageData());
QGraphicsPixmapItem *pixmap_item = scene()->addPixmap(pixmap);
pixmap_item->setPos(mapToScene(event->pos()));
event->acceptProposedAction();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QHBoxLayout* lay = new QHBoxLayout(&w);
QGraphicsScene scene;
Display view(&scene);
view.setAcceptDrops(true);
TableView table;
QStandardItemModel model(10, 10);
table.setModel(&model);
lay->addWidget(&table);
lay->addWidget(&view);
w.show();
return a.exec();
}
以上是关于QGraphicsPixmapItem未在QGraphicsView中显示的主要内容,如果未能解决你的问题,请参考以下文章
QGraphicsPixmapItem - 仅触发顶部对象的事件