Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:07:55+00:00 2026-06-14T23:07:55+00:00

I am a beginner in my fourth week learning C++; I had been working

  • 0

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]);
                }
           }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:07:56+00:00Added an answer on June 14, 2026 at 11:07 pm

    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
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner in python, but been programming for about 5 years now. I suspect I
A beginner question I am sure, but I have just started working with a
Beginner iOS dev here. I have a problem in my array. Im making an
Beginner here trying to get a pipeline working in bash. If somebody can see
Question : Beginner Level Code: Pure Javascript I had created a web page in
Beginner question. I'm making a simple Gtk notepad in C# and MonoDevelop and everything
Beginner at Django here, I've been trying to fix this for a long time
Python beginner here. I've looked around and found similar questions but can't quite cobble
Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is
Python beginner here. I'm working with diffs at the moment. I'm generating them with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.