Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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;
    

    }



  • 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.



  • @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++;
    

    }



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



  • @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. :)



  • 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.



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



  • 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];
    


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



  • @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.



  • 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.


Log in to reply