# QTRuby Strategy for setting up an addition twixt 2 lineEdits

**URL:** <https://www.ruby-forum.com/t/qtruby-strategy-for-setting-up-an-addition-twixt-2-lineedits/226047>\
**Category:** Ruby\
**Created:** [August 27, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/qtruby-strategy-for-setting-up-an-addition-twixt-2-lineedits/226047 "2009-08-27T23:42:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewmurcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@matthewmurcia](https://www.ruby-forum.com/u/matthewmurcia)\
**Post date:** [August 27, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/qtruby-strategy-for-setting-up-an-addition-twixt-2-lineedits/226047/1 "2009-08-27T23:42:56Z")

</div>

Hello-

Thanks to some help previously received here (thanks to Ken B.), I  
have managed to practice using blocks of code with the connect method to  
assign values to several class variables using lineEdit objects and  
display them to a third lineEdit object.

I am now trying to figure out a strategy for using a method to:

1. determine when two class variables have BOTH had their values changed  
by changes in 2 separate lineEdit fields

and

2.once detected, the method will calculate the sum of these two numbers  
and display it in a third lineEdit field.

I have tried to use the sum method alone rather than as a slot as well  
as signaling the sum slot method after each of the lineEdit boxes have  
been changed to no avail.

My (relevant) code currently:

require ‘Qt’

class Form1 \< Qt::Widget  
slots ‘sum()’  
attr\_reader :textLabel1  
attr\_reader :textLabel2  
attr\_reader :textLabel3  
attr\_reader :pushButton1  
attr\_reader :lineEdit1  
attr\_reader :lineEdit2  
attr\_reader :lineEdit3

```
def initialize(parent = nil, name = nil, fl = 0)
    super
    if name.nil?
            setName("Form1")
    end

```

@a=0  
@b=0  
\<-----snipped widget geomeotries-----\>  
#sets @a and @b by user input  
@lineEdit1.connect(SIGNAL(“textChanged(const QString &)”)) do |x| @a = x  
end  
@lineEdit2.connect(SIGNAL(“textChanged(const QString &)”)) do |x| @b = x  
end

Qt::Object.connect(@pushButton1, SIGNAL(“clicked()”), @lineEdit1,  
SLOT(“clear()”) )  
Qt::Object.connect(@pushButton1, SIGNAL(“clicked()”), @lineEdit2,  
SLOT(“clear()”) )  
Qt::Object.connect(@pushButton1, SIGNAL(“clicked()”), @lineEdit3,  
SLOT(“clear()”) )

@lineEdit1.connect(SIGNAL(“textChanged(const QString &)”)) do  
|x| if @b \> 0  
sum  
end

@lineEdit2.connect(SIGNAL(“textChanged(const QString &)”)) do  
|x| if @a \> 0  
sum  
end  
end

def sum  
if (@a \> 0 and @b \> 0)  
@c = @a + @b  
emit @lineEdit3.setText(@c.to\_s)  
end  
end

```
def languageChange()
    setCaption(trUtf8("Form1"))
    @textLabel1.setText( trUtf8("addend 1") )
    @textLabel2.setText( trUtf8("addend 2") )
    @textLabel3.setText( trUtf8("sum") )
    @pushButton1.setText( trUtf8("Reset") )
end

```

end

I don’t know if this is the right strategy…it seemed more cut and  
dried to me when I was writing CLI programs…

Any conceptual help or code snippets would be appreciated-  
Thans in advance-

Matthew B.
