PDA

View Full Version : auto-math in Qt



nitriles
13th September 2007, 10:18
hello,

I have a rather simple question.
I have 2 lineEdit boxes. In the first box i want to type a number (only numbers should be allowed) and as soon as the number has been typed, it has to appear in the second lineEdit box but multiplied by 2. How can i create this in Qt? The UI is already made.

I created my own function and then i want to edit the code in Qt.
void Math::Math2()
{
#get number A from lineEdit_A
a = strtoint(self.lE_A.text().ascii()) -do i need str to int?
#multiply by 2
b = 2*a
#fill in B into lineEdit_B
inttostr(self.lE_B.insertItem(b))
}

I managed to make a lineEdit and when clicking the button it appears in a list box, but how do you do this with numbers?

Thank a lot, greetings.

wysota
13th September 2007, 10:44
Connect a custom slot to the textChanged() signal of the line edit and use QString::toInt() and QString::number along QLineEdit::setText to implement the functionality you want.

nitriles
13th September 2007, 10:47
thanks a lot!
im using python so lets hope i manage to translate the c++ to python.

wysota
13th September 2007, 10:49
Function calls are the same in both cases. Just use dots instead of colons just remember that QString::number is a static function whereas others are regular methods.

nitriles
13th September 2007, 12:37
Im not sure I understand the difference, i am 'literally' a beginner.
How can i use those classes, how do i implement that?

jpn
13th September 2007, 13:00
void Math::Math2()
{
#get number A from lineEdit_A
a = strtoint(self.lE_A.text().ascii()) -do i need str to int?
#multiply by 2
b = 2*a
#fill in B into lineEdit_B
inttostr(self.lE_B.insertItem(b))
}


This doesn't look like Python to me. It looks more like a mix of C++ and Python. As a friendly advice, I warmly suggest you to learn at least basics of the programming language you're working with before starting to work with a toolkit like Qt.

nitriles
14th September 2007, 10:09
nah, that is indeed c++ with python.
its because Qt is programmed for c++ but python is smart enough to read it as python language, thus it does not matter!
but thanks, it finally worked.

wysota
14th September 2007, 10:25
its because Qt is programmed for c++ but python is smart enough to read it as python language, thus it does not matter!

It's not Python, it's Riverbank ;)

http://www.riverbankcomputing.co.uk/pyqt/

nitriles
14th September 2007, 14:17
haha, yeah sorry, youre completly right