Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基于示例代码添加QGraphicsView,里面显示的QGraphicsItem下半部分区域无法接收鼠标事件 #93

Open
Shimapoikaze opened this issue May 30, 2024 · 4 comments

Comments

@Shimapoikaze
Copy link

qt版本:6.5.3 msvc 2019
系统:windows11
在主窗口中添加了一个 QGraphicsView,创建多个不同类型的 QGraphicsItem (QGraphicsRectItem、QGraphicsEllipseItem 和 QGraphicsTextItem)。当鼠标位于这些 QGraphicsItem 的下半部分区域时候,QGraphicsItem无法接收到鼠标点击事件,具体表现为无法点击下半部分进行拖动。

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
     installWindowAgent();

//#if 1
//    auto clockWidget = new ClockWidget();
//    clockWidget->setObjectName(QStringLiteral("clock-widget"));
//    clockWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//    setCentralWidget(clockWidget);
//#else
//    auto webView = new QWebEngineView();
//    webView->load(QUrl("https://www.baidu.com"));
//    setCentralWidget(webView);
//#endif
     view = new QGraphicsView(this);
     scene = new QGraphicsScene(this);

     rectItem = new QGraphicsRectItem(0, 0, 100, 100);
     rectItem->setBrush(Qt::red);
     rectItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(rectItem);

     auto ellipseItem = new QGraphicsEllipseItem(150, 0, 100, 100);
     ellipseItem->setBrush(Qt::green);
     ellipseItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(ellipseItem);

     auto textItem = new QGraphicsTextItem("Drag me!");
     textItem->setPos(300, 0);
     textItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(textItem);
     view->setScene(scene);
     view->setMouseTracking(true);

     setCentralWidget(view);

    loadStyleSheet(Dark);

    setWindowTitle(tr("Example MainWindow"));
    resize(800, 600);

     windowAgent->centralize();
}
@wangwenx190
Copy link
Collaborator

是不是在标题栏区域里了?

@wangwenx190
Copy link
Collaborator

标题栏区域中的控件(一部分在也算),要用setHitTestVisible手动开启交互事件,否则无法响应任何交互,请仔细看自述文件

@wangwenx190 wangwenx190 added the invalid This doesn't seem right label May 30, 2024
@Shimapoikaze
Copy link
Author

设置了setHitTestVisible之后一样不行

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
     installWindowAgent();

//#if 1
//    auto clockWidget = new ClockWidget();
//    clockWidget->setObjectName(QStringLiteral("clock-widget"));
//    clockWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//    setCentralWidget(clockWidget);
//#else
//    auto webView = new QWebEngineView();
//    webView->load(QUrl("https://www.baidu.com"));
//    setCentralWidget(webView);
//#endif
     centralWidget = new QWidget(this);
     auto layout = new QVBoxLayout(centralWidget);

     view = new QGraphicsView(this);
     scene = new QGraphicsScene(this);

     rectItem = new QGraphicsRectItem(0, 0, 100, 100);
     rectItem->setBrush(Qt::red);
     rectItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(rectItem);

     auto ellipseItem = new QGraphicsEllipseItem(150, 0, 100, 100);
     ellipseItem->setBrush(Qt::green);
     ellipseItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(ellipseItem);

     auto textItem = new QGraphicsTextItem("Drag me!");
     textItem->setPos(300, 0);
     textItem->setFlag(QGraphicsItem::ItemIsMovable);
     scene->addItem(textItem);
     view->setScene(scene);
     view->setMouseTracking(true);

     layout->addWidget(view);

     windowAgent->setHitTestVisible(centralWidget,true);
//     windowAgent->setHitTestVisible(view,true);
     setCentralWidget(centralWidget);

    loadStyleSheet(Dark);

    setWindowTitle(tr("Example MainWindow"));
    resize(800, 600);

//     windowAgent->centralize();
}

其余代码与示例代码相同
并且在改变主窗口大小(拉大或者缩小)之后QGraphicsItem所有区域都可以拖动

@SineStriker SineStriker removed the invalid This doesn't seem right label May 30, 2024
@SineStriker
Copy link
Contributor

先更新到 Qt 6.6.2 及以上再说

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants