Qt3D Konum Değiştirme

Merhabalar.
Geçenlerde Qt3D ile arayüz üzerinde 3D nesneleri görüntüleyebileceğim bir örneğe rastladım.
Kodun kısaltılmış hali:

import sys
from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.Qt3DCore import Qt3DCore
from PySide2.Qt3DExtras import Qt3DExtras
from PySide2.Qt3DRender import Qt3DRender
from PySide2.Qt3DInput import Qt3DInput


class SceneModifier(QtCore.QObject):
    def __init__(self, root_entity=None):
        super().__init__()
        self.m_rootEntity = root_entity

        self.cuboid = Qt3DExtras.QCuboidMesh()

        self.cuboidTransform = Qt3DCore.QTransform(scale=4.0, translation=QtGui.QVector3D(5.0, -4.0, 0.0))

        self.cuboidMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#665423"))

        self.m_cuboidEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_cuboidEntity.addComponent(self.cuboid)
        self.m_cuboidEntity.addComponent(self.cuboidMaterial)
        self.m_cuboidEntity.addComponent(self.cuboidTransform)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    view = Qt3DExtras.Qt3DWindow()
    view.defaultFrameGraph().setClearColor(QtGui.QColor("#4d4d4f"))
    container = QtWidgets.QWidget.createWindowContainer(view)
    screenSize = view.screen().size()
    container.setMinimumSize(QtCore.QSize(200, 200))
    container.setMaximumSize(screenSize)

    widget = QtWidgets.QWidget()
    hLayout = QtWidgets.QHBoxLayout(widget)
    vLayout = QtWidgets.QVBoxLayout()
    vLayout.setAlignment(QtCore.Qt.AlignTop)
    hLayout.addWidget(container, 1)
    hLayout.addLayout(vLayout)

    widget.setWindowTitle("Basic shapes")

    input_ = Qt3DInput.QInputAspect()
    view.registerAspect(input_)

    rootEntity = Qt3DCore.QEntity()

    cameraEntity = view.camera()

    cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0)
    cameraEntity.setPosition(QtGui.QVector3D(0, 0, 20.0))
    cameraEntity.setUpVector(QtGui.QVector3D(0, 1, 0))
    cameraEntity.setViewCenter(QtGui.QVector3D(0, 0, 0))

    lightEntity = Qt3DCore.QEntity(rootEntity)
    light = Qt3DRender.QPointLight(lightEntity)
    light.setColor("white")
    light.setIntensity(1)
    lightEntity.addComponent(light)

    lightTransform = Qt3DCore.QTransform(lightEntity)
    lightTransform.setTranslation(cameraEntity.position())
    lightEntity.addComponent(lightTransform)

    modifier = SceneModifier(rootEntity)

    view.setRootEntity(rootEntity)

    info = QtWidgets.QCommandLinkButton()
    info.setText("Qt3D ready-made meshes")
    info.setDescription("Bla bla bla.")
    info.setIconSize(QtCore.QSize(0, 0))

    vLayout.addWidget(info)

    widget.show()
    widget.resize(1200, 800)

    sys.exit(app.exec_())

  • Burada QHBoxLayout() kullanılmasından ötürü olmalı ki, 3D nesnenin içinde yer aldığı bölge çok büyük oluyor. Bunu container.setMaximumSize(QtCore.QSize(400, 400)) ile ayarlayabilsem de bu bölgenin konumunu x ve y değerleri olarak değiştirmeyi beceremedim.

  • Yardıma ihtiyaç duyduğum nokta şu,
    ben bu bahsini ettiğim bölgenin, setGeometry(QRect(25, 25, 400, 400)) ile konumunu ve boyutunu ayarlamak istiyorum.
    Fakat bu QHBoxLayout() tüm planlarımı mahvediyor gibi görünüyor.

Bahsini ettiğim yöntemle veya başka yöntemlerle, bu pencerenin konumunu nasıl ayarlayabileceğimi gösterebilirseniz çok mutlu olurum.

Merhaba, ne istediğinizden emin değilim ama anladığım kadarıyla modeli gösterdiğiniz gri alanı sol uste taşımak istiyorsunuz.

