Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019974
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:15+00:00 2026-05-20T10:10:15+00:00

I am attempting to use PyQT to position and display a custom widget. Thus

  • 0

I am attempting to use PyQT to position and display a custom widget. Thus far, I have a widget, and my window. I have been successful in displaying the widget through a layout, however, I am interested in using .move(x,y,) to position my widget before show()’ing it. Thus far my code is as follows:

import sys, random
from PyQt4 import QtGui, QtCore

# Robot Widget
class RobotLink(QtGui.QWidget):
    def __init__(self, parent, x, y, width, height, fill):
        super(RobotLink, self).__init__(parent)
        self._x        = x
        self._y        = y
        self._width    = width
        self._height   = height
        self._fill     = fill
        self._rotation = 0

    def paintEvent(self, e):
        painter = QtGui.QPainter()
        painter.begin(self)
        self.drawLink(painter)
        painter.end()

    def drawLink(self, painter):
        painter.setPen(QtGui.QColor(0, 0, 0))
        painter.setBrush(self._fill)
        painter.drawEllipse(self._x, self._y, self._width, self._height)

# Window
class Window(QtGui.QWidget):
    # Default Constructor, sets up the window GUI
    def __init__(self):
        super(Window, self).__init__()
        self.initUI()

    def initUI(self):
        self._link1 = RobotLink(self, 225, 400, 30, 150, QtCore.Qt.DiagCrossPattern)
        self._link2 = RobotLink(self, 0, 320, 30, 100, QtCore.Qt.Dense5Pattern)
        self._link3 = RobotLink(self, 225, 260, 30, 75, QtCore.Qt.Dense2Pattern)

        self._link1.move(0, 0)
        self._link1.show()

        self.setGeometry(300, 300, 800, 600)
        self.setWindowTitle("CSCE 452 - PaintBot")


    def paintEvent(self, e):
        super(Window, self).paintEvent(e)
        painter = QtGui.QPainter()
        painter.begin(self)
        self.drawBoundingBoxes(painter)
        painter.end()

    # Draws the boxes that define the robots workspace and
    # the control panel
    def drawBoundingBoxes(self, painter):
        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor("#cccccc")
        painter.setPen(color)

        # Draw the robot workspace
        painter.setBrush(QtGui.QColor(255, 255, 255))
        painter.drawRect(10, 10, 500, 578)

        # Draw the control panel workspace
        painter.setBrush(QtGui.QColor(150, 150, 150))
        painter.drawRect(520, 10, 270, 578)

        # Draws the slider 'base'
        painter.setPen(QtGui.QColor(0, 0, 0))
        painter.drawLine(100, 570, 400, 570)

    def changeValue(self, value):
        self.wid.emit(QtCore.SIGNAL("updateRobot(int)"), value)
        self.wid.repaint()

# Setup the Window, and the Robot
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.exec_()

Any idea how to attach my widget, without a layout, position it and have it show inside of my window?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:10:15+00:00Added an answer on May 20, 2026 at 10:10 am

    Couple of things I noticed in your code:

    1. you don’t really need to define store x, y, width and height for your custom widget. Just call setGeometry with coordinates passed into the constructor. Widget already providers getGeometry, getHeight, getWidth, etc. methods which you can use to manipulate and paint your component.

    2. when you call drawEllipse in the child widget drawLink method, you’re passing x and y coordinates into the function as rectangle beginning. From my understanding you should put there 0, 0 as those coordinates should be widget relative not the window relative.

    I’ve made some changes to your code, see if it works for you

    import sys, random
    from PyQt4 import QtGui, QtCore
    
    # Robot Widget
    class RobotLink(QtGui.QWidget):
        def __init__(self, parent, x, y, width, height, fill):
            super(RobotLink, self).__init__(parent)
            self._fill     = fill
            self._rotation = 0
            self.setGeometry(x, y, width, height)
    
        def paintEvent(self, e):
            painter = QtGui.QPainter()
            painter.begin(self)
            self.drawLink(painter)
            painter.end()
    
        def drawLink(self, painter):
            painter.setPen(QtGui.QColor(0, 0, 0))
            painter.setBrush(self._fill)
            painter.drawEllipse(0, 0, self.width(), self.height())
    
    # Window
    class Window(QtGui.QWidget):
        # Default Constructor, sets up the window GUI
        def __init__(self):
            super(Window, self).__init__()
            self.initUI()
    
        def initUI(self):
            self._link1 = RobotLink(self, 10, 10, 100, 50, QtCore.Qt.DiagCrossPattern)
            self._link2 = RobotLink(self, 100, 100, 50, 100, QtCore.Qt.Dense5Pattern)
            self._link3 = RobotLink(self, 150, 150, 50, 50, QtCore.Qt.Dense2Pattern)
    
            self.setGeometry(300, 300, 800, 600)
            self.setWindowTitle("CSCE 452 - PaintBot")
    
        def paintEvent(self, e):
            super(Window, self).paintEvent(e)
            painter = QtGui.QPainter()
            painter.begin(self)
            self.drawBoundingBoxes(painter)
            painter.end()
    
        # Draws the boxes that define the robots workspace and
        # the control panel
        def drawBoundingBoxes(self, painter):
            color = QtGui.QColor(0, 0, 0)
            color.setNamedColor("#cccccc")
            painter.setPen(color)
    
            # Draw the robot workspace
            painter.setBrush(QtGui.QColor(255, 255, 255))
            painter.drawRect(10, 10, 500, 578)
    
            # Draw the control panel workspace
            painter.setBrush(QtGui.QColor(150, 150, 150))
            painter.drawRect(520, 10, 270, 578)
    
            # Draws the slider 'base'
            painter.setPen(QtGui.QColor(0, 0, 0))
            painter.drawLine(100, 570, 400, 570)
    
        def changeValue(self, value):
            self.wid.emit(QtCore.SIGNAL("updateRobot(int)"), value)
            self.wid.repaint()
    
    # Setup the Window, and the Robot
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    app.exec_()
    

    hope this helps, regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to use ASP.NET MVC remote validation for username login access
I am attempting to use an MVVM-ish approach to my WPF development. I have
Attempting to use a custom hex color for my css triangle (border). However since
I am attempting to use a VSPackage to create a Tool window for visual
I am attempting to use a custom workflow activity to auto-increment build numbers based
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
While attempting to use the CopyFile() function I have encountered a strange error. It
In attempting to use ed to delete lines around a certain pattern I've been
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.