jerous86 / nimqt

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

colors! #24

Closed Martinix75 closed 1 year ago

Martinix75 commented 1 year ago

Today I don't write to you for bugs or things that do not work (at least I think), only that I have been thinking about it for three days and I do not arrive at a solution (I don't know how to do it). The thing I can't do is make it appear in red (or other color) the numbers in the table (or the red writing or the background), the program is always as follows:

import std/[os, random, tables, strformat, math, strutils, algorithm]
import nimqt
import nimqt/qdialog
import nimqt/[qpushbutton, qtablewidget, qgridlayout,qboxlayout, qlabel,qbrush, qcolor] 
#import modale

proc aggiornaTabella(tab: ptr QTableWidget; xmax, ymax: cint; num: Table[array[0..1, int], float], formati: Table) =
 echo("aggiorno......")
 var
   contattore = 0
   strTemp: string
   finaleQ: Qstring

 tab.clear()
 tab.setColumnCount(xmax)
 tab.setRowCount(ymax)
 tab.setHorizontalHeaderLabels(newQstringList(@["A","B","C","D","E","F","G"]))
 for x in countup(0, ymax-1):
   for y in countup(0, xmax-1):
    let splitNum = splitDecimal(num[[x,y]])
    let segno = if num[[x,y]] < 0: "-" else: ""
    let interoStr = $(int(abs(splitNum[0])))
    let decimale = abs(splitNum[1])
    let decimaleStr = $round(decimale, parseInt(formati["noddec"])) #per ora 3 poi si decide...
    for t in countDown(len(interoStr)-1, 0):
      if contattore == 3:
        strTemP = strTemp & formati["separatore"] & interoStr[t]
        contattore = 0
      else:
        strTemp = strTemp & interoStr[t]
        contattore.inc()
    let invertiStr = reversed(strTemp)
    let interoJStr = join(invertiStr, "")
    finaleQ = Q segno & interoJStr & formati["decimali"] & decimaleStr[2..^1]
    let item =  newQTableWidgetItem(finaleQ)
    item.setTextAlignment(newQFlags(Qt_AlignmentFlag.AlignHCenter)) #allinea al centro
    #item.setBackgroundColor(newQBrush(newQColor(r=cint(255), g=cint(0), b=cint(0)),Qt_BrushStyle.NoBrush)) # --> inspired by Python
    tab.setItem(cint(x), cint(y), item)
    contattore = 0
    strTemp=""

const
  xMax = 7
  yMax = 10
var 
  numeri = initTable[array[0..1, int], float]() #inizializza il dizionario pe ri valori.
  formato = {"separatore": "`", "decimali": ".", "noddec": "2", "rossonegativo": "false"}.toTable #exaple1 after try type!!
  #cancellami=1

for x in countup(0, yMax-1):
  for y in countup(0, xMax-1):
    numeri[[x ,y]] = (10000*rand(1.0)) - 5000
    #numeri[[x ,y]] = cancellami
    #cancellami.inc()

echo numeri
nimqt.init
let app1 = newQApplication(commandLineParams())
let tabella1 = newQtableWidget()
let bottone1 = newQPushButton(Q "Modale")
let bottone2 = newQPushButton(Q "Modless")
#let bottone3 = newQPushButton(Q "Live")

let finestra: ptr QDialog = newQDialog() #finestra = qDialog
finestra.makelayout:
  - useObject tabella1
  - newQWidget():
    - newQHBoxLayout():
      - useObject bottone1
      - useObject bottone2

aggiornaTabella(tab=tabella1, xmax=xMax, ymax=yMax, num=numeri, formati=formato)

finestra.resize(750, 400)
finestra.setWindowTitle(Q "Tabella")
finestra.show()
discard app1.exec()

# !!! CIOMPILE WHIT:  -d:nimPreviewFloatRoundtrip

In the program the Python originates, the background color is written as:

item.setBackground(Qbrush(Qt.red))

But I can't reproduce it in Nim; Then I would like the red writing more, or if we turn exaggerating, the red background and the blue writing for example, also to provide an example of how to do it! I don't really know how to do it! Maybe you have an idea? Thanks from Andrea

jerous86 commented 1 year ago

item.setBackground(newQBrush(newQColor(r=cint(255), g=cint(0), b=cint(0)),Qt_BrushStyle.SolidPattern)) works for me; using NoBrush will not paint anything.

Martinix75 commented 1 year ago

Always like this .. I arrive near the solution .. then I get lost in a glass of water! It comes to cry ... yes with "solidpaint" it works! thanks

Martinix75 commented 1 year ago

And in this way, only the text is colored:

item.setForeground(newQBrush(newQColor(r=cint(255), g=cint(15), b=cint(5)),Qt_BrushStyle.SolidPattern))

tanhks e good day by andrea