[PyQt] Question about laying out widgets.

Christian chrisde88 at yahoo.de
Tue Mar 31 14:23:46 BST 2009


Hi Gabriele,
I have pasted you the corrected version below.

Some general rule, you have to decide, if something has to be set up 
once or every time you do a specific action, add a widget. For example, 
you don't have to set the spacing of a layout whenever you add a widget. 
Do it when you create the layout. But on the other hand, you have to 
create new widgets when you add a new line.

Then instead of

s = ('abc'\
    + 'def')

simplify it to:

s = ('abc'
    'def')

you don't need the + and escape \ if you surround it with brackets.


I hope I could simplify you program a bit, but you have done very well 
with your corrections, you only missed to create the widget in the 
'pressed' handler.


Christian





import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
 
class WizAndChipsCal(QWidget):
 
    def __init__(self, parent = None):
        QWidget.__init__(self)
        self._labels = []
        self._edits = []
       
        self.setWindowTitle("Wiz and Chips Calendar")
        self.setWindowIcon(QIcon("C:/Python26/PyQt/Icon/date.png"))
        self.setToolTip("Hello to this Wiz and Chips fancy calendar!")
       
        self.title = ("<font color=red size=3><b>"
                "Wiz and Chips Pushable Calendar!"
                "</font></b>")
        self.Label = QLabel(self.title)
        self.Label.setAlignment(Qt.AlignCenter | Qt.AlignJustify)
 
        self.calendar = QCalendarWidget()
        self.calendar.setGridVisible(1)
        self.calendar.setMinimumHeight(180)
        self.calendar.setMaximumHeight(180)
        self.calendar.setMaximumWidth(250)
        self.connect(self.calendar,
                SIGNAL('selectionChanged()'),
                self.SelDate)
 
 
        self.AddButton = QPushButton("&AddProject")
        self.connect(self.AddButton,
                SIGNAL('pressed()'),
                self.AddProj)
 
        self.dateLabel = QLabel("Date:")
        self.dateLabel.setMaximumWidth(80)
        CurrDate = QDate.currentDate()
        self.date = QDateEdit(CurrDate)
        self.date.setCalendarPopup(1)
        self.date.setMaximumWidth(80)

        self.summaryBox = QGroupBox("Project Management Layout")
        self.summaryBoxLayout = QFormLayout(self.summaryBox)
        self.summaryBoxLayout.setSpacing(1)
        self.summaryBox.setLayout(self.summaryBoxLayout)
     
        self.CloseButton = QPushButton("&Quit")
        self.CloseButton.setToolTip("<font color=red size=2><b>"
            "Press here to Quit</font></b>")
        self.CloseButton.setMaximumSize(50, 25)
 
        GeneralLayout = QGridLayout()
        GeneralLayout.addWidget(self.Label, 0, 0)
        GeneralLayout.addWidget(self.AddButton, 1,0)
        GeneralLayout.addWidget(self.calendar, 1, 1)
        GeneralLayout.addWidget(self.CloseButton, 4, 0)
        GeneralLayout.addWidget(self.summaryBox, 3, 0)
        self.setLayout(GeneralLayout)
 
        self.connect(self.CloseButton, SIGNAL("pressed()"),
                self.close)
          
    def moveEvent(self, event):
        self.setWindowOpacity(0.7)
        QTimer.singleShot(50, self.opac)
         
    def opac(self):
        self.setWindowOpacity(1)
      
    def closeEvent(self, event):
        self.CloseDialog = QMessageBox.question(self,
                "The application is being closed",
                "Do you really want to exit?",
                QMessageBox.Save|QMessageBox.Yes|QMessageBox.Discard,
                QMessageBox.Discard)
                           
        if self.CloseDialog == QMessageBox.Yes:
            event.accept()
        elif self.CloseDialog == QMessageBox.Save or QMessageBox.Discard:
            event.ignore()
   
    def SelDate(self):
        self.SelectedDate = self.calendar.selectedDate()
        print(self.SelectedDate)
 
    def AddProj(self):
        projLabelLayout = QHBoxLayout()
        label_list = []
        projTextBoxesLayout = QHBoxLayout()
        edit_list = []
        for name, width in [('Job No.', 60), ('Cust.Job', 60),
                ('Cust.Ords.', 60), ('Product', 150)]:
            label = QLabel(name)
            label.setMaximumWidth(width)
            projLabelLayout.addWidget(label)
            label_list.append(label)
            edit = QTextEdit()
            edit.setMaximumHeight(20)
            edit.setMaximumWidth(width)
            edit.setTabChangesFocus(1)
            projTextBoxesLayout.addWidget(edit)
            edit_list.append(edit)
        self._labels.append(tuple(label_list))
        self._edits.append(tuple(edit_list))
       
        self.summaryBoxLayout.addRow(projLabelLayout)
        self.summaryBoxLayout.addRow(projTextBoxesLayout)

app = QApplication(sys.argv)
main_window = WizAndChipsCal()
main_window.show()
app.exec_()



Gabriele Visconti wrote:
> Dear Christian,
>
> thanks for your help.
> Indeed I did try to make some modifications by counting the rows and 
> creating a widget in the following row each time. However each time I 
> press the button the reference to the widget I have already created is 
> lost with a weird effect (run attachment)
> Moreover, I didn't find your attachment to the answer and would be 
> very interesting in seeing how you did solve my issue.
>
> Thanks so much,
>
> Gabriele



More information about the PyQt mailing list