[qtcontribs] QLineEdit() how to keep space with setInputmask()?

872 views
Skip to first unread message

Jonce

unread,
Mar 25, 2016, 9:55:35 PM3/25/16
to qtcon...@googlegroups.com
​​
​Hi All

As title,
I set QLineEdit() with InputMask
to avoid
char insert  mode
and ​select all / cursor at end when gotfocus.
It works great but without space char

here is my example

v:=Space(20) // or from a field
o:=QLineEdit()
o:setText(v)
​o:setInputmask(Repl('x',len(v))​

​If the input is "This is a book."
o:text() return "Thisisabook."​

How to keep the space char?


​Regards​

Jonce

unread,
Mar 25, 2016, 10:09:14 PM3/25/16
to qtcon...@googlegroups.com
Sorry, my mistake.

Jonce

unread,
Mar 25, 2016, 10:28:25 PM3/25/16
to qtcon...@googlegroups.com
here is my sample code

Func test()
    Stat w
    Stat e
    Stat b
    w:=QMainWindow()
    w:resize(400,300)
    w:move(0,0)
    w:setStyleSheet('font-size:16px;')

    e:=QLineEdit(w) 
  
    e:move(10,10)
    e:resize(300,30)
    e:setText     ('This is a book.')
    e:setInputMask(Repl('x',40))

   b:=QPushButton(w)
    b:move(10,50)
    b:resize(300,30)
    b:setText('Show LineEdit text to window title')
    b:connect("clicked()", {||w:setWindowTitle(e:text())})

    w:show()

    //Qapplication():exec()  //if test as main()


Regards

Luigi Ferraris

unread,
Mar 26, 2016, 7:10:33 AM3/26/16
to qtcon...@googlegroups.com
Il 26/03/2016 3.28, Jonce ha scritto:
    e:setInputMask(Repl('x',40))

Hi Jonce, reading Qt docs about QLineEdit:setInputMask() you can find «The blank character are always removed from the text after editing»
When you do :setText( <theText> ) blanks are accepted, but they are removed when you do cText := text()

If your idea is to enter any character but keep length to 40, better to use :setMaxLength( 40 ) instead setInputMask.

As alternative, if you want use setInputMask try next code
e:setInputMask( REPLICATE( 'x', 40 ) + ";_" )

Regards
Luigi


Jonce

unread,
Mar 26, 2016, 10:37:00 AM3/26/16
to qtcon...@googlegroups.com
Hi Luigi,

Yes,
e:setInputMask( REPLICATE( 'x', 40 ) + ";_" )
is almost what I want (although there are reapl '_' at tail)
I want to change the inputmask or maxLength when Edit Change
for control length for screen/report space.

Because before UTF8, the ascii codepage for chinese BIG5 (Double-byte Char)
1 alphanumeric takes 1 byte in len, and 1 space on screen/report
1 chinese char takes 2 bytes in len, and 2 space on screen/report
It means both alphanumeric and chinese char are take same space at same len, no difference.

now 1 UTF8 char take 1~4 byte, It is not countable unless you know the whole unicode-utf8 rule.
Harbour offer UTF8EX to count len in char not in byte make things more smooth.

len('abc中文堃')
in accii /BIG5 =9 (1+1+1+2+2+2)
in UTF8        =15(1+1+1+?+?+?) ?=2~4
in UTF8EX      =6 (1+1+1+1+1+1)

On SCREEN / REPORT I need 9 for space (so it could be 'abc123456', or '1234567中', '一二三四1'...etc)
I need to  check and change maxlength  and char while user editing to fit 9 in Big5.
Or if qeditline has a way To not scrool left/right ? just fit the e:width (I'll make e:width just fit 9 in big5)


I don't know how to make QlineEidt without inputmask to
set cursor positon at first and no selection while gotfocus
I try e:connect(QEvent_FocusIn , {|Event| myFocusIn ( Event,e ) } )
to
e:setCursorPosition(1)
...
not work at all.
All only the first QLinedts  cursorposition is 0 when widow show
then all QLineEdit cursorposition at tail and select all.
for now, if there are no inputmask, I'll setInputMask(Repl('x',Len_in_Big5)+';_')

And thanks again about QT PRINTER
Here is my first report porting to mysql hbqt utf8ex,
preview and print both work fine.~

內置圖片 2

內置圖片 1


Regards


--
You received this message because you are subscribed to the Google Groups "QtContribs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qtcontribs+...@googlegroups.com.
To post to this group, send email to qtcon...@googlegroups.com.
Visit this group at https://groups.google.com/group/qtcontribs.
For more options, visit https://groups.google.com/d/optout.

Luigi Ferraris

unread,
Mar 26, 2016, 1:37:17 PM3/26/16
to qtcon...@googlegroups.com
Il 26/03/2016 15.36, Jonce ha scritto:
I want to change the inputmask or maxLength when Edit Change etc...
It's not very clear to me your problem and I don't have a great knowledge about codepage problems: I'm sorry.

I don't know how to make QlineEidt without inputmask to
set cursor positon at first and no selection while gotfocus
I try e:connect(QEvent_FocusIn , {|Event| myFocusIn ( Event,e ) } )
to
e:setCursorPosition(1)
...
not work at all.
All only the first QLinedts  cursorposition is 0 when widow show
then all QLineEdit cursorposition at tail and select all.
I hope next code give you an idea. It requires much space to explain different things about HbQt<->Qt, so I will confine myself to what is essential.

    your previous code here
   WITH OBJECT oLineEdit := QLineEdit( oWindow )
      :setGeometry( 10, 10, 200, 30 )

      :setInputMask( REPLICATE( "x", 40 ) + ";_" )
      :setText( "This is my book" )
      :connect( QEvent_FocusIn, { |oEv| myFocusin(oEv, oLineEdit) } )   <===(1)
   END WITH

    your next code here

FUNCTION myFocusin( oEvent, oObject )  <===(1)
   oEvent:accept() <===(2)
   oObject:setCursorPosition( 0 )
   oObject:deselect()
RETURN .F.      // .F. means don't stop event hanlder, else .T. STOP <===(3)

(1) I connect the QEvent_FocusIn but I call related function giving 2 parameters: event itself and the object. Why?
1.a) because I need the event object to do something (see 2)
1.b) because I need the object itself
to do something (see setCursorPosition and delselect)
(2) normally, you must accept the event
(3) normally, you must return .F. (it's HbQt history/trick to stop or not the event propagation)

Probably (but it's my pov), it's better to use classes instead functions while using Qt: you can get  many benefits and simplified code.


And thanks again about QT PRINTER
I'm happy to give you my little help :-)

Here is my first report porting to mysql hbqt utf8ex,
preview and print both work fine
Wow, congratulations!

Regards
Luigi

Jonce

unread,
Mar 26, 2016, 10:41:41 PM3/26/16
to qtcon...@googlegroups.com
I've tried before.

If obj:setInputMask(...),
   the cursor position is 0 and no selection while gofocus
so myFocusin() is not need.


If not setInputMask(...) ,
  myFocusin() is no work.
  cusor position always at end of line
  with  select all
  while gotfocus.


--

Luigi Ferraris

unread,
Mar 29, 2016, 8:51:05 AM3/29/16
to qtcon...@googlegroups.com
Il 27/03/2016 4.41, Jonce ha scritto:
If obj:setInputMask(...),
   the cursor position is 0 and no selection while gofocus
so myFocusin() is not need.


If not setInputMask(...) ,
  myFocusin() is no work.
  cusor position always at end of line
  with  select all
  while gotfocus.

Hi Jonce,
I hope there is someone can give you a better answer than me anyway this is my pov.

first: with or without inputmask the focusinEvent is fired on so your function is always called

second: perhaps it's a Qt bugs as reported here https://forum.qt.io/topic/51878/solved-remove-text-selection-when-qlineedit-gets-focus but I don't know why is marked as solved.
The solution it seems to override the focusInEvent method and do something (see previous link) but AFAIK we can't do.
I think is due to line 1780 to 1793 about https://github.com/qtproject/qtbase/blob/dev/src/widgets/widgets/qlineedit.cpp but I'm not sure

In atechement you can find a simple program with three widgets: QLineEdit, QPushButton, QLineEdit. Using it, enabling/disabling pieces of code, you can have an idea of Qt behaviour (or bug). I found a  contradiction: while deselect doesn't works selectAll works fine.

Regards
Luigi
oop001.prg.txt

Jonce

unread,
Mar 30, 2016, 1:49:45 AM3/30/16
to qtcon...@googlegroups.com
Hi Luigi,


About the  selectall() in QLineEdit()'s focusin,
It may not a qt bug but a qtcontribs' (not surely)

https://forum.qt.io/topic/51878/solved-remove-text-selection-when-qlineedit-gets-focus
the author SIDDI2 said it's his mistake not a bug: on the last 2 answer:

Thanks all,

Actually it is now working fine. Some other component was setting the
@
selectAll()
@

Because of that the behavior was not correct.

Now all is OK

Cheers!

Testing your code (remark the lines oEdit:setInputmask(...))
When the first QLineEdit() Focusin while the  window shows //(or the  dialog execs)
It works well ( not selectall() and cursor position is at what I set (I tested with  0 or 3) )  "Q(2)"
But afterr tab key presss, even loop to the first QLineEdit(), It don't work!
It alway selectAll() after we return .f. in myFocusin, If oEvent:reason() == Qt_TabFocusReason  "Q(1)"
That's why I guest: a bug of qtcontribs not qt.

Back to the title "how to keep space with setInputmask()"
I found a simple way

oEdit1:setInputMask( REPL('x',n) )  // for replace mode and no selecall() at foucin.
....
...
oDlg:exec()
oEdit1:setInputmask( REPL('x',n)+';_' ) //now put ';_' to keep space, and oEdit1 is hide now user won't see the undersocpe
cVal:=oEdit:Text()
It works. spaces in string is keeped.
Don't  do oEdit1:setInputMask(""), the oEdit1:text() will be blank ("") too.

Regards,

--
Reply all
Reply to author
Forward
0 new messages