jerous86 / nimqt

Qt bindings for nim
GNU General Public License v2.0
101 stars 7 forks source link

proc/SLOT visibility? #6

Closed matkuki closed 1 year ago

matkuki commented 1 year ago

Hi,

Quick question, why does this work (clicking the buttons renames the button's text):

import std/enumerate

import nimqt
import nimqt / [
    qpushbutton,
    qboxlayout,
    qmainwindow
]

nimqt.init

inheritQObject(GuiHandler, QObject):
    slot_decl on_helloWorld_clicked()

proc on_helloWorld_clicked(this: ptr GuiHandler) =
    let sender = cast[ ptr QPushButton]( this.get_sender())
    sender.setText( Q "Hello world!")

let
    app = newQApplication()
    guiHandler: ptr GuiHandler = newGuiHandler()
    win = newQMainWindow()
    main_widget = newQWidget()
    main_layout = newQVBoxLayout(parent=main_widget)
    stylesheet: QString = newQString("background: red;")
    buttons = [
        newQPushButton(Q "Click me 0!!"),
        newQPushButton(Q "Click me 1!!"),
        newQPushButton(Q "Click me 2!!"),
    ]

main_widget.setLayout(main_layout)

for i,b in enumerate(buttons):
    if i == 1:
        b.setStyleSheet(stylesheet)
    b.connect(SIGNAL "clicked()", guiHandler, SLOT "on_helloWorld_clicked()")
    main_layout.addWidget(b)

win.setCentralWidget(main_widget)
win.resize(640, 480)

win.show()
discard app.exec()

... while this doesn't invoke on_helloWorld_clicked and rename the button's text:

import std/enumerate

import nimqt
import nimqt / [
    qpushbutton,
    qboxlayout,
    qmainwindow
]

nimqt.init

inheritQObject(GuiHandler, QObject):
    slot_decl on_helloWorld_clicked()

proc on_helloWorld_clicked(this: ptr GuiHandler) =
    let sender = cast[ ptr QPushButton]( this.get_sender())
    sender.setText( Q "Hello world!")

proc main() =
    let
        app = newQApplication()
        guiHandler: ptr GuiHandler = newGuiHandler()
        win = newQMainWindow()
        main_widget = newQWidget()
        main_layout = newQVBoxLayout(parent=main_widget)
        stylesheet: QString = newQString("background: red;")
        buttons = [
            newQPushButton(Q "Click me 0!!"),
            newQPushButton(Q "Click me 1!!"),
            newQPushButton(Q "Click me 2!!"),
        ]

    main_widget.setLayout(main_layout)

    for i,b in enumerate(buttons):
        if i == 1:
            b.setStyleSheet(stylesheet)
        b.connect(SIGNAL "clicked()", guiHandler, SLOT "on_helloWorld_clicked()")
        main_layout.addWidget(b)

    win.setCentralWidget(main_widget)
    win.resize(640, 480)

    win.show()
    discard app.exec()

if isMainModule:
    main()
jerous86 commented 1 year ago

I tracked it down to using the constructor pragma. Not exactly sure why it then crashes, but it should be fixed in the latest commit.

matkuki commented 1 year ago

Excellent, it works, thanks 👍👍