Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Create a Graphics View
Forum Updated to NodeBB v4.3 + New Features

Create a Graphics View

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JThornton
    wrote on last edited by
    #1

    I found an example and tried to incorporate it into my code but it creates a new QGraphicsView and does not put the objects in the QGraphicsView that is in the loaded UI. I know I'm missing some small detail...

    #!/usr/bin/python3
    
    import os, sys
    from PyQt5 import uic
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsItem
    from PyQt5.QtGui import QBrush, QPen
    
    UI_FILE = os.path.join(os.path.dirname(__file__), "d2p.ui")
    
    class MainWindow(QMainWindow):
    	def __init__(self):
    		super(MainWindow, self).__init__()
    		uic.loadUi(UI_FILE, self)
    		self.setWindowTitle('HELP ME')
    		self.createGraphicView()
    		self.show()
    
    	def createGraphicView(self):
    		self.scene  = QGraphicsScene()
    		self.greenBrush = QBrush(Qt.green)
    		self.grayBrush = QBrush(Qt.gray)
    		self.pen = QPen(Qt.red)
    		self.graphicView = QGraphicsView(self.scene, self)
    		self.graphicView.setGeometry(0,0,600,400)
    		self.shapes()
    
    	def shapes(self):
    		ellipse = self.scene.addEllipse(20,20, 200,200, self.pen, self.greenBrush)
    		rect = self.scene.addRect(-100,-100, 200,200, self.pen, self.grayBrush)
    		ellipse.setFlag(QGraphicsItem.ItemIsMovable)
    		rect.setFlag(QGraphicsItem.ItemIsMovable)
    		ellipse.setFlag(QGraphicsItem.ItemIsSelectable)
    
    def main():
    	app = QApplication(sys.argv)
    	ex = MainWindow()
    	sys.exit(app.exec_())
    
    if __name__ == "__main__":
    	main()
    
    

    The ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QWidget" name="widget" native="true">
          <widget class="QLabel" name="coordLbl">
           <property name="geometry">
            <rect>
             <x>30</x>
             <y>10</y>
             <width>141</width>
             <height>19</height>
            </rect>
           </property>
           <property name="text">
            <string>TextLabel</string>
           </property>
          </widget>
         </widget>
        </item>
        <item>
         <widget class="QWidget" name="widget_2" native="true">
          <layout class="QHBoxLayout" name="horizontalLayout">
           <item>
            <widget class="QGraphicsView" name="graphicsView">
             <property name="sizePolicy">
              <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
               <horstretch>0</horstretch>
               <verstretch>30</verstretch>
              </sizepolicy>
             </property>
             <property name="minimumSize">
              <size>
               <width>0</width>
               <height>400</height>
              </size>
             </property>
            </widget>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>24</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>Fi&amp;le</string>
        </property>
        <addaction name="actionOpen"/>
       </widget>
       <addaction name="menuFile"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      <action name="actionOpen">
       <property name="text">
        <string>&amp;Open</string>
       </property>
      </action>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    alt text

    Thanks
    JT

    jsulmJ 1 Reply Last reply
    0
    • J JThornton

      I found an example and tried to incorporate it into my code but it creates a new QGraphicsView and does not put the objects in the QGraphicsView that is in the loaded UI. I know I'm missing some small detail...

      #!/usr/bin/python3
      
      import os, sys
      from PyQt5 import uic
      from PyQt5.QtCore import Qt
      from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView, QGraphicsItem
      from PyQt5.QtGui import QBrush, QPen
      
      UI_FILE = os.path.join(os.path.dirname(__file__), "d2p.ui")
      
      class MainWindow(QMainWindow):
      	def __init__(self):
      		super(MainWindow, self).__init__()
      		uic.loadUi(UI_FILE, self)
      		self.setWindowTitle('HELP ME')
      		self.createGraphicView()
      		self.show()
      
      	def createGraphicView(self):
      		self.scene  = QGraphicsScene()
      		self.greenBrush = QBrush(Qt.green)
      		self.grayBrush = QBrush(Qt.gray)
      		self.pen = QPen(Qt.red)
      		self.graphicView = QGraphicsView(self.scene, self)
      		self.graphicView.setGeometry(0,0,600,400)
      		self.shapes()
      
      	def shapes(self):
      		ellipse = self.scene.addEllipse(20,20, 200,200, self.pen, self.greenBrush)
      		rect = self.scene.addRect(-100,-100, 200,200, self.pen, self.grayBrush)
      		ellipse.setFlag(QGraphicsItem.ItemIsMovable)
      		rect.setFlag(QGraphicsItem.ItemIsMovable)
      		ellipse.setFlag(QGraphicsItem.ItemIsSelectable)
      
      def main():
      	app = QApplication(sys.argv)
      	ex = MainWindow()
      	sys.exit(app.exec_())
      
      if __name__ == "__main__":
      	main()
      
      

      The ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QWidget" name="widget" native="true">
            <widget class="QLabel" name="coordLbl">
             <property name="geometry">
              <rect>
               <x>30</x>
               <y>10</y>
               <width>141</width>
               <height>19</height>
              </rect>
             </property>
             <property name="text">
              <string>TextLabel</string>
             </property>
            </widget>
           </widget>
          </item>
          <item>
           <widget class="QWidget" name="widget_2" native="true">
            <layout class="QHBoxLayout" name="horizontalLayout">
             <item>
              <widget class="QGraphicsView" name="graphicsView">
               <property name="sizePolicy">
                <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
                 <horstretch>0</horstretch>
                 <verstretch>30</verstretch>
                </sizepolicy>
               </property>
               <property name="minimumSize">
                <size>
                 <width>0</width>
                 <height>400</height>
                </size>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
         </layout>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>800</width>
           <height>24</height>
          </rect>
         </property>
         <widget class="QMenu" name="menuFile">
          <property name="title">
           <string>Fi&amp;le</string>
          </property>
          <addaction name="actionOpen"/>
         </widget>
         <addaction name="menuFile"/>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
        <action name="actionOpen">
         <property name="text">
          <string>&amp;Open</string>
         </property>
        </action>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      alt text

      Thanks
      JT

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @JThornton said in Create a Graphics View:

      uic.loadUi(UI_FILE, self)

      Change this line to

      self.ui = uic.loadUi(UI_FILE, self)
      

      And then access your view via

      self.ui.graphicsView
      

      And remove

      self.graphicView = QGraphicsView(self.scene, self)
      

      from createGraphicView as it creates another view.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • J Offline
        J Offline
        JThornton
        wrote on last edited by
        #3

        Thanks so much for the example that works, I'll study that and proceed with my learning. My goal is to open a DXF file and display it and allow the user to select the start point for plasma cutting.

        JT

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JThornton
          wrote on last edited by
          #4

          @Denni-0

          I've been studying your code and do you not use the Qt Designer but rather just code everything in Python?

          JT

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved