SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listboxcombo.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env python
  2. """**************************************************************************
  3. ** $Id: listboxcombo.py,v 1.1 2003/05/30 17:47:57 phil Exp $
  4. **
  5. ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
  6. **
  7. ** This file is part of an example program for TQt. This example
  8. ** program may be used, distributed and modified without limitation.
  9. **
  10. ***************************************************************************"""
  11. import sys
  12. from python_tqt.qt import *
  13. FALSE = 0
  14. TRUE = 1
  15. class ListBoxCombo( TQVBox ):
  16. # Constructor
  17. # Creates child widgets of the ListBoxCombo widget
  18. def __init__( self, parent=None, name=None ):
  19. TQVBox.__init__( self, parent, name )
  20. self.setMargin( 5 )
  21. self.setSpacing( 5 )
  22. i = 0
  23. row1 = TQHBox( self )
  24. row1.setSpacing( 5 )
  25. # Create a multi-selection ListBox...
  26. self.lb1 = TQListBox( row1 )
  27. self.lb1.setSelectionMode( TQListBox.Multi )
  28. # ...insert a pixmap item...
  29. xpm = TQPixmap( "qtlogo.png" )
  30. txt = TQString()
  31. TQListBoxPixmap( self.lb1, xpm, txt )
  32. # ...and 100 text items
  33. for i in range(0, 100, 1) :
  34. xpm = TQPixmap()
  35. txt = TQString( "Listbox Item %1" ).arg( i )
  36. if not i % 4 :
  37. xpm = TQPixmap( "fileopen.xpm" )
  38. TQListBoxPixmap( self.lb1, xpm, txt )
  39. # Create a pushbutton...
  40. arrow1 = TQPushButton( " -> ", row1 )
  41. # ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
  42. self.connect( arrow1, SIGNAL("clicked()"), self.slotLeft2Right )
  43. # create an empty single-selection ListBox
  44. self.lb2 = TQListBox( row1 )
  45. row2 = TQHBox( self )
  46. row2.setSpacing( 5 )
  47. box1 = TQVBox( row2 )
  48. box1.setSpacing( 5 )
  49. # Create a non-editable Combobox and a label below...
  50. cb1 = TQComboBox( FALSE, box1 )
  51. self.label1 = TQLabel( "Current Item: Combobox Item 0", box1 )
  52. self.label1.setMaximumHeight( self.label1.sizeHint().height() * 2 )
  53. self.label1.setFrameStyle( TQFrame.Panel | TQFrame.Sunken )
  54. #...and insert 50 items into the Combobox
  55. for i in range( 0, 50, 1 ) :
  56. txt = str( TQString( "Combobox Item %1" ).arg( i ) )
  57. if i % 9 :
  58. cb1.insertItem( txt )
  59. else :
  60. cb1.listBox().insertItem( MyListBoxItem() )
  61. box2 = TQVBox( row2 )
  62. box2.setSpacing( 5 )
  63. # Create an editable Combobox and a label below...
  64. cb2 = TQComboBox( TRUE, box2 )
  65. self.label2 = TQLabel( "Current Item: Combobox Item 0", box2 )
  66. self.label2.setMaximumHeight( self.label2.sizeHint().height() * 2 )
  67. self.label2.setFrameStyle( TQFrame.Panel | TQFrame.Sunken )
  68. # ... and insert 50 items into the Combobox
  69. for i in range(0, 50, 1 ) :
  70. txt = str(TQString( "Combobox Item %1" ).arg( i ))
  71. if not i % 4 :
  72. cb2.insertItem( TQPixmap( "fileopen.xpm" ), txt )
  73. else :
  74. cb2.insertItem( txt )
  75. # Connect the activated SIGNALs of the Comboboxes with SLOTs
  76. self.connect( cb1, SIGNAL("activated( const TQString & )"), self.slotCombo1Activated )
  77. self.connect( cb2, SIGNAL("activated( const TQString & )"), self.slotCombo2Activated )
  78. """ SLOT slotLeft2Right
  79. * Copies all selected items of the first ListBox into the second ListBox
  80. """
  81. def slotLeft2Right( self ):
  82. # Go through all items of the first ListBox
  83. for i in range( 0, self.lb1.count(), 1 ) :
  84. item = self.lb1.item( i )
  85. # if the item is selected...
  86. if self.lb1.isSelected( i ): #item.isSelected() :
  87. # ...and it is a text item...
  88. if item.pixmap() and not(item.text().isEmpty()):
  89. self.lb2.insertItem( item.pixmap(), item.text() )
  90. elif not( item.pixmap() ):
  91. self.lb2.insertItem( item.text() )
  92. elif item.text().isEmpty() :
  93. self.lb2.insertItem( item.pixmap() )
  94. """ SLOT slotCombo1Activated( const TQString &s )
  95. * Sets the text of the item which the user just selected in the
  96. * first Combobox (and is now the value of s) to the first Label.
  97. """
  98. def slotCombo1Activated( self, s ) :
  99. self.label1.setText( str(TQString( "Current Item: %1" ).arg( s ) ) )
  100. """ SLOT slotCombo2Activated( const TQString &s )
  101. * Sets the text of the item which the user just selected in the
  102. * second Combobox (and is now the value of s) to the second Label.
  103. """
  104. def slotCombo2Activated( self, s ) :
  105. self.label2.setText( str(TQString( "Current Item: %1" ).arg( s ) ) )
  106. class MyListBoxItem( TQListBoxItem ):
  107. def __init__( self, parent=None, name=None ):
  108. TQListBoxItem.__init__( self, parent, name )
  109. self.setCustomHighlighting( TRUE )
  110. def paint( self, painter ):
  111. # evil trick: find out whether we are painted onto our listbox
  112. in_list_box = 0
  113. if self.listBox() and self.listBox().viewport() == painter.device():
  114. in_list_box = 1
  115. r = TQRect( 0, 0, self.width( self.listBox() ), self.height( self.listBox() ) )
  116. brush = TQBrush( TQt.red, TQt.SolidPattern )
  117. if in_list_box and isSelected():
  118. painter.eraseRect( r )
  119. painter.fillRect( 5, 5, self.width( self.listBox() ) - 10, self.height( self.listBox() ) - 10, brush )
  120. if in_list_box and isCurrent():
  121. self.listBox().style().drawPrimitive( TQStyle.PE_FocusRect, painter, r, self.listBox().colorGroup() )
  122. def width( self, TQListBox ):
  123. return 100
  124. def height( self, TQListBox ):
  125. return 16
  126. def main( args ):
  127. a = TQApplication( args )
  128. listboxcombo = ListBoxCombo()
  129. listboxcombo.resize( 400, 270 )
  130. listboxcombo.setCaption( "TQt Example - Listboxes and Comboboxes" )
  131. a.setMainWidget( listboxcombo )
  132. listboxcombo.show();
  133. a.exec_loop()
  134. if __name__=="__main__":
  135. main(sys.argv)