我正在為Qt3DRender::QCamera實作一個自定義的相機控制器,我遇到了Qt3DInput::QMouseHandler相當奇怪的行為。根據它被創建的環境,它要么對滑鼠事件有反應,要么沒有。有兩種情況:在MainWindow物件中同時創建設備和處理程式,或者在我的相機控制器類中創建它們(只在第一種情況下有效)。
第一種情況:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this)。
auto* window = new Qt3DExtras::Qt3DWindow()。
auto* window_container = QWidget::createWindowContainer(window, this, Qt::Widget)。
setCentralWidget(window_container)。
auto* scene = new Qt3DCore::QEntity() 。
window->setRootEntity(scene)。
auto* mouse_device = new Qt3DInput::QMouseDevice(場景)。
auto* mouse_handler = new Qt3DInput::QMouseHandler(場景)。
mouse_handler->setSourceDevice(mouse_device)。
connect(mouse_handler, &Qt3DInput::QMouseHandler::positionChanged,
[](Qt3DInput::QMouseEvent* event)
{
qDebug() << "我真的在向控制臺列印!!"。
});
}
第二種情況:
class CustomCameraController final : public Qt3DCore::QNode
{
Q_OBJECT
public:
explicit CustomCameraController(Qt3DCore::QNode* parent = nullptr)
: Qt3DCore::QNode(father)。
mouse_device(new Qt3DInput::QMouseDevice(this))。
mouse_handler(new Qt3DInput::QMouseHandler(this))
{
mouse_handler->setSourceDevice(mouse_device)。
connect(mouse_handler, &Qt3DInput::QMouseHandler::pressAndHold, this,
&CustomCameraController::MousePositionChanged)。)
}
public slots:
void MousePositionChanged(Qt3DInput::QMouseEvent* event)
{
qDebug() << "我沒有列印任何東西..."。
}
protected:
Qt3DInput::QMouseDevice* mouse_device。
Qt3DInput::QMouseHandler* mouse_handler。
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this)。
auto* window = new Qt3DExtras::Qt3DWindow()。
auto* window_container = QWidget::createWindowContainer(window, this, Qt::Widget)。
setCentralWidget(window_container)。
auto* scene = new Qt3DCore::QEntity() 。
window->setRootEntity(scene)。
auto* controller = new CustomCameraController(場景)。
所有不必要的代碼被洗掉。main.cpp檔案是由Qt Framework自動生成的
我搜索了所有的Qt檔案,沒有發現任何可以幫助我解決這個問題的內容。我還注意到,如果設備和處理程式在建構式中以parent作為引數被初始化,也沒有幫助。此外,我試圖在MainWindow范圍內創建設備和處理程式,并通過一些setter函式將它們傳遞給控制器,但這也沒有幫助。
所以我想問的問題是:使用 你應該把控制器類宣告添加到頭檔案而不是主視窗源檔案中。下面是你需要的所有內容和qmake選項: uj5u.com熱心網友回復: 看起來你只是連接了不同的信號。
標籤:Qt3DInput::QMouseDevice和Qt3DInput::QMouseHandler的正確方式是什么?是否有更好的解決方法來為我的自定義相機控制器實作輸入處理?
Update 1:
#include <Qt3DCore/QNode>/span>
#include <Qt3DInput/QMouseDevice>
#include <Qt3DInput/QMouseHandler>
#include <Qt3DExtras/Qt3DWindow>/span>
#include <Qt3DCore/QEntity>
3dinput 3dcore 3dextras
在第一種情況下,你正在使用&Qt3DInput::QMouseHandler::positionChanged,這將被經常發送。在第二種情況下--&Qt3DInput::QMouseHandler::pressAndHold,它將在滑鼠按鈕被按下時被發送。
固定后,兩個記錄函式都被呼叫。
