Rapid-Q Documentation by William Yu (c)1999-2000 Appendix A: QLISTVIEW


QLISTVIEW Компонент

QListView provides 3 different ways of displaying items on a form. Items can be displayed in columns with column headers and sub-items, or individually with large/small icons.

QListView Internal Types
   TYPE ItemType            '' ie. ListView.Item(0).Caption
     Caption AS STRING
     Checked AS INTEGER     '' If CheckBoxes is enabled and item is checked
     Handle AS INTEGER      '' Read-only
     ImageIndex AS INTEGER
     Index AS INTEGER       '' Read-only
     Selected AS INTEGER
     StateIndex AS INTEGER
   END TYPE

   TYPE ColumnType          '' ie. ListView.Column(0).Width
     Caption AS STRING
     Width AS INTEGER
   END TYPE
QListView Свойства
ПолеТип R/WПо умолчанию




AlignINTEGER RWalNone
Align определяет как компонент располагается на родительской форме
BorderStyleINTEGER RWbsSingle
CheckBoxesINTEGER RWFalse
CheckBoxes specifies whether checkboxes appear next to the items in the listview.
ColorINTEGER RW
ColumnARRAY of ColumnType RW
ColumnClickINTEGER RWTrue
ColumnClick determines whether the column header behaves like a button.
ColumnsCountINTEGER RW
CursorINTEGER RWcrDefault
EnabledINTEGER RWTrue
FontQFONT W
GridLinesINTEGER RWFalse
GridLines determines whether lines are drawn separating items in the list.
HandleINTEGER R
HeightINTEGER RW
HideSelectionINTEGER RWFalse
HideSelection determines whether the listview gives a visual indication of which item is selected when focus shifts to another control.
HintSTRING RW
HotTrackINTEGER RWFalse
HotTrack specifies whether list items are highlighted when the mouse passes over them.
ItemARRAY of ItemType RW
ItemCountINTEGER RW
ItemIndex INTEGER
Index of selected Item
RW
LargeImagesQIMAGELIST W
LeftINTEGER RW0
MultiSelectINTEGER RWFalse
ParentQFORM/QPANEL/QTABCONTROL W
PopupMenuQPOPUPMENU W
ReadOnlyINTEGER RWFalse
RowSelectINTEGER RWFalse
RowSelect determines whether an entire row in the listview can be selected.
SelCountINTEGER RW
SelectedARRAY of INTEGER RW
ShowColumnHeadersINTEGER RWTrue
ShowHintINTEGER RWFalse
SmallImagesQIMAGELIST W
SortTypeINTEGER RWstNone
SortType determines if and how the items in the list are automatically sorted.
0 = stNone -- No sorting is done.
2 = stText -- Items are sorted according to their caption property.
StateImagesQIMAGELIST W
SubItem2D ARRAY of STRING RW
TabOrderINTEGER RW
TagINTEGER RW
TopINTEGER RW0
WidthINTEGER RW
ViewStyleINTEGER RWvsIcon
VisibleINTEGER RWTrue


QListView Методы
МетодТип ОписаниеПараметры
AddColumnsSUBIAdd column headers to listviewSTRING, Infinite
AddItemsSUBIДобавить элемент к listviewSTRING, Infinite
AddSubItemSUB (Index%, String$)Add sub item to Index%2
ClearSUBClear all items in listview0
ClearColumnsSUBClear all columns in listview0
DelItemsSUBIDelete items from listview by ItemIndex
example:
ListView.DelItems ListView.ItemIndex
INTEGER, Infinite
DelSubItemSUB (Index%, SubIndex%)Delete subitem from listview2
InsertItemSUB (Index%, String$)Вставить элемент at Index%2
InsertSubItemSUB (Index%, SubIndex%, String$)Insert subitem3
SwapItemSUB (Index1%, Index2%)Swap items2

QListView События
СобытиеТип Происходит когда...Параметры
OnClickVOIDItem was clicked on0
OnChangeSUB (Index%, Change AS BYTE)Item changed (text, image, or state)2
OnColumnClickSUB (Column%)Column header was clicked
This sub is bugly!  Always return 0 in Column%

See example2 to solve this problem

1
OnDblClickVOIDSelection was double clicked on0
OnKeyPressedSUB (Key AS BYTE)Key was pressed1


QListView Примеры
' Simple .ZIP viewer (doesn't extract or anything useful like that).
' You're free to modify and distribute the code without restrictions.
' Written in Rapid-Q by William Yu
' Demonstrates QOpenDialog, QFileStream, QListView, QMainMenu, QMenuItem
'              and QImageList.

$APPTYPE GUI
$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"

$RESOURCE ICO_ZIP AS "ZIP.ICO"
$RESOURCE ICO_APP AS "APP.ICO"

DECLARE SUB ZipView (ZIPFile AS STRING)
DECLARE SUB FormResize
DECLARE SUB OpenClick
DECLARE SUB ExitClick
DECLARE SUB IconClick
DECLARE SUB SmallIconClick
DECLARE SUB ReportClick
DECLARE SUB ItemDblClick

CONST SIG = &H04034B50         ' ZIP Signature/ID

TYPE ZFHeader
  Signature AS LONG
  Version AS WORD
  GPBFlag AS WORD              ' ?
  Compress AS WORD             ' Compression types
  DateTime AS LONG             ' Packed format
  CRC32 AS LONG
  CSize AS LONG                ' Compressed size
  USize AS LONG                ' Uncompressed size
  FileNameLen AS LONG
END TYPE

TYPE PackedDateType            ' I think this is it:
  Year AS SHORT                ' 7 bits
  Month AS BYTE                ' 4 bits
  Day AS BYTE                  ' 5 bits
  Hour AS BYTE                 ' 5 bits
  Minute AS BYTE               ' 6 bits
  Secs AS BYTE                 ' 5 bits
END TYPE                       ' Total = 32 bits

DIM CompType(0 TO 9) AS STRING
    CompType(0) = "Stored"
    CompType(1) = "Shrunk"
    CompType(2) = "Reduced1"
    CompType(3) = "Reduced2"
    CompType(4) = "Reduced3"
    CompType(5) = "Reduced4"
    CompType(6) = "Imploded"
    CompType(7) = "Defalted"
    CompType(8) = "DeflatN"      '' Could be DeflatX as well...
    CompType(9) = "DeflatX"

DIM OpenItem AS QMenuItem
    OpenItem.Caption = "&Open"
    OpenItem.OnClick = OpenClick
DIM BreakItem AS QMenuItem
    BreakItem.Caption = "-"
DIM ExitItem AS QMenuItem
    ExitItem.Caption = "E&xit"
    ExitItem.OnClick = ExitClick
DIM IconItem AS QMenuItem
    IconItem.Caption = "vs&Icon"
    IconItem.RadioItem = True
    IconItem.OnClick = IconClick
DIM SmallIconItem AS QMenuItem
    SmallIconItem.Caption = "vs&SmallIcon"
    SmallIconItem.RadioItem = True
    SmallIconItem.OnClick = SmallIconClick
DIM ReportItem AS QMenuItem
    ReportItem.Caption = "vs&Report"
    ReportItem.RadioItem = True
    ReportItem.Checked = True
    ReportItem.OnClick = ReportClick

DIM FileMenu AS QMenuItem
    FileMenu.Caption = "&File"
    FileMenu.AddItems OpenItem, BreakItem, ExitItem
DIM ViewMenu AS QMenuItem
    ViewMenu.Caption = "&View"
    ViewMenu.AddItems IconItem, SmallIconItem, ReportItem

DIM ImageList1 AS QImageList
    ImageList1.Height = 32
    ImageList1.Width = 32
    ImageList1.AddICOHandle ICO_APP
    ImageList1.AddICOHandle ICO_ZIP
DIM ImageList2 AS QImageList            ' Scale 16x16
    ImageList2.Height = 16
    ImageList2.Width = 16
    ImageList2.AddICOHandle ICO_APP
    ImageList2.AddICOHandle ICO_ZIP

CREATE Form AS QForm
  ICOHandle = ICO_ZIP
  Center
  OnResize = FormResize
  Height = 330
  Width = 525
  Caption = "Simple .ZIP Viewer for Rapid-Q"
  CREATE MainMenu AS QMainMenu
    AddItems FileMenu, ViewMenu
  END CREATE
  CREATE ListView AS QListView
    Width = Form.ClientWidth
    Height = Form.ClientHeight
    SmallImages = ImageList2
    LargeImages = ImageList1
    ViewStyle = vsReport
    AddColumns "FileName","Length","Метод","Size","Rate","Date","Time","CRC-32"
    Column(0).Width = 200
    Column(4).Width = 40
    Column(5).Width = 70
    Column(7).Width = 70
    OnDblClick = ItemDblClick
  END CREATE
  ShowModal
END CREATE


'------------------------------------------------------------------

SUB ZipView (ZIPFile AS STRING)
  DIM Hdr      AS ZFHeader
  DIM ZF       AS QFileStream
  DIM PD       AS PackedDateType
  DIM FileName AS STRING
  DIM Index    AS INTEGER

  IF ZF.Open(ZIPFile, fmOpenRead) = False THEN
    ShowMessage("Problem with reading "+ZIPFile)
    EXIT SUB
  END IF

  ListView.Clear
  Hdr.Signature = ZF.ReadNum(4)

  Index = 0
  WHILE Hdr.Signature = SIG
    Hdr.Version  = ZF.ReadNum(2)
    Hdr.GPBFlag  = ZF.ReadNum(2)
    Hdr.Compress = ZF.ReadNum(2)
    Hdr.DateTime = ZF.ReadNum(4)
    Hdr.CRC32 = ZF.ReadNum(4)
    Hdr.CSize = ZF.ReadNum(4)
    Hdr.USize = ZF.ReadNum(4)
    Hdr.FileNameLen = ZF.ReadNum(4)

    FileName = ZF.ReadStr(Hdr.FileNameLen)

    PD.Year = ((Hdr.DateTime SHR 25) AND &H7F) + 1980
    PD.Month = (Hdr.DateTime SHR 21) AND &H0F
    PD.Day = (Hdr.DateTime SHR 16) AND &H1F
    PD.Hour = (Hdr.DateTime SHR 11) AND &H1F
    PD.Minute = (Hdr.DateTime SHR 5) AND &H3F
    PD.Secs = Hdr.DateTime AND &H1F
    ListView.AddItems FileName
    IF INSTR(UCASE$(FileName), ".ZIP") THEN
      ListView.Item(Index).ImageIndex = 1
    END IF
    ListView.AddSubItem Index, STR$(Hdr.USize)
    ListView.AddSubItem Index, CompType(Hdr.Compress)
    ListView.AddSubItem Index, STR$(Hdr.CSize)
    IF Hdr.USize = 0 THEN
      ListView.AddSubItem Index, "----"
    ELSE
      ListView.AddSubItem Index, STR$(INT((Hdr.USize - Hdr.CSize) / Hdr.USize * 100))+"%"
    END IF
    ListView.AddSubItem Index, STR$(PD.Month)+"-"+STR$(PD.Day)+"-"+STR$(PD.Year)
    ListView.AddSubItem Index, STR$(PD.Hour)+":"+STR$(PD.Minute)
    ListView.AddSubItem Index, LCASE$(HEX$(Hdr.CRC32))
    ZF.Seek(Hdr.CSize, soFromCurrent)
    Hdr.Signature = ZF.ReadNum(4)
    Index = Index + 1
  WEND

  IF Index = 0 THEN
     '-- Likely not a .ZIP file
     ShowMessage(ZIPFile+" is not a valid ZIP file!")
  END IF
END SUB

SUB OpenClick
  DIM OpenDialog AS QOpenDialog

  OpenDialog.InitialDir = CurDir$
  OpenDialog.Filter = "ZIP Files (*.zip)|*.zip|All Files (*.*)|*.*"

  IF OpenDialog.Execute THEN
    ZipView(OpenDialog.FileName)
  END IF
END SUB

SUB ExitClick
  Form.Close
END SUB

SUB FormResize
  ListView.Height = Form.ClientHeight
  ListView.Width = Form.ClientWidth
END SUB

SUB IconClick
  IconItem.Checked = True
  ListView.ViewStyle = vsIcon
END SUB

SUB SmallIconClick
  SmallIconItem.Checked = True
  ListView.ViewStyle = vsSmallIcon
END SUB

SUB ReportClick
  ReportItem.Checked = True
  ListView.ViewStyle = vsReport
END SUB

SUB ItemDblClick
  IF ListView.ItemIndex >= 0 THEN
    ShowMessage("Can't extract "+ListView.Item(ListView.ItemIndex).Caption);
  END IF
END SUB

Пример2
$TYPECHECK ON
 $INCLUDE "RAPIDQ.INC"

 TYPE RECTA
 Left As Long
 Top As Long
 Right As Long
 Bottom As Long 
 END TYPE 

 
 CONST LVM_FIRST = &H1000
 Const LVM_GETHEADER = (LVM_FIRST + 31)
 Const HDM_FIRST = &H1200
 Const HDM_HITTEST = HDM_FIRST + 6

 Declare Function SendMessagea Lib "user32" Alias_
 "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long,_
 ByVal wParam As Integer, ByVal lParam As LONG) As Long

 Declare Function GetWindowRect Lib "user32" Alias_
 "GetWindowRect" (ByVal hwnd As Long, lpRect As RECTA)AS Long

 Declare Function GetParent Lib "user32"_
 Alias "GetParent" (ByVal hwnd As Long) As Long

 DECLARE SUB OK(COLUMN%, sender as qlistview)

 dim r as rectA
 dim rh as rectA

 
 dim IDcolumn as integer
 dim ccount as integer
 CREATE Form AS QForm
 Center
 Caption = "Sorting Listview Click in the Buttons Headers"
 Height = 330
 Width = 525
 
 CREATE ListView AS QListView
 ColumnClick = TRUE
 ALIGN=5
 Gridlines=1
 Width = Form.ClientWidth
 Height = Form.ClientHeight
 ViewStyle = vsReport
 AddColumns "Group","Lead Guitar","Rithms Guitar","Bass","Drums", "Others"
 Column(0).Width = 110
 Column(1).Width = 80
 Column(2).Width = 80
 Column(3).Width = 80
 Column(4).Width = 80
 Column(5).Width = 80
 
 AddItems "The Rolling Stones"
 AddSubItem 0, "Keith"
 AddSubItem 0, "Brian"
 AddSubItem 0, "Bill"
 AddSubItem 0, "Charlie"
 AddSubItem 0, "Mick"

 AddItems "The Beatles"
 AddSubItem 1, "George"
 AddSubItem 1, "John"
 AddSubItem 1, "Paul"
 AddSubItem 1, "Ringo"
 
 AddItems "Yes"
 AddSubItem 2, "Steve"
 AddSubItem 2, "Jon"
 AddSubItem 2, "Chris"
 AddSubItem 2, "Alan"
 AddSubItem 2, "Rick"
 
 AddItems "Nirvana"
 AddSubItem 3, "Kurt"
 AddSubItem 3, ""
 AddSubItem 3, "Kris"
 AddSubItem 3, "Dave"

 ONCOLUMNCLICK=OK
 END CREATE
 END CREATE 

 
 Form.ShowModal

 SUB OK(COLUMN%, sender as qlistview)

with sender

defint i,cw,lvh,cc,hh,parw

 lvh=ListView.Handle
 hh=sendMESSAGEa(lvh, (LVM_GETHEADER),0,0)'get handler of header 
 ccount=sendMESSAGEa (  hh, (HDM_FIRST + 0),0, 0) 'get number of columns 
 parw=getparent(lvh) 
 getWINDOWrect(hh,Rh) ' rect of  header !!' 

 cc=0:cw=0
 
 for i=0 to ccount-1
 cw=.column(i).width
 cc=cw+cc
 if cc+Rh.left>screen.mousex then exit for
 next
 
 idcolumn=i

print "idcolumn=",idcolumn

end with 
 END SUB
 

Предыдущий Компонент Содержание Следующий Компонент