PerlQt vs Qt C++

Even if you have never programmed before you can look at these example to see what is common about them and that will help you to identify important parts when looking at example code. Try to spot the differences between PerlQt and Qt C++.

Using Qt objects

The PerlQt way

this->connect(this->pushButton1, SIGNAL('clicked()'), SLOT('OnClick()'));
this->lineEdit1->setText("Hello world!");

The Qt C++ way. Notice how this is not needed.

connect(pushButton1, SIGNAL(clicked()), SLOT(OnClick()));
lineEdit1->setText("Hello world!");

Variables and operators

In Perl a variable must be identified by a special operator every time it is used. In this case the operator is $ to tell the program it is a local variable. In C++ a variable only needs to be identified by a special operator the first time it is used. In this case the operator is int to tell the program it is an integer;

Perl code language style
sub MyFunction
{
    $A = 6;
    $B = $A * 9;
    if ( $A > 4 )
    {
        $A = $B + 100;
    }
    return($A);
}
C or C++ code language style
int MyFunction()
{
    int A = 6;
    int B = A * 9;
    if ( A > 4 )
    {
        A = B + 100;
    }
    return(A);
}

Variables and operators within objects

In these two examples the variable A has been made global so it can be used my any function that is a member of the object but the B variable is local and is only be used in the functions where you see it.

Perl code language style
package MyObject;
 
sub MyFunction
{
    this->{A} = 6;
    $B = this->{A} * 9;
    if ( this->{A} > 4 )
    {
        this->{A} = $B + 100;
    }
}
 
sub GetTheValueOfA
{
    return(this->{A});
}
 
1;
C++ code language style
class  MyObject
{
    int A;
 
    void MyFunction()
    {
        A = 6;
        int B = A * 9;
        if ( A > 4 )
        {
            A = B + 100;
        }
    }
 
    int GetTheValueOfA
    {
        return(A);
    }
};

Notice how in C++ a function that does not return a value starts with void and if it returns an integer it starts with int.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License