Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to RUN my Plain C++ App Code on Qt GUI
Forum Update on Monday, May 27th 2025

How to RUN my Plain C++ App Code on Qt GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Denmikz
    wrote on 16 Mar 2016, 18:28 last edited by Denmikz
    #1

    Hi. I just want to know how to run my c++ code to my qt gui and display its output through a QTextEdit widget. I'm new to qt gui and I've researched for a week now and failed to solve my problem and now my last resort is to ask you here on the forums on how to do it.
    Here is my code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctime>
    #include <fstream>
    #include <conio.h>
    #include <string.h>

    using namespace std;

    int countlines();
    string getRandomLine();
    string getAnswer(string);
    void getChoices(string*,string);

    int main()
    {
    int numberOfItems = countlines();
    string questions[numberOfItems];
    string answers[numberOfItems];
    string choices[numberOfItems][4];

    string temp;
    int i = 0;
    int o = 0;
    
    cout << "Loading questions...";
    while(i < numberOfItems)
    {
    
    
    
        temp = getRandomLine();
        while(o < numberOfItems)
        {
            if(questions[o] == temp)
            {
                temp = getRandomLine();
                o = 0;
            }else{
                o++;
            }
        }
        questions[i] = temp;
    
        o=0;
        i++;
    }
    i = 0;
    while(i < numberOfItems)
    {
        answers[i] = getAnswer(questions[i]);
        i++;
    }
    
    i = 0;
    while(i < numberOfItems)
    {
        getChoices(choices[i],answers[i]);
        i++;
    }
    
    system("cls");
    
    int answer[numberOfItems];
    
    i = 0;
    while(i < numberOfItems)
    {
        cout << questions[i] << endl;
        for(int o=0;o<4;o++)
            cout << o+1 << "]" << choices[i][o] << endl;
        cin >> answer[i];
    
        i++;
    }
    int correct = 0;
    i = 0;
    while(i < numberOfItems)
    {
        if(choices[i][answer[i] - 1] == answers[i])
            correct++;
    
        i++;
    }
    
    cout << correct << " imong score!!";
    
    return 0;
    

    }

    void getChoices(string choice[],string answer)
    {
    string line;

    int maxLine = countlines();
    bool found = false;
    
    ifstream myfile("sample.txt");
    while(getline(myfile,line))
    {
        if(found)
        {
            choice[0] = line.substr(0,line.find(" "));
            line = line.substr(line.find(" ")+1);
            choice[1] = line.substr(0,line.find(" "));
            line = line.substr(line.find(" ")+1);
            choice[2] = line.substr(0,line.find(" "));
            line = line.substr(line.find(" ")+1);
            choice[3] = line.substr(0,line.find(" "));
            return;
        }
        if(answer == line)
        {
            found = true;
        }
    }
    

    }

    string getAnswer(string question)
    {
    string line;

    int maxLine = countlines();
    bool found = false;
    
    ifstream myfile("sample.txt");
    while(getline(myfile,line))
    {
        if(found){
            return line;
        }
        if(question == line)
        {
            found = true;
        }
    }
    

    }

    string getRandomLine()
    {
    string line;

    int maxLine = countlines();
    int random;
    srand(time(0));
    random = rand() % maxLine;
    random = random * 3;
    int count = 0;
    ifstream myfile ("sample.txt");
    
    while (getline (myfile,line))
    {
        if(random == count)
        {
            line;
            break;
        }
        count++;
    }
    myfile.close();
    
    return line;
    

    }

    int countlines()
    {
    string line;
    int count = 0;

    ifstream myfile ("sample.txt");
    
    if(myfile.is_open())
    {
        while (getline (myfile,line))
        {
            count++;
        }
        count /= 3;
        myfile.close();
    }
    else
    {
        cout << "Unable to open file";
    }
    
    return count;
    

    }

    Pablo J. RoginaP 1 Reply Last reply 16 Mar 2016, 19:15
    0
    • M Offline
      M Offline
      michelson
      wrote on 16 Mar 2016, 19:04 last edited by
      #2

      Not sure if thats what you want but you can (i guess - not tested) display your code by reading .cpp and/or .h file and apending text you read to QTextBrowser like:

      File inputFile(/*path to your cpp file*/);
      if (inputFile.open(QIODevice::ReadOnly))
      {
         QTextStream in(&inputFile);
         while (!in.atEnd())
         {
            QString line = in.readLine();
            your_text_browser.append(line);
         }
         inputFile.close();
      }
      

      Repeat this procedure with .h file if needed.

      D 1 Reply Last reply 16 Mar 2016, 19:13
      0
      • M michelson
        16 Mar 2016, 19:04

        Not sure if thats what you want but you can (i guess - not tested) display your code by reading .cpp and/or .h file and apending text you read to QTextBrowser like:

        File inputFile(/*path to your cpp file*/);
        if (inputFile.open(QIODevice::ReadOnly))
        {
           QTextStream in(&inputFile);
           while (!in.atEnd())
           {
              QString line = in.readLine();
              your_text_browser.append(line);
           }
           inputFile.close();
        }
        

        Repeat this procedure with .h file if needed.

        D Offline
        D Offline
        Denmikz
        wrote on 16 Mar 2016, 19:13 last edited by
        #3

        @michelson I'm very sorry to confuse you on my question. Okay, so my code there is a part of my Multiple Choice Quiz Application and that code is the one that's going to read a text file that contains the questions and answers for the quiz. I just want to know how I can display this line to the QTextEdit widget I mentioned:
        while(i < numberOfItems)
        {
        cout << questions[i] << endl;
        for(int o=0;o<4;o++)
        cout << o+1 << "]" << choices[i][o] << endl; <----------- This one. I want it to be displayed on the QTextEdit widget.
        cin >> answer[i];

        i++;
        

        }

        1 Reply Last reply
        0
        • D Denmikz
          16 Mar 2016, 18:28

          Hi. I just want to know how to run my c++ code to my qt gui and display its output through a QTextEdit widget. I'm new to qt gui and I've researched for a week now and failed to solve my problem and now my last resort is to ask you here on the forums on how to do it.
          Here is my code:
          #include <iostream>
          #include <stdlib.h>
          #include <ctime>
          #include <fstream>
          #include <conio.h>
          #include <string.h>

          using namespace std;

          int countlines();
          string getRandomLine();
          string getAnswer(string);
          void getChoices(string*,string);

          int main()
          {
          int numberOfItems = countlines();
          string questions[numberOfItems];
          string answers[numberOfItems];
          string choices[numberOfItems][4];

          string temp;
          int i = 0;
          int o = 0;
          
          cout << "Loading questions...";
          while(i < numberOfItems)
          {
          
          
          
              temp = getRandomLine();
              while(o < numberOfItems)
              {
                  if(questions[o] == temp)
                  {
                      temp = getRandomLine();
                      o = 0;
                  }else{
                      o++;
                  }
              }
              questions[i] = temp;
          
              o=0;
              i++;
          }
          i = 0;
          while(i < numberOfItems)
          {
              answers[i] = getAnswer(questions[i]);
              i++;
          }
          
          i = 0;
          while(i < numberOfItems)
          {
              getChoices(choices[i],answers[i]);
              i++;
          }
          
          system("cls");
          
          int answer[numberOfItems];
          
          i = 0;
          while(i < numberOfItems)
          {
              cout << questions[i] << endl;
              for(int o=0;o<4;o++)
                  cout << o+1 << "]" << choices[i][o] << endl;
              cin >> answer[i];
          
              i++;
          }
          int correct = 0;
          i = 0;
          while(i < numberOfItems)
          {
              if(choices[i][answer[i] - 1] == answers[i])
                  correct++;
          
              i++;
          }
          
          cout << correct << " imong score!!";
          
          return 0;
          

          }

          void getChoices(string choice[],string answer)
          {
          string line;

          int maxLine = countlines();
          bool found = false;
          
          ifstream myfile("sample.txt");
          while(getline(myfile,line))
          {
              if(found)
              {
                  choice[0] = line.substr(0,line.find(" "));
                  line = line.substr(line.find(" ")+1);
                  choice[1] = line.substr(0,line.find(" "));
                  line = line.substr(line.find(" ")+1);
                  choice[2] = line.substr(0,line.find(" "));
                  line = line.substr(line.find(" ")+1);
                  choice[3] = line.substr(0,line.find(" "));
                  return;
              }
              if(answer == line)
              {
                  found = true;
              }
          }
          

          }

          string getAnswer(string question)
          {
          string line;

          int maxLine = countlines();
          bool found = false;
          
          ifstream myfile("sample.txt");
          while(getline(myfile,line))
          {
              if(found){
                  return line;
              }
              if(question == line)
              {
                  found = true;
              }
          }
          

          }

          string getRandomLine()
          {
          string line;

          int maxLine = countlines();
          int random;
          srand(time(0));
          random = rand() % maxLine;
          random = random * 3;
          int count = 0;
          ifstream myfile ("sample.txt");
          
          while (getline (myfile,line))
          {
              if(random == count)
              {
                  line;
                  break;
              }
              count++;
          }
          myfile.close();
          
          return line;
          

          }

          int countlines()
          {
          string line;
          int count = 0;

          ifstream myfile ("sample.txt");
          
          if(myfile.is_open())
          {
              while (getline (myfile,line))
              {
                  count++;
              }
              count /= 3;
              myfile.close();
          }
          else
          {
              cout << "Unable to open file";
          }
          
          return count;
          

          }

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on 16 Mar 2016, 19:15 last edited by
          #4

          @Denmikz what about the code editor or syntax highlighter examples?

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          D 1 Reply Last reply 16 Mar 2016, 19:21
          0
          • Pablo J. RoginaP Pablo J. Rogina
            16 Mar 2016, 19:15

            @Denmikz what about the code editor or syntax highlighter examples?

            D Offline
            D Offline
            Denmikz
            wrote on 16 Mar 2016, 19:21 last edited by
            #5

            @Pablo-J.-Rogina again I'm sorry for the confusion. I already edited my post to make things clearer. sorry for my mistake english is not my main language. :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              michelson
              wrote on 16 Mar 2016, 19:34 last edited by
              #6

              First of all if you need ONLY to display text I'd go for QLabel rather than QTextEdit. If not you can eaisily append text to QTextEdit by running QTextEdit::append(QString) function ( http://doc.qt.io/qt-4.8/qtextedit.html#append ) so your code would look like this:

              while(i < numberOfItems)
              {
                  cout << questions[i] << endl;
                  for(int o=0;o<4;o++)
                      your_text_edit.append(choices[i][o]);
                  cin >> answer[i];
                  i++;
              }
              

              Of course assuming choices table is holding QStrings.

              D 1 Reply Last reply 16 Mar 2016, 19:39
              0
              • M michelson
                16 Mar 2016, 19:34

                First of all if you need ONLY to display text I'd go for QLabel rather than QTextEdit. If not you can eaisily append text to QTextEdit by running QTextEdit::append(QString) function ( http://doc.qt.io/qt-4.8/qtextedit.html#append ) so your code would look like this:

                while(i < numberOfItems)
                {
                    cout << questions[i] << endl;
                    for(int o=0;o<4;o++)
                        your_text_edit.append(choices[i][o]);
                    cin >> answer[i];
                    i++;
                }
                

                Of course assuming choices table is holding QStrings.

                D Offline
                D Offline
                Denmikz
                wrote on 16 Mar 2016, 19:39 last edited by
                #7

                @michelson oh okay. but will it run? because i and o there are both int or do I have to convert those to QString?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  michelson
                  wrote on 16 Mar 2016, 19:45 last edited by
                  #8

                  Dude, I meant in the array choices you hold QStrings so the definition of it (which is missing in code included above) would be like:

                  QString choices[10][4];
                  
                  D 1 Reply Last reply 16 Mar 2016, 19:47
                  0
                  • M michelson
                    16 Mar 2016, 19:45

                    Dude, I meant in the array choices you hold QStrings so the definition of it (which is missing in code included above) would be like:

                    QString choices[10][4];
                    
                    D Offline
                    D Offline
                    Denmikz
                    wrote on 16 Mar 2016, 19:47 last edited by
                    #9

                    @michelson oh sorry my bad. I'm going to try it now. Thanks for your help. :)

                    D 1 Reply Last reply 16 Mar 2016, 20:14
                    0
                    • D Denmikz
                      16 Mar 2016, 19:47

                      @michelson oh sorry my bad. I'm going to try it now. Thanks for your help. :)

                      D Offline
                      D Offline
                      Denmikz
                      wrote on 16 Mar 2016, 20:14 last edited by
                      #10

                      @michelson I just did what you told me to do on my code and my app compiled but when I push the pushbutton assigned to trigger the code nothing displayed on my QTextEdit.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        michelson
                        wrote on 16 Mar 2016, 20:19 last edited by michelson
                        #11

                        Are you sure that choices is filled with non-empty QStrings?
                        Try filling it "by hand" like:

                        choices[0][0] = "Choice [0][0]";
                        choices[0][1] = "Choice [0][1]";
                        choices[0][2] = "Choice [0][2]";
                        choices[0][3] = "Choice [0][3]";
                        choices[1][0] = "Choice [1][0]";
                        choices[1][1] = "Choice [1][1]";
                        choices[1][2] = "Choice [1][2]";
                        choices[1][3] = "Choice [1][3]";
                        

                        i dont know the exact dimensions of it but I guess two levels are enough for tests. If nothing displays this time we will try to figure it out.

                        1 Reply Last reply
                        0

                        1/11

                        16 Mar 2016, 18:28

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved