QtRuby Questions: Why does

  • Thread starter Matthew Borgeson
  • Start date

M

Matthew Borgeson

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 User 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 Borgeson
 
Ad

Advertisements

K

Ken Bloom

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.

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

#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
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?

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))
 
Ad

Advertisements

M

Matthew Borgeson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top