user1855595
user1855595

Reputation: 21

Creating an Efficient Function

I am a beginner in my fourth week learning C++; I had been working on CodeBlocks, but due to my interest in making GUIs I switched to Qt Creator. Back in CodeBlocks I had created a function that would avoid all the repetition in the code below, only changing the "TXT FILE". However, with Qt Creator's "specialized" C++ I am having trouble making sense of how to create a function to avoid all this repetition.

Any ideas? (I'm too far into this Qt project to go back to CodeBlocks.)

The "TXT FILE" changes depending on which RadioButton the user selects.

void MovierRec::on_searchButton_clicked()
{
    int randomValue = qrand() % 100;
    QList<QString> titles;
    if(ui->modernButton->isChecked())
           {
               QFile myfile(":/classics.txt");
               if (myfile.open(QIODevice::ReadOnly))
               {
                   QTextStream in(&myfile);
                   while (!in.atEnd())
                   {
                       QString line = in.readLine();
                       titles.append(line);
                   }
                   myfile.close();
                   ui->textBrowser->setPlainText (titles[randomValue]);
                }
           }
    else if(ui->romanceButton->isChecked())
           {
               QFile myfile(":/romance.txt");
               if (myfile.open(QIODevice::ReadOnly))
               {
                   QTextStream in(&myfile);
                   while (!in.atEnd())
                   {
                       QString line = in.readLine();
                       titles.append(line);
                   }
                   myfile.close();
                   ui->textBrowser->setPlainText (titles[randomValue]);
                }
           }

    else if(ui->scifiButton->isChecked())
           {
               QFile myfile(":/scifi.txt");
               if (myfile.open(QIODevice::ReadOnly))
               {
                   QTextStream in(&myfile);
                   while (!in.atEnd())
                   {
                       QString line = in.readLine();
                       //titles.append(line);
                   }
                   myfile.close();
                   ui->textBrowser->setPlainText (titles[randomValue]);
                }
           }

Upvotes: 1

Views: 191

Answers (2)

skg
skg

Reputation: 948

QT is well known for Signals and Slots. Each button can be connected to a slot. For Example in your case. You can connect each radio button to a slot. in order to do that, Open ur GUI form, right click on the radio button and select "Go To Slot", and select the slot you want to connect to. This will create an empty function in ur .cpp file.

Now write your code for that button. And this function is called only when that particular Button is pressed/clicked .

example:

     void ClassA::on_radioButton_clicked()
     {
         // write your code inside this function for , when this button is checked
      }

I hope this will help you solve your issue. If you have other query , please provide more information.

Upvotes: 0

billz
billz

Reputation: 45420

This is generic programming issue, could refactor code in a better way:

// I didn't dig into every line of the code. just provide the refactor idea here
void getTitle(const QString& file_name, QList<QString>& titles;)
{
   QFile myfile(file_name);
   if (myfile.open(QIODevice::ReadOnly))
   {
     QTextStream in(&myfile);
     while (!in.atEnd())
     {
       QString line = in.readLine();
       titles.append(line);
     }
   myfile.close();
 }
}

void MovierRec::on_searchButton_clicked()
{
    int randomValue = qrand() % 100;
    QList<QString> titles;
    if(ui->modernButton->isChecked())
    {
        getTitle("classics.txt", titles);       
    }
    else if(ui->romanceButton->isChecked())
    {
        getTitle("romance.txt", titles);       
    }
    else if(ui->scifiButton->isChecked())
    {
        getTitle("scifi.txt", titles);
   }
   ui->textBrowser->setPlainText(titles[randomValue]); // move the dup action to the end
 }

Upvotes: 1

Related Questions