[SOLVED] Differences between attach a Button in QWidget with layout or without it.



  • I'm in my first "project" with PyQt and I find out with something I can't explain. I have two different widgets but I want them to have the same button in the same place because I'm interested on access to one functionality in any window I have in my "project".

    So in one widget (Wid1) I have the buttons attached to the widget and I place them where I want.

    By the other side, I have a widget (Wid2) with a QGridLayout which have the same button as the first widget.

    My question is, why Wid2 with the layout, if I attach the button to it and I move it in the same place as in the Wid1 the button changes his shape? Why happens that? And also (but less important) there's anyway to solve it? Because it would be very comftable for me having the possibility to place buttons that ignore the layouts.

    Here is the code:

    @
    class Wid1(QtGui.QWidget):

    def init(self):

    super(Inici, self).init()

    self.btn1 = QtGui.QPushButton(u'One', self)
    self.btn1.setStyleSheet('Text-align:center')
    self.btn2 = QtGui.QPushButton(u'Two', self)
    self.btn2.setStyleSheet('Text-align:center')
    self.btn3 = QtGui.QPushButton(u'Three', self)
    self.btn3.setStyleSheet('Text-align:center')
    self.btn4 = QtGui.QPushButton(u'Four', self)
    self.btn4.setStyleSheet('Text-align:center')

    self.But = QtGui.QToolButton(self)
    self.But.setIcon( QtGui.QIcon('Imatges/z.png') )
    self.But.setIconSize(QtCore.QSize(40,40))
    self.But.move(544,9)

    l=[self.btn1,self.btn2,self.btn3,self.btn4]
    i = 70
    Rect = QtCore.QRect(0,0,150,40)
    for b in l:
    Rect.moveCenter( QtCore.QPoint(300,i) )
    b.setGeometry(Rect)
    i += 80

    class Wid2(QtGui.QWidget):

    def init(self):
    super(Personal, self).init()

    self.CP_group = QtGui.QGroupBox("Cat1")
    self.CP = QtGui.QLineEdit()
    self.CP.setMaxLength(5)
    self.CP.setPlaceholderText("XXXXX")
    layout = QtGui.QVBoxLayout()
    layout.addWidget(self.CP)
    self.CP_group.setLayout(layout)
    self.CP_group.setCheckable(True)
    self.CP_group.setChecked(False)

    self.N_group = QtGui.QGroupBox("Cat2")
    self.N = QtGui.QLineEdit()
    self.N.setMaxLength(5)
    self.N.setPlaceholderText("XXXXX")
    layout = QtGui.QVBoxLayout()
    layout.addWidget(self.N)
    self.N_group.setLayout(layout)
    self.N_group.setCheckable(True)
    self.N_group.setChecked(False)

    self.Ext = QtExtras.ExtendedQLabel(self)
    pix = QtGui.QPixmap('Imatges/x.gif')
    self.Ext.setPixmap( pix )

    self.But = QtGui.QToolButton()
    self.But.setIcon( QtGui.QIcon('Imatges/z.png') )
    self.But.setIconSize( QtCore.QSize(40,40) )

    self.Grid = QtGui.QGridLayout()
    self.Grid.addWidget(self.Ext,0,0)
    self.Grid.addWidget(self.CP_group,2,1)
    self.Grid.addWidget(self.N_group,2,3)
    self.Grid.addWidget(self.But,0,4,Qt.AlignRight)
    self.Grid.setRowMinimumHeight(1,50)
    self.Grid.setRowMinimumHeight(1,50)
    self.Grid.setRowMinimumHeight(3,250)
    self.Grid.setColumnMinimumWidth(0,100)
    self.Grid.setColumnMinimumWidth(2,50)
    self.Grid.setColumnMinimumWidth(4,100)
    self.setLayout(self.Grid)
    @

    Thanks for reading.


  • Moderators

    Hi and welcome to Devnet!

    You can get the behaviour you want in a layout using spacers.
    Here is an old but good tutorial on using them:
    "Link":http://doc.qt.digia.com/2.3/designer/chap4_2.html

    Tip: using qt creator with designer allows you to set layouts and experiment in a fun way.



  • [quote author="Eddy" date="1398065500"]Hi and welcome to Devnet!

    You can get the behaviour you want in a layout using spacers.
    Here is an old but good tutorial on using them:
    "Link":http://doc.qt.digia.com/2.3/designer/chap4_2.html

    Tip: using qt creator with designer allows you to set layouts and experiment in a fun way.

    [/quote]

    Ok, thanks for the asnwer. They are really userful to place something where you want but that doesn't explain why happens what I was asking.
    Also, doesn't seem spacers can change the shape of a button, they only will "move" the button. And "my problem" is that when I attach the "Button2" to the widget, it changes its shape, even if I set it manually.


  • Moderators

    a good explanation is in "the QGridLayout docs":http://qt-project.org/doc/qt-4.8/qgridlayout.html#details

    QGridLayout is a special beast that does a lot automatically for us.

    Your QPushButtons have a sizehint based on the text and font it contains.
    the horizontal policy by default is : minimum (width is equal to sizehint, but the button can expand if there is space for it in the layout.
    and
    the vertical policy is fixed. normally the shape of your buttons shouldn't change heights.

    So when using the gridlayout your dialog window will be divided automatically in a number of rows and columns.

    bq. doesn’t seem spacers can change the shape of a button, they only will “move” the button

    if you put a spacer in the gridlayout, it can create a new column and claim the available space so that the buttons' width remain at their sizehint. Yout get the feeling that the buttons only move then.

    I hope this makes it more clear for you.

    I suggest you read and experiment with the info from the link I gave you. It's really worth the time and effort.



  • Thank you, Eddy! It's was a wonderful explanation. also after various experiemnts I think I understand it quite good :)


  • Moderators

    My pleasure!

    Happy coding


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.