Hello.
Please see the attached program. It's a simple todo program I wrote.
"Kartavya" means "Something that needs to be done" in Sanskrit.
I want to know why the spacing between the list items increases when
items are added and how I can stop that from happening. The spacing
between the list items must be constant whether there are items or not.
Thank you list.
Shriramana Sharma.
#! /usr/bin/env python
# Copyright Shriramana Sharma 2007
# This program may be used under the GNU General Public License v 2.
# No warranty is provided for this program.
import sys
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
app = QApplication ( sys . argv )
# Laying out the GUI of the application
window = QWidget ()
mainLayout = QVBoxLayout ()
mainLayout . setMargin ( 15 )
mainLayout . setSpacing ( 15 )
mainLayout . setSizeConstraint ( QLayout . SetFixedSize )
checkboxes = list ( QCheckBox () for i in range ( 10 ) )
for i in range ( 10 ) :
checkboxes [ i ] . setEnabled ( False )
checkboxes [ i ] . setContentsMargins ( 5, 5, 5, 5 )
mainLayout . addWidget ( checkboxes [ i ] )
buttonsLayout = QHBoxLayout ()
addButton = QPushButton ( "&Add" )
editButton = QPushButton ( "&Edit" )
removeButton = QPushButton ( "&Remove" )
buttonsLayout . addWidget ( addButton )
buttonsLayout . addWidget ( editButton )
buttonsLayout . addWidget ( removeButton )
mainLayout . addLayout ( buttonsLayout )
# Populating the checkboxes with the kartavya items from the file
file = QFile ( QDir . homePath () + "/.kartavya" )
file . open ( QIODevice . ReadOnly | QIODevice . Text )
filestream = QTextStream ( file )
i = 0
while not filestream . atEnd () and i < 10 :
checkboxes [ i ] . setEnabled ( True )
checkboxes [ i ] . setText ( filestream . readLine () )
i += 1
file . close ()
nextAvailableCheckboxIndex = i
# Defining the slots
def addItem () :
global nextAvailableCheckboxIndex
if nextAvailableCheckboxIndex == 10 :
QMessageBox . critical ( window, "The kartavya list is full!", "The current version of Kartavya only supports 10 items. Please delete an existing item if possible to add a new item." )
return
else :
outputTuple = QInputDialog . getText ( window, "Add a kartavya item", "Please enter a kartavya item:" )
if outputTuple [ 1 ] : # means ok was pressed
item = outputTuple [ 0 ] . trimmed ()
if len ( item ) == 0 :
QMessageBox . critical ( window, "Empty entry!", "You have entered an empty entry! This will not be added to the kartavya list." )
return
checkboxes [ nextAvailableCheckboxIndex ] . setEnabled ( True )
checkboxes [ nextAvailableCheckboxIndex ] . setText ( item )
nextAvailableCheckboxIndex += 1
def editItems () :
for i in range ( 10 ) :
if checkboxes [ i ] . isChecked () :
checkboxes [ i ] . setChecked ( False )
outputTuple = QInputDialog . getText ( window, "Edit kartavya item #" + QString . number ( i + 1 ), "Please edit the kartavya item #" + QString . number ( i + 1 ), QLineEdit . Normal, checkboxes [ i ] . text () )
if outputTuple [ 1 ] : # means ok was pressed
item = outputTuple [ 0 ] . trimmed ()
if len ( item ) == 0 :
QMessageBox . critical ( window, "Empty entry!", "You have emptied the current entry! This edit will not be applied. Please use the remove command to remove items." )
continue
checkboxes [ i ] . setText ( item )
def removeItems () :
global nextAvailableCheckboxIndex
if QMessageBox . question ( window, "Remove kartavya items", "Really delete all the selected kartavya items? Items once deleted cannot be recovered.", QMessageBox . Yes | QMessageBox . No ) == QMessageBox . No :
return
newList = list ( QString () for i in range ( 10 ) )
counter = 0
for i in range ( 10 ) :
checkboxes [ i ] . setEnabled ( False )
if checkboxes [ i ] . isChecked () :
checkboxes [ i ] . setChecked ( False )
else :
newList [ counter ] = checkboxes [ i ] . text ()
counter += 1
nextAvailableCheckboxIndex = 0
for i in range ( 10 ) :
checkboxes [ i ] . setText ( newList [ i ] )
if newList [ i ] != "" :
checkboxes [ i ] . setEnabled ( True )
nextAvailableCheckboxIndex += 1
def writeKartavyaFile () :
file . open ( QIODevice . WriteOnly | QIODevice . Text ) # the previous contents of the file are lost!
for i in range ( 10 ) :
t = checkboxes [ i ] . text ()
if t != "" :
filestream << t << "\n"
file . close ()
# Making the signal-slot connections
QObject . connect ( addButton, SIGNAL ( "clicked ()" ), addItem )
QObject . connect ( editButton, SIGNAL ( "clicked ()" ), editItems )
QObject . connect ( removeButton, SIGNAL ( "clicked ()" ), removeItems )
QObject . connect ( app, SIGNAL ( "aboutToQuit ()" ), writeKartavyaFile )
# Displaying the GUI
window . setLayout ( mainLayout )
window . setWindowTitle ( "Kartavya" )
window . show ()
sys . exit ( app . exec_ () )
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt