Pritpal,

I have a suggestion about methods that return pointers and i would like to know your opinion (and the opinion of the others developers too).

While a function return a pointer, my suggestion is that methods must return objects and not pointers.

See this example:

Class TQMainWindow

METHOD menuBar() INLINE Qt_QMainWindow_menuBar( ::pPtr )

The function Qt_QMainWindow_menuBar() return a pointer to a QMenuBar object.

The method TQMainWindow:menuBar() can be changed to return a object of the class TQMenuBar:

METHOD menuBar() INLINE TQMenuBar():NewFromPointer( Qt_QMainWindow_menuBar( ::pPtr ) )

Method NewFromPointer (can be another name) can be implemented in TQObject class:

METHOD NewFromPointer (p) CLASS TQObject
  ::pointer := p
Return self

If the developer need a pointer, can use the function. Using classes, the developers get objects.

See this sample (adapted from my blog):

========== begin code ==========

#include "qt.ch"

Procedure Main ()

  Local oApplication
  Local oMainWindow
  Local oMenuBar
  Local oMenu1
  Local oActionNew
  Local oActionOpen
  Local oActionSave
  Local oMenu2
  Local oActionCut
  Local oActionCopy
  Local oActionPaste
  Local oMenu3
  Local oActionAbout
  Local oActionAboutQt
  Local oStatusBar
  Local oToolBar
  Local oActionTBNew
  Local oActionTBOpen
  Local oActionTBSave
  Local oActionTBSeparator
  Local oActionTBCut
  Local oActionTBCopy
  Local oActionTBPaste

  oApplication := QApplication():New()

  oMainWindow := QMainWindow():New()
  oMainWindow:SetWindowTitle("Testing Harbour and Qt")
  oMainWindow:Resize(320, 240)

  oStatusBar := oMainWindow:StatusBar()

  oMenuBar := oMainWindow:MenuBar()

  oMenu1 := oMenuBar:AddMenu("File")

  oActionNew := oMenu1:AddAction("images\new.png","New")
  oActionNew:SetStatusTip("new")

  oActionOpen := oMenu1:AddAction("images\open.png","Open")
  oActionOpen:SetStatusTip("open")

  oActionSave := oMenu1:AddAction("images\save.png","Save")
  oActionSave:SetStatusTip("save")

  oMenu2 := oMenuBar:AddMenu("Edit")

  oActionCut := oMenu2:AddAction("images\cut.png","Cut")
  oActionCut:SetStatusTip("cut")

  oActionCopy := oMenu2:AddAction("images\copy.png","Copy")
  oActionCopy:SetStatusTip("copy")

  oActionPaste := oMenu2:AddAction("images\paste.png","Paste")
  oActionPaste:SetStatusTip("paste")

  oMenu3 := oMenuBar:AddMenu("Help")

  oActionAbout := oMenu3:AddAction("About this sample")
  oActionAbout:SetStatusTip("about this sample")

  oActionAboutQt := oMenu3:AddAction("About Qt")
  oActionAboutQt:SetStatusTip("about Qt")

  oToolBar := oMainWindow:AddToolBar("File")

  oActionTBNew := oToolBar:AddAction("images\new.png","New")
  oActionTBNew:SetStatusTip("new")

  oActionTBOpen := oToolBar:AddAction("images\open.png","Open")
  oActionTBOpen:SetStatusTip("open")

  oActionTBSave := oToolBar:AddAction("images\save.png","Save")
  oActionTBSave:SetStatusTip("save")

  oActionTBSeparator := oToolBar:AddSeparator()

  oActionTBCut := oToolBar:AddAction("images\cut.png","Cut")
  oActionTBCut:SetStatusTip("cut")

  oActionTBCopy := oToolBar:AddAction("images\copy.png","Copy")
  oActionTBCopy:SetStatusTip("copy")

  oActionTBPaste := oToolBar:AddAction("images\paste.png","Paste")
  oActionTBPaste:SetStatusTip("paste")

  oMainWindow:Show()

  oApplication:Exec()

Return

========== end code ==========

No pointers in the samples. Only objects.

If you want to maintain methods returning pointers, maybe we can use a flag to control the value returned by methods:

METHOD menuBar() INLINE IIf( flag, TQMenuBar():NewFromPointer( Qt_QMainWindow_menuBar( ::pPtr ) ), Qt_QMainWindow_menuBar( ::pPtr ) )

When passing a object as a parameter to a function, the type of the parameter can be checked to see if is a object or a pointer. Something like this:

#define GETPOINTER(p) IIf( valtype(p)=="O", p:ptr, p )


Regards,
Marcos Gambeta




_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to