내 이전 게시물을 따라 나는 withing에 위치 결정 문제를 해결하기 위해 노력 해왔다 QGraphicsView
.
위젯을에서 끌어서 QListWidget
내부에 놓으면 QGraphicsView
모든 곳으로 이동합니다.
문제는 내가 다른 사용자도 도움으로 해결하기 위해 노력 해왔다은 다음과 같다 : 위젯은 내부에 드래그 즉시로 QGraphicsView
하고 떨어졌다. 그것은 단지 임의의 위치에 가서 그것을 회수하기 위해주의를 기울여야합니다. 이 동작은 사용자에게 친숙하지 않으며 때로는 위젯을 찾는 데 약간의 시간이 걸립니다.
코드 아래 :
scene.h
#ifndef SCENE_H
#define SCENE_H
#include <QGraphicsScene>
class Scene : public QGraphicsScene
{
public:
Scene(QObject *parent = nullptr);
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
};
#endif // SCENE_H
scene.cpp
void Scene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
QByteArray encoded =
event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
QStringList rosTables;
QString newString;
while (!stream.atEnd()) {
int row, col;
QMap<int, QVariant> roleDataMap;
stream >> row >> col >> roleDataMap;
rosTables << roleDataMap[Qt::DisplayRole].toString();
}
for (const QString &tableType : rosTables) {
if (tableType == "Images") {
QPoint initPos(0, 0);
auto *wgt = new CustomTableWidget;
auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
QBrush(Qt::darkGreen));
auto *sizeGrip = new QSizeGrip(wgt);
auto *layout = new QHBoxLayout(wgt);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);
connect(wgt, &CustomTableWidget::sizeChanged, [wgt, proxyControl](){
proxyControl->setRect(wgt->geometry().adjusted(-10, -10, 10, 10));
});
wgt->setColumnCount(2);
wgt->setRowCount(2);
for (int ridx = 0; ridx < wgt->rowCount(); ridx++) {
for (int cidx = 0; cidx < wgt->columnCount(); cidx++) {
auto *item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
auto *const proxy = addWidget(wgt);
proxy->setPos(initPos.x(), initPos.y()
+ proxyControl->rect().height());
proxy->setParentItem(proxyControl);
proxyControl->setPos(initPos.x(), initPos.y());
proxyControl->setFlag(QGraphicsItem::ItemIsMovable, true);
proxyControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
proxyControl->setRect(wgt->geometry().adjusted(-10, -10, 10, 10));
}
}
}
문제를 해결하기 위해 지금까지 시도한 것 :
이제 QGraphicsScene
마우스 이벤트를 다시 작성하도록 하위 클래스가 지정되었습니다. 특히이 경우 dropEvent(QGraphicsSceneDragDropEvent *event)
"위젯보기"를 담당하는 기능인에 주의를 기울였습니다.
여러 번의 시행 착오가 있었으며 동일한 기능에 다음 부분을 추가하려고 시도했습니다.
for (const QString &tableType : rosTables) {
if (tableType == "Images") {
QPoint initPos(event->scenePos()); // <-- Tried this but no change
auto *wgt = new CustomTableWidget;
auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
QBrush(Qt::darkGreen));
auto *sizeGrip = new QSizeGrip(wgt);
auto *layout = new QHBoxLayout(wgt);
}
시도 추가 것은 제공했다 문제 중 하나가 해결되지 않는,하지만 불행히도 목표에 대한 적절한하다고 판단 :QPoint
event->scenePos().toPoint()
for (const QString &tableType : rosTables) {
if (tableType == "Images") {
QPoint initPos(event->scenePos().toPoint()); // <-- Tried this too but no change
auto *wgt = new CustomTableWidget;
auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
QBrush(Qt::darkGreen));
auto *sizeGrip = new QSizeGrip(wgt);
auto *layout = new QHBoxLayout(wgt);
}
문제가 무엇인지 아는 사람이 있으면 해결 방법에 대한 지침을 제공하십시오.
변경 proxy->setPos(initPos.x(), initPos.y() + proxyControl->rect().height());
에 proxy->setPos(10, 10);
.
장면 직사각형 설정 :
Scene::Scene(QObject *parent) :
QGraphicsScene(parent)
{
setBackgroundBrush(Qt::lightGray);
setSceneRect(0, 0, 1000, 1000);
}
예를 들어 MainWindow.cpp 에서보기 정렬을 설정합니다 .
ui->graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다