# QtRuby Questions: Why does

**URL:** <https://www.ruby-forum.com/t/qtruby-questions-why-does/173210>\
**Category:** Ruby\
**Created:** [August 4, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/qtruby-questions-why-does/173210 "2009-08-04T23:50:20Z")\
**Posts on this page:** 4\
**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 4, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/qtruby-questions-why-does/173210/1 "2009-08-04T23:50:20Z")

</div>

Guten Tag, everyone-

I have finally found the time to sit down and attempt GUI programming  
with QtRuby. I am attempting to use QtRuby from within KDevelop to make  
a simple widget in Qt Designer, run the .ui file through rbuic, and then  
simply modify the connections.

The widget in question has 3 lineEdit fields, all three of which begin  
blank. Ultimately, I want eventually to have the lineEdit3 field  
(labeled “sum”) to “automagically” populate with the sum of the first  
two lineEdit fields once the user had entered non-zero numbers.

My current goal is to simply have the lineEdit3 field “automagically”  
display the word “fudge” whenever a number is simply entered into  
lineEdit1, I have looked at the Rapid GUI Development Fridays book, The  
Ruby Way, and various online tutorials to help me with this to no avail.

Here is the code so far:

# Form implementation generated from reading ui file ‘sum.ui’

# 

# Created: Mon Aug 3 16:38:58 2009

# by: The QtRuby U. Interface Compiler (rbuic)

# 

# WARNING! All changes made in this file will be lost!

require ‘Qt’

class Form1 \< Qt::Widget  
slots  
attr\_reader :textLabel1  
attr\_reader :textLabel2  
attr\_reader :textLabel3  
attr\_reader :pushButton1  
attr\_reader :lineEdit1  
attr\_reader :lineEdit2  
attr\_reader :lineEdit3

#start my slots  
‘update(const QString &)’  
#end my slots

```
def initialize(parent = nil, name = nil, fl = 0)
    super

    if name.nil?
            setName("Form1")
    end

```

#start my variables

#end my variables

```
    @textLabel1 = Qt::Label.new(self, "textLabel1")
    @textLabel1.setGeometry( Qt::Rect.new(160, 50, 111, 31) )

    @textLabel2 = Qt::Label.new(self, "textLabel2")
    @textLabel2.setGeometry( Qt::Rect.new(160, 90, 121, 31) )

    @textLabel3 = Qt::Label.new(self, "textLabel3")
    @textLabel3.setGeometry( Qt::Rect.new(150, 140, 121, 31) )

    @pushButton1 = Qt::PushButton.new(self, "pushButton1")
    @pushButton1.setGeometry( Qt::Rect.new(270, 50, 71, 31) )

    @lineEdit1 = Qt::LineEdit.new(self, "lineEdit1")
    @lineEdit1.setGeometry( Qt::Rect.new(30, 50, 90, 31) )

    @lineEdit2 = Qt::LineEdit.new(self, "lineEdit2")
    @lineEdit2.setGeometry( Qt::Rect.new(30, 90, 90, 31) )

    @lineEdit3 = Qt::LineEdit.new(self, "lineEdit3")
    @lineEdit3.setGeometry( Qt::Rect.new(30, 140, 90, 31) )

```

#start my connections  
Qt::Object.connect(@lineEdit1, SIGNAL(“textChanged(const QString &)”),  
@lineEdit3, SLOT(‘update(const QString &)’) )

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()”) )

#end my connections

```
    languageChange()
    resize( Qt::Size.new(581, 480).expandedTo(minimumSizeHint()) )
    clearWState( WState_Polished )
end

```

#start my slot methods  
def update  
@lineEdit3.setText( trUtf8(“fudge”))  
end  
#end my slot methods

```
#
# Sets the strings of the subwidgets using the current
# language.
#
def languageChange()
    setCaption(trUtf8("Form1"))
    @textLabel1.setText( trUtf8("addend 1") )
    @textLabel2.setText( trUtf8("addend 2") )
    @textLabel3.setText( trUtf8("sum") )
    @pushButton1.setText( trUtf8("Reset") )
end
protected :languageChange

```

end

Here is the error message I get when I try to run it:

ruby -KA -C"/home/hujraad/Desktop/sum" -I"/home/hujraad/Desktop/sum/."  
“sum.rb”  
QObject::connect: No such slot Qt::LineEdit::update(const QString&)  
QObject::connect: (sender name: ‘lineEdit1’)  
QObject::connect: (receiver name: ‘lineEdit3’)  
\*\*\* Exited normally \*\*\*

I believe I have the connections in correctly, as I have already  
practiced that with other connections. Have I improperly syntaxed the  
update() method?

any help is appreciated; thanks in advance

Matthew B.

---

<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 5, 2009, 1:08am UTC](https://www.ruby-forum.com/t/qtruby-questions-why-does/173210/2 "2009-08-05T01:08:57Z")

</div>

On Wed, 05 Aug 2009 06:50:22 +0900, Matthew B. wrote:

> ```
> attr_reader :textLabel2
> attr_reader :textLabel3
> attr_reader :pushButton1
> attr_reader :lineEdit1
> attr_reader :lineEdit2
> attr_reader :lineEdit3
> 
> ```

#I snipped the extra stuff that was irrelevant to the example.  
#Please learn to make minimal examples.

def initialize

#somewhere deep in initialize

> #start my connections  
> Qt::Object.connect(@lineEdit1, SIGNAL(“textChanged(const QString &)”),  
> @lineEdit3, SLOT(‘update(const QString &)’) )

end #I snipped the extra stuff

> #start my slot methods  
> def update  
> @lineEdit3.setText( trUtf8(“fudge”))  
> end  
> #end my slot methods

> practiced that with other connections. Have I improperly syntaxed the  
> update() method?

When you call connect as you did with the signature (CONTROL, SIGNAL,  
CONTROL, SLOT), then the slot is called on the second control, as though  
you had called @lineEdit3.update(“foobar”)

You’re trying to call Form1#update, and the simplest way to do that is  
use a block as the code to call. connect accepts a block like so:

@lineEdit1.connect(SIGNAL(“textChanged(const QString &)”)) do |thestr|  
lineEdit3.setText( trUtf8(“fudge”))  
end

Once you know how to do that, you can use method(:update) to get a  
handle  
to the update method on the current object, then using the unary &  
operator to treat it like a block, as follows:

@lineEdit1.connect(SIGNAL(“textChanged(const QString &)”),  
&method(:update))

---

<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 5, 2009, 6:40am UTC](https://www.ruby-forum.com/t/qtruby-questions-why-does/173210/3 "2009-08-05T06:40:14Z")

</div>

Matthew B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ken-  
> First, I do apologize for the long post…i just wanted to be clear and  
> am used to dotting my i’s and crossing my t’s too precisely in my line  
> of work (clinical pharmacist)

Making minimal examples helps not just in the asking process, but also  
in the learning process. Frequently eliminating unnecessary stuff to  
ask a question brings the problem into sharp enough focus that you  
could answer it yourself.

> Second, thanks for the help. I was unaware that I could pass blocks (in  
> addition to methods) with a signal(as opposed to a call to CONNECT). All  
> the examples I have seen use the control/signal/control/slot syntax with  
> the creation of a slot method.
> 
> I am glad I posted this here not only for myself, but for anyone else  
> learning QtRuby on their own. Hopefully it will make it easier for those  
> who, like me, started their programming with Ruby and are slowly making  
> their way toward proficiency…

I had some of these same issues trying to learn QtRuby for myself.  
This particular piece of information was not easy to find. In the end  
though, I actually found that GTK+'s Ruby interface was more  
Ruby-like, and also very well documented, so my latest GUI programming  
project has been using GTK+ instead.

---

<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 5, 2009, 2:42am UTC](https://www.ruby-forum.com/t/qtruby-questions-why-does/173210/4 "2009-08-05T02:42:18Z")

</div>

Ken-  
First, I do apologize for the long post…i just wanted to be clear and  
am used to dotting my i’s and crossing my t’s too precisely in my line  
of work (clinical pharmacist)

Second, thanks for the help. I was unaware that I could pass blocks (in  
addition to methods) with a signal(as opposed to a call to CONNECT). All  
the examples I have seen use the control/signal/control/slot syntax with  
the creation of a slot method.

I am glad I posted this here not only for myself, but for anyone else  
learning QtRuby on their own. Hopefully it will make it easier for those  
who, like me, started their programming with Ruby and are slowly making  
their way toward proficiency…

Thanks again-

mfb
