0

I'm sorry if any of this isn't detailed enough or doesn't make sense, I was up until 5:30 this morning trying to get this working. I have tried using a string array, declaring my strings as QStrings, and converting my string to a QString. I have a C++ code that I will post at the end of my question that prints a random string from an array then asks if that is what is wanted and if so it will exit the loop and I want to incoperate that code into a QT application where a push button would run the code and print the result to a lineEdit (it doesn't necesarrily have to be a lineEdit just anything that would display text). I know that I have to get rid of the loop since it's a push button but I can't for the life of me figure out how to make it print the variable. Any help would be greatly appreciated and if I need to clarify anything please let me know, thanks.

    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    #include <string>
    using namespace std;

    int main()
    {
      string ans = "no";
      string result;
      int a;
      string result;
      string myarr[] = {"foo", "bar", "etc"};
      do{
          srand(time(0));
          a = sizeof(myarr) / sizeof(myarr[0]);
          result = myarr[rand() % a];
          cout << result<<endl;
          cout << "Will that work?";<<endl;
          cin >> ans;
      while(ans=="no");
      cout << "Glad I could help."<<endl;
      reurn 0;
      }

soryy, I have tried

    void MainWindow::on_pushButton_clicked()
    {
      string ans = "no";
      string result;
      int a;
      string result;
      string myarr[] = {"foo", "bar", "etc"};

      srand(time(0));
          a = sizeof(myarr) / sizeof(myarr[0]);
          result = myarr[rand() % a];
      ui->lineEdit->setText(result);
     }

I have also tried declaring result as a QString instead of a string, and a post I found for converting string into QString but I don't recall the syntax for that.

  • In your code I do not see any buttons or QLineEdit, show what you have tried to solve the specific problem of the GUI. – eyllanesc Jan 25 '18 at 21:38
  • Is that enough or would I need to post the entire source code? I’m sort of a noob to cpp, I started with it years ago in high school and then quit programming for the most part, and qt and GUI programming in general is completely new to me. tia – Super Squirrel Jan 25 '18 at 22:00
  • No need to use `string` in Qt.Think of an array of `QString`. Take a random index and show that `QString` in the `QLineEdit`. – Aditya Jan 25 '18 at 22:33
  • Use `QString ans =`, `QString result`, `QString myarr[] =`. Your code works perfectly in showing the text for me (if I change to a `QString`). Make sure you remove the double declaration of `result`. Also, check if your `QLineEdit` background and text color are the same/different. Maybe you are just not able to see. – Aditya Jan 25 '18 at 22:39
  • I’m not sure I understand what you mean, are you saying use QString myarr=... or just myarr=... ?I have tried using QString myarr instead of string myarr but that wouldn’t compile either. – Super Squirrel Jan 25 '18 at 22:39
  • You can be new in any language but what we need here is that you ask your question correctly, and for that you must provide a [mcve], I also recommend reading [ask], if you do not post what you have tried (it is not necessary complete code) then we'll think you want us to do your job. – eyllanesc Jan 25 '18 at 23:07
  • Thanks for the info and resources. I would usually be a lot more thorough but I’m going on very little sleep and not thinking clearly at the moment – Super Squirrel Jan 25 '18 at 23:19
  • Well, it is good that you showed your work after mentioning at least. We do have a strict policy to not do the entire work for the asker and that's why we would need a clear, if needed detailed, description with the attempt. – Aditya Jan 26 '18 at 16:02

1 Answers1

1

You need slight modifications. Your main compile issue might be the duplicate declaration of result. Apart from that, just change all your string to QString.

void MainWindow::on_pushButton_clicked()
{
    QString ans = "no";
    QString result;
    int a;
    QString myarr[] = {"foo", "bar", "etc"};

    srand(time(0));
    a = sizeof(myarr) / sizeof(myarr[0]);
    result = myarr[rand() % a];
    ui->lineEdit->setText(result);
 }
Aditya
  • 439
  • 4
  • 14
  • I knew it had to be something simple that I was overlooking. Smh... Thanks for your help. – Super Squirrel Jan 25 '18 at 22:50
  • Happy coding. In future, make sure to look at your errors, they give a lot of information. – Aditya Jan 25 '18 at 22:51
  • Thanks I will be sure to keep that in mind, speaking of that do you have any clue what cannot find -|GL would mean? I’m guessing it’s some helper file or something should I reinstall QT? I used the get help online option but I couldn’t find anything about it. – Super Squirrel Jan 25 '18 at 23:13
  • I don't have any guesses - all I can tell is it is some library probably of OpenGL. But, a quick google search gave me this: https://stackoverflow.com/questions/18406369/qt-cant-find-lgl-error – Aditya Jan 26 '18 at 14:21