Qt Pig Latin Translator: Part 1

A few days ago, I posted a video showing a quick pig latin translator. Let’s start a tutorial series, shall we? Before we begin, the majority of the application was made up of Braden’s pig latin code, because his code works well and was easy enough to adapt for this application. The main part I’ll cover in this tutorial series is how to open/convert files and do that nifty little real-time translation thing.

To start, you’ll need a UI file that is very similar to mine… okay, not that close. Just make sure you can tell the signals/slots apart. I named the text edit widgets after the original line edit they represented, so you can probably see that (resultLineEdit, etc.) and the buttons are pretty similar, so no worries there. Beyond that, just tile two plain text editors in a vertical layout, put all your push buttons in a horizontal layout, and set the form’s layout to vertical. Done!

Now, lets just get to the best part first and skip all the header files, etc. You should be able to figure those out anyway! (Hint: Qt Creator does it all for you if you do it the right way… 😉 )

    QStringList wordList = ui->entryLineEdit->toPlainText().split(" ");
    QStringList pigLatin;

    foreach(QString word, wordList)
        word = QString::fromStdString(toPigLatin(word.toStdString()));

    ui->resultLineEdit->setPlainText(pigLatin.join(" "));

Let’s break this down into smaller pieces to understand what’s going on here. First we get a string list–which is essentially a wrapper for a QList of QString’s–from the entry text field (notice my poor object names). We split it based on spaces, and then declare an empty string list which will contain the translated words.

Then the actual work happens in the foreach loop. But wait, foreach…? This isn’t C# or Java! Well, you’re [definitely] right about that. Remember that the QStringList is just a wrapper for a QList, and then remember that Qt provides a foreach function for it’s container classes such as QLinkedList, QMap, and all that good stuff.

Inside the loop, we extract a single word from the word list, and first simplify and remove punctuation that could confuse things in Braden’s algorithm. Also, if you want to use punctuation in gibberish… you might want to rethink just using English. The punctuation removal string we use for our regular expression is just a line of different characters people could mistakenly use.

    const QString punctuationRemoval = "[-`~!@#$%^&*()_—+=|:;<>,.?/{}\'\"\\\[\\\]\\\\]";

Then we reassign the word variable based on a QString from Braden’s toPigLatin function. Notice how we have to convert to and from an std::string because I was too sick to rewrite it to use QStrings instead…. anyway, we then append the word to the new string list containing the pig latin. We join by a space, and viola, it works… sort of. Don’t forget to connect the signals and slots!

    connect(ui->entryLineEdit, SIGNAL(textChanged()), this, SLOT(updateResultLineEdit()));

Since we use a slot, we can easily connect this to other signals. Perhaps you would rather translate on a keypress or editing finished, or even a button press. The magic of those signals and slots! And that about sums this one up. In part 2 (if I can get around to it), we’ll look into the simple file handling so we can save the pig latin and load text files and easily convert them to gibberish… automatically!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s