Ben biraz qdesigneri kurcaladım ve buldum :slight_smile:

    container.setMaximumSize(QtCore.QSize(400, 400))
    widget = QtWidgets.QWidget()
    hLayout = QtWidgets.QHBoxLayout(widget)
    vLayout = QtWidgets.QVBoxLayout()
    vLayout.setAlignment(QtCore.Qt.AlignTop)
    hLayout.addWidget(container, 1, QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
    hLayout.addLayout(vLayout)

Burası, kodunuzda değişiklik yaptığım yer. Bir widgetin orijin noktasını pencerenin sol üst köşesi olarak kabul edersek hboxlayout a ekleyeceğimiz widgetin hboxlayout a göre aligment(hizalama)'i ile oynamak gerekli. Bu işi şu kod parçacığı ile yapıyoruz.

hLayout.addWidget(container, 1, QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)

daha sonra ise bu widgetin olabileceği maximum width ve height için de

container.setMaximumSize(QtCore.QSize(400, 400))

yapabilirsiniz.

kodların son hali

import sys
from PySide6 import QtWidgets, QtCore, QtGui
from PySide6.Qt3DCore import Qt3DCore
from PySide6.Qt3DExtras import Qt3DExtras
from PySide6.Qt3DRender import Qt3DRender
from PySide6.Qt3DInput import Qt3DInput


class SceneModifier(QtCore.QObject):
    def __init__(self, root_entity=None):
        super().__init__()
        self.m_rootEntity = root_entity

        self.cuboid = Qt3DExtras.QCuboidMesh()

        self.cuboidTransform = Qt3DCore.QTransform(scale=4.0, translation=QtGui.QVector3D(5.0, -4.0, 0.0))

        self.cuboidMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#665423"))

        self.m_cuboidEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_cuboidEntity.addComponent(self.cuboid)
        self.m_cuboidEntity.addComponent(self.cuboidMaterial)
        self.m_cuboidEntity.addComponent(self.cuboidTransform)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    view = Qt3DExtras.Qt3DWindow()
    view.defaultFrameGraph().setClearColor(QtGui.QColor("#4d4d4f"))
    container = QtWidgets.QWidget.createWindowContainer(view)
    screenSize = view.screen().size()
    container.setMinimumSize(QtCore.QSize(200, 200))
    # container.setMaximumSize(screenSize)
    container.setMaximumSize(QtCore.QSize(400, 400))
    widget = QtWidgets.QWidget()
    hLayout = QtWidgets.QHBoxLayout(widget)
    vLayout = QtWidgets.QVBoxLayout()
    vLayout.setAlignment(QtCore.Qt.AlignTop)
    hLayout.addWidget(container, 1, QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
    hLayout.addLayout(vLayout)

    widget.setWindowTitle("Basic shapes")

    input_ = Qt3DInput.QInputAspect()
    view.registerAspect(input_)

    rootEntity = Qt3DCore.QEntity()

    cameraEntity = view.camera()

    cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0)
    cameraEntity.setPosition(QtGui.QVector3D(0, 0, 20.0))
    cameraEntity.setUpVector(QtGui.QVector3D(0, 1, 0))
    cameraEntity.setViewCenter(QtGui.QVector3D(0, 0, 0))

    lightEntity = Qt3DCore.QEntity(rootEntity)
    light = Qt3DRender.QPointLight(lightEntity)
    light.setColor("white")
    light.setIntensity(1)
    lightEntity.addComponent(light)

    lightTransform = Qt3DCore.QTransform(lightEntity)
    lightTransform.setTranslation(cameraEntity.position())
    lightEntity.addComponent(lightTransform)

    modifier = SceneModifier(rootEntity)

    view.setRootEntity(rootEntity)

    info = QtWidgets.QCommandLinkButton()
    info.setText("Qt3D ready-made meshes")
    info.setDescription("Bla bla bla.")
    info.setIconSize(QtCore.QSize(0, 0))

    vLayout.addWidget(info)

    widget.show()
    widget.resize(1200, 800)

    sys.exit(app.exec_())

Umarım sorunuzu doğru anlamışımdır ve istenilen çözüme ulaşmışımdır. İyi akşamlar.

Edit: Siz konumu da ayarlamak istiyorsunuz, onu bulursam döneyim, hiç fark etmemişim.

3 Beğeni

Merhabalar, iyi akşamlar.
Evet istediğim şey genel olarak buydu, çok teşekkürler.

Bugüne dek hep setGeometry() kullandığım için yine onunla yapmayı denedim ama QHBoxLayout() olayı biraz farklıymış. Sanırım biraz daha kurcalamam gerekecek.

Tekrardan teşekkürler :+1:

1 Beğeni

aslında hboxlayout yerine başka layoutlar da kullanılabilir. Muhtemelen hboxlayout ile widget konumu ayarlamak, uğraştırıcı olur. yani spacer falan kullanmak gerekebilir. Bu işi yapan başka layoutlar illaki vardır, bi bakmak lazım ama şu anlık geç oldu pek vaktim yok ona da sonra bakayım.

Rica ederim kolay gelsin.

1 Beğeni

Tekrardan merhaba, aslında bu kahverengi alanı da bir layout a alarak soldan ve üstten margin vererek konumu da ayarlayabiliriz. Ama yarın falan deneyeyim. Bilgisayar başından kalktim şimdi

1 Beğeni

Hocam hortlatarak tekrardan teşekkür ediyorum :slight_smile:
Dediğinize bağlı olarak setContentsMargins() kullanarak amacıma ulaştım.

Ya iyi ki hortlattın, sizin meselenizi unutmuşum ben. Bu arada ben, sizin işinizi görecek bir layout bakmıştım da nedense göremedim (çok derin araştırmadım ama). Bu arada ben teşekkür ediyorum.

1 Beğeni