gizgok
gizgok

Reputation: 7649

Setting buttons inside a widget a circular layout in Pyside

I'm trying to position buttons in a circular layout. This is the code that I have written so far.

class Temp(QPushButton):
    def __init__(self, x, y, w, h, parent = None):
        super(Temp, self).__init__(parent)
        self.w = w
        self.h = h
        self.x = x
        self.y = y
        self.text = "test"


    def paintEvent(self, e):

        super(self.parent, self).paintEvent(e)

        qp = QPainter()
        qp.begin(self.viewPort())
        self.drawP(qp)
        qp.end()

    def drawP(self, qp):
        qp.setPen(Qt.red)
        qp.drawEllipse(self.x, self.y, self.w, self.h)



class Example(QWidget):

    def __init__(self):
       super(Example, self).__init__()
       self.initUI()

    def initUI(self):      

       self.setGeometry(300, 300, 350, 100)
       self.setWindowTitle('Points')
       self.show()

    def paintEvent(self, e):
       qp = QPainter()
       qp.begin(self)
       self.drawP(qp)
       qp.end()

    def drawP(self, qp):
       theta = 2 * np.pi  / 15
       for i in range(15):
           angle = theta * (i + 1)
           dx = int(round(400 + 300 * np.cos(angle)))
           dy = int(round(300 + 300 * np.sin(angle)))
          #qp.drawEllipse(dx, dy, 10, 10)
          t = Temp(dx, dy, 10, 10, parent = self)




app = QApplication(sys.argv)  
ex = Example()
sys.exit(app.exec_())

If I uncomment the drawEllipse function in Example class, I get the ellipse in a cirular layout but not the buttons.

Upvotes: 0

Views: 982

Answers (2)

BoshWash
BoshWash

Reputation: 5470

Here is a cleaned up working version:

class Temp(QPushButton):
    def __init__(self, x, y, w, h, parent=None):
        super(Temp, self).__init__(parent)
        self.w = w
        self.h = h
        self.x = x
        self.y = y
        self.text = "test"

    def paintEvent(self, e):
        qp = QPainter(self)
        qp.setPen(Qt.red)
        qp.drawEllipse(0, 0, self.w - 2, self.h - 2)


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('Points')
        self.drawP()


    def drawP(self):
        theta = 2 * np.pi / 15
        for i in range(15):
            angle = theta * (i + 1)
            dx = int(round(400 + 300 * np.cos(angle)))
            dy = int(round(300 + 300 * np.sin(angle)))
            t = Temp(dx, dy, 10, 10, parent=self)
            t.setGeometry(dx, dy, 10, 10)


app = QApplication(sys.argv)
ex = Example()
self.show()
sys.exit(app.exec_())

Some hints:

  • you need to call .show() on every button you add after the parents .show()
  • the geometry needs to be set inside the parent widget
  • it's probably best to not create any widgets in the paint function, put it into the __init__ or your initUI
  • for what you're trying to do QGraphicsScene/View might be more suitable

Upvotes: 1

You are mixing up multiple problems:

  1. You shouldn't be adding new widgets within a paint event. The buttons should be added to Example within the constructor.

  2. You should start with QPushButtons, get them to work, and only then switch to your own class. 10x10 is too small to display a button!

  3. A widget shouldn't be showing itself. Its user should do it.

  4. A pushbutton doesn't have a viewport.

  5. Widgets added to an already shown widget will not be visible.

Start with something like this:

class Example(QWidget):
  def __init__(self):
    super(Example, self).__init__()
    self.initUI()

  def initUI(self):      
    theta = 2 * np.pi  / 15
    for i in range(15):
      angle = theta * (i + 1)
      dx = int(round(self.width()/2 + self.width()/3 * np.cos(angle)))
      dy = int(round(self.height()/2 + self.height()/3 * np.sin(angle)))
      b = QPushButton("test", parent = self)
      b.setGeometry(QRect(dx, dy, 50, 50))

app = QApplication(sys.argv)  
ex = Example()
ex.setGeometry(300, 300, 350, 100)
ex.show()
sys.exit(app.exec_())

Upvotes: 1

Related Questions