jerous86 / nimqt

Qt bindings for nim
GNU General Public License v2.0
93 stars 6 forks source link

Argument in Slot #18

Closed Martinix75 closed 1 year ago

Martinix75 commented 1 year ago

Good morning, Today I am writing to you, because I can't understand (but it is also true that yesterday I celebrated too much) what to put as a targument to a slot, in the example below I would like to send a string (or a qstring) which he will then view in QLabel, but not me works!

import nimqt
import nimqt/[qdialog,qgridlayout, qlabel, qpushbutton]
import os

nimqt.init
let miApp = newQApplication(commandLineParams())

inheritQObject(Bottonex, QPushButton):
  slot_defer cambia_1(testo: const_var QString):#<-- slot recivied string (o qustring)
    echo("-->: ", testo)
    label_1.setText(testo)

let bottone_1 = newBottonex()
bottone_1.setText(Q "Bottone 1")
let bottone_2 = newBottonex()
bottone_2.setText(Q "Bottone 2")
let bottone_3 = newBottonex()
bottone_3.setText(Q "Bottone 3")
let label_1 = newQLabel(Q "-----")
label_1.setAlignment(newQFlags(Qt_AlignmentFlag.AlignHCenter)) #allinea al centro

let finestra: ptr QDialog = newQDialog() 
finestra.makelayout:
  - useObject bottone_1:
      connect(SIGNAL "clicked()" ,bottone_1, SLOT "cambia_1(???)")#<-- I don't know what to put here, but I would like to send a string (or qstring)
  - useObject bottone_2:
      connect(SIGNAL "clicked()" ,bottone_2, SLOT "cambia_1(???)")#<-- I don't know what to put here, but I would like to send a string (or qstring)
  - useObject bottone_3:
      connect(SIGNAL "clicked()" ,bottone_3, SLOT "cambia_1(???)")#<-- I don't know what to put here, but I would like to send a string (or qstring)
  - useObject label_1

nimqt.insertAllSlotImplementations()

finestra.resize(200, 200)
finestra.show()

discard miApp.exec()

I don't know where I'm wrong, or maybe it's not possible to do it? I tried them (except the right one of course) all but nothing! Thanks for the help from Martin A.

jerous86 commented 1 year ago

This is not possible in Qt, and it's neither possible in nimqt: the parameters of the signal must match those of the slot. You'll have to figure out another way to pass the string to be available inside the slot. One option is to user get_sender inside the function cambia_1 (see examples/hello.nim for an example of get_sender), and choose the string depending on the sender.

Martinix75 commented 1 year ago

Yes, you're right, I know that for example to solve this problem with Python they are used or Lambda functions, or partial functions. I thought I had done some tricks to solve this "problem". Now I try to see if I can invest something :). Thanks always very kind. I still do not close this Issues, I see first if I can do something, otherwise I close (then if you want to close, feel free to do it). bye by Martin A.

Martinix75 commented 1 year ago

Yes with the "sender" method it works (in fact it is also used with pyqt but I forgot it). Let's say that for now I am full, satisfied. I added a small template (you will see me in the code) to make the code easier and perhaps even simpler. If you like it, you can obviously implement it in Nimqt.

import nimqt
import nimqt/[qdialog,qgridlayout, qlabel, qpushbutton]
import os

nimqt.init
let miApp = newQApplication(commandLineParams())

template nimQtSender(name: untyped; widget: untyped) = #<-- template for sender (temporary name)
  let name = cast[ptr widget](this.get_sender)

inheritQObject(Bottonex, QPushButton):
  slotDefer cambia_2():
    nimQtSender(messaggioB2, Bottonex) #or QPushButton???
    echo (repr(messaggioB2))
    label_1.setText(messaggioB2.text())

let bottone_1 = newBottonex()
bottone_1.setText(Q "Bottone 1")
let bottone_2 = newBottonex()
bottone_2.setText(Q "Bottone 2")
let bottone_3 = newBottonex()
bottone_3.setText(Q "Bottone 3")

let label_1 = newQLabel()
label_1.setText(Q "------")
label_1.setAlignment(newQFlags(Qt_AlignmentFlag.AlignHCenter)) #allinea al centro

let finestra: ptr QDialog = newQDialog() 
finestra.makelayout:
  - useObject bottone_1:
      connect(SIGNAL "clicked()" ,bottone_1, SLOT "cambia_2()")
  - useObject bottone_2:
      connect(SIGNAL "clicked()" ,bottone_2, SLOT "cambia_2()")
  - useObject bottone_3:
      connect(SIGNAL "clicked()" ,bottone_3, SLOT "cambia_2()")
  - useObject label_1

nimqt.insertAllSlotImplementations()

finestra.resize(200, 200)
finestra.show()

discard miApp.exec()

Thanks for your precious help. Hi from Martin A.

P.S. I already have another question to ask you (next week) that will be important :)