1

我是第四周学习 C++ 的初学者;我一直在研究 CodeBlocks,但由于我对制作 GUI 感兴趣,我转而使用 Qt Creator。回到 CodeBlocks,我创建了一个函数,可以避免下面代码中的所有重复,只更改“TXT 文件”。但是,使用 Qt Creator 的“专用”C++,我无法理解如何创建一个函数来避免所有这些重复。

有任何想法吗?(我对这个 Qt 项目太深入了,无法回到 CodeBlocks。)

“TXT 文件”根据RadioButton用户选择而改变。

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]);
                }
           }
4

2 回答 2

1

这是通用编程问题,可以以更好的方式重构代码:

// 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
 }
于 2012-11-27T07:01:02.653 回答
0

QT 以信号和插槽而闻名。每个按钮都可以连接到一个插槽。例如在你的情况下。您可以将每个单选按钮连接到一个插槽。为此,打开您的 GUI 表单,右键单击单选按钮并选择“Go To Slot”,然后选择要连接的插槽。这将在您的 .cpp 文件中创建一个空函数。

现在为该按钮编写代码。并且仅当该特定 Button 被按下/单击时才会调用此函数。

例子:

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

我希望这能帮助你解决你的问题。如果您有其他疑问,请提供更多信息。

于 2012-11-27T07:05:04.433 回答