jerous86 / nimqt

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

Mainwindow menus. #36

Closed Martinix75 closed 6 months ago

Martinix75 commented 7 months ago

As I anticipated, I am dealing with the Mainwindow (always for study) and I am trying the menus (and toolbaar but we will talk about this). I tried to create the menus with the "pyton" method and the creation is simple and fuynzing (also for voices, such as icons, tip and everything that can be put):

import os
import nimqt
import nimqt/qmainwindow
import nimqt/[qpushbutton, qlineedit, qaction, qtoolbar]

nimqt.init
let app1 = newQApplication(commandLineParams())
let finestra: ptr QMainWindow = newQMainWindow() #finestra = qDialog
let centrale = newQWidget()
# ------------------------------------------------------------------
let ledit = newQLineEdit()
ledit.setReadOnly(true)
ledit.setDisabled(true)
ledit.setAlignment(newQFlags(Qt_AlignmentFlag.AlignHCenter)) #allinea al centro
inheritQObject(Bottone, QPushButton):
  slot_decl cambia() 
proc cambia(this: ptr Bottone) =
  ledit.setText(Q"Hello World!!")
let bottone = newBottone() #<-- cosi non funziona lo slot/signal
bottone.setText(Q"Premi Qui")
# ------------------------------------------------------------------

let azione1 = newQAction(Q"Apri")
azione1.setIcon(newQIcon(Q "Icons/open.png"))
let azione2 = newQAction(Q"Chiudi")
azione2.setIcon(newQIcon(Q "Icons/close.png"))
let azione3 = newQAction(Q"Imposta Qualcosa..")
azione3.setIcon(newQIcon(Q "Icons/set.png"))
let azione4 = newQAction(Q"Aiuto")
azione4.setIcon(newQIcon(Q "Icons/help.png"))
let azione5 = newQAction(Q"Sotto Menu!")
azione5.setIcon(newQIcon(Q "Icons/connect.png"))
# ------------------------------------------------------------------

let barraTools = finestra.addToolBar(Q"ww")
barraTools.addAction(azione1)
barraTools.addAction(azione2)
discard barraTools.addSeparator()
barraTools.addAction(azione5)
let bottoneTool = newqpushbutton(Q"Bottone")
discard barraTools.addSeparator()
discard barraTools.addWidget(bottoneTool)
discard barraTools.addSeparator()
# ------------------------------------------------------------------
#let finestra: ptr QDialog = newQDialog() #finestra = qDialog
centrale.makeLayout:
  - use_object ledit
  - use_object bottone:
      connect(SIGNAL "clicked()", bottone, SLOT "cambia()") 

finestra.setCentralWidget(centrale)
finestra.setWindowTitle(Q"MainWindow ToolBar")
finestra.setGeometry(600, 500, 250, 150)
finestra.setWindowIcon(newQIcon(Q "Icons/panoratux.png"))
finestra.show()
discard app1.exec()

As said this works well, but the slot/signal system does not work. Then I tried the system you indicated with "makemenu", very nice even enough I understand, but: 1- How to insert icons, tips, 2- Personalized procedures.

In short, use something like "UsoBject" where the object (QAction in this case) with all its properties first declares? Because then it would be easier also to create the "toolbars". something like that (it doesn't work !! only for example):

import os

import nimqt
import nimqt/[qpushbutton,qmainwindow]
import nimqt/tools/[menu,layout]

nimqt.init

let app = newQApplication(commandLineParams())

let
    mm=newQMenu()
    btn1=newQPushButton(Q "btn1")
    btn2=newQPushButton(Q "btn2")

let azione1 = newQAction(Q"Apri")#        <---QACTION + settings!!!
azione1.setIcon(newQIcon(Q "Icons/open.png"))
azione1.setToolTip(Q "open file")

mm.makeMenu:
    * "Disabled menus"
    + "This is a disabled submenu":
        setEnabled(false)
    + "This is a tear-off enabled submenu with its children disabled":
        setTearOffEnabled(true)
        - "Action 1": setEnabled(false)
        - "Action 2": setEnabled(false)
        - "Action 3": setEnabled(false)
    * "Enable buttons"
    - "Enable btn1" as enableBtn1:
        setCheckable(true)
        setChecked(btn1.isEnabled)
        handleToggled: btn1.setEnabled(checked)
    - "Enable btn2" as enableBtn2:
        setCheckable(true)
        setChecked(btn2.isEnabled)
        handleToggled: btn2.setEnabled(checked)
    * "Quitting"
    addSeparator0()
    + "Quit":
        + "Confirm quit":
            - "Yes!":
                handleTriggered(): quit(0)

let 
    win: ptr QMainWindow = newQMainWindow()
    menuBar=newQMenuBar()
    menuFile=menuBar.addMenu(Q"File")
    menuView=menuBar.addMenu(Q"View")
    centralWidget=newQWidget()
centralWidget.makeLayout:
    - useObject btn1:
        handleSignal0(SIGNAL "clicked()"):
            enableBtn1.setChecked(btn1.isEnabled)
            enableBtn2.setChecked(btn2.isEnabled)
            mm.popup(btn1.mapToGlobal(btn1.pos))
    - useObject btn2:
        handleSignal0(SIGNAL "clicked()"):
            enableBtn1.setChecked(btn1.isEnabled)
            enableBtn2.setChecked(btn2.isEnabled)
            mm.popup(btn2.mapToGlobal(btn2.pos))
win.setCentralWidget(centralWidget)
win.setMenuBar(menuBar)
menuFile.makeMenu:
    * "Stuff"
    #- "Open ...": 
    - use_Object azione1 #<--------- Example use_object for menu file?????????

    - "Save as ...": setEnabled(false)
    - "Save": setEnabled(false)
    * "Quit"
    - "Quit!":
        handleTriggered(): quit(0)
menuView.makeMenu:
    - "Enable btn1":
        handleTriggered(): btn1.setEnabled(true)
    - "Enable btn2":
        handleTriggered(): btn2.setEnabled(true)

win.show()
discard app.exec()

I don't know if it is possible to do or not, it's just an idea I propose, then see you! Hello from Martinix

P.S. Do you think that "NIR" over time can simplfy/improve qtnim (it seems very promising)?

Martinix75 commented 6 months ago

Hi, today (finally) I managed to try your system to create the menus! Fantastic, I am really enjoying, but I still have to play a little to learn! I wanted to ask, is it possible to do the same thing for the "toolbar"? (always if it is not already implemented but I don't seem to have seen it!) big work!!!!!! Hi from Andrea and happy holidays!

jerous86 commented 6 months ago

Sounds like a good idea, I will have a look if I can do it. Happy holidays

jerous86 commented 6 months ago

I just pushed something to #head; in examples/menu_toolbar.nim you can find an example for a toolbar. Is that sufficient?

Martinix75 commented 6 months ago

Wow !!! Fantastic!!! Now I let the holidays spend then I try everything and study, but already what I saw from the example I really like !!! excellent! Thank you!