[PyQt] Question about laying out widgets.

Christian chrisde88 at yahoo.de
Fri Mar 27 16:20:40 GMT 2009


Hi Gabriele,
you made some design mistakes:
- you have to create separate widgets for each row, you can't reuse them
- you have to add a top layout and assign it to the groupbox. Then you 
can add the layouts of the lines to that top layout. You can't assign 
multiple layouts to one widget.

To save a reference of the individual Widgets you could store them in a 
list:
self.job_rows = []

and then in the click handler:
self.job_rows.add((JobNo, CustJob, CustOrd, Product))

or you could create a class instead of the tuple, what you prefer.


I have attached a working program without storing a reference of the 
individual controls.



Christian


Gabriele Visconti schrieb:
>
> Hi I’m trying to build up something that for every push of a button, a 
> row of widgets is inserted within a layout. So if I press one time I 
> have a row, two times and the next row comes up, and so on…
>
> If you try running the code below you will see that if you press the 
> button one time all goes well, but on the second time the new row is 
> pasted upon the first one and not next to it.
>
> I’m quite a basic programmer in bot python and PyQt so I’m sure I’m 
> missing something basic. I tried using QformLayout insertRow method 
> but I must specify the row int and this I can’t do.
>
> Again, in case I have several rows how to delete the specific one 
> which has the focus in the moment I press a “remove row” button?
>
>  
>
> Thank you very much for your help. 
>
>  
>
-------------- next part --------------
import sys
from PyQt4.QtCore import *                       
from PyQt4.QtGui import *
 
class WizAndChipsCal(QWidget):
 
        def __init__(self, parent = None):
                QWidget.__init__(self)
                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 = QVBoxLayout()
		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):
  
                JobLabel = QLabel("Job No.")
                JobLabel.setMaximumWidth(60)
                JobNo = QTextEdit()
                JobNo.setMaximumHeight(20)
                JobNo.setMaximumWidth(60)
                JobNo.setTabChangesFocus(1)
                CustJobLabel = QLabel("Cust.Job")
                CustJobLabel.setMaximumWidth(60)
                CustJob = QTextEdit()
                CustJob.setMaximumHeight(20)
                CustJob.setMaximumWidth(60)
                CustJob.setTabChangesFocus(1)
                CustOrdLabel = QLabel("Cust.Ord.")
                CustOrdLabel.setMaximumWidth(60)
                CustOrd = QTextEdit()
                CustOrd.setMaximumHeight(20)
                CustOrd.setMaximumWidth(60)
                CustOrd.setTabChangesFocus(1)
                ProductLabel = QLabel("Product")
                ProductLabel.setMaximumWidth(100)
                Product = QTextEdit()
                Product.setMaximumHeight(20)
                Product.setMaximumWidth(150)
                Product.setTabChangesFocus(1)
                             
                projTextBoxesLayout = QHBoxLayout()
                projTextBoxesLayout.addWidget(JobNo)
                projTextBoxesLayout.addWidget(CustJob)
                projTextBoxesLayout.addWidget(CustOrd)
                projTextBoxesLayout.addWidget(Product)
 
                projLabelLayout = QHBoxLayout()
                projLabelLayout.addWidget(JobLabel)
                projLabelLayout.addWidget(CustJobLabel)
                projLabelLayout.addWidget(CustOrdLabel)
                projLabelLayout.addWidget(ProductLabel)
               
                self.summaryBoxLayout.addLayout(projLabelLayout)
                self.summaryBoxLayout.addLayout(projTextBoxesLayout)
               
               
app = QApplication(sys.argv)
main_window = WizAndChipsCal()
main_window.show()
app.exec_()


More information about the PyQt mailing list