1

I'm creating a GUI that stores and displays objects of various data types such as int, double, string and three other class that i have created that are Rational, Date, and Complex. These objects are stored into Linked Lists of the same type. For int, double, and string i've had to problem storing values that the user enters into a QPlainTextEdit to the lists and displaying them into the QTextBrowser, however, i'm unsure on how to display objects from classes that i've created into the QTextBrowser. Is there a function that can do this?

I'm currently working with my Rational class that takes in objects in the form of "Rational(3,4);" and displays them like fractions such as "3/4". I've managed to create the objects from user inputs that are in the form "3/4" and push them into the linked list but i have not been able to display them into my QTextBrowser

//sample code
else if(ui->Rational_button->isChecked())
{
    ui->main_display->setText("");

    //Iterator that goes through the linked list to get the objects
    LinkedList<Rational>::Iterator it = rationalvec.begin();

    while(it != nullptr)
    {
       ui->main_display->append(QString::fromStdString(*it)); 
                                      /* Trying to display all Rational 
                                      objects in the QTextBrowser */
       ++it;                    
    }
}

//There should be an output like the following inside the QTextBrowser

4/5
2/3
7/9
9/11

//These are all Rational type objects

I'm getting a "semantic issue" no viable conversion from 'Rational' to QString/const std::string. I can't seem to find a way to convert or display these objects into the QTextBrowser.

EDIT: Here is the Rational class

class Rational
{
private:
    int numer;  //IN/OUT - the numerator int
    int denom;  //IN/OUT - the denominator int
public:
    /******************************
    ** CONSTRUCTOR & DESTRUCTOR **
    ******************************/
    Rational()         //Constructor
    {
       numer = 0;
       denom = 1;
    }
    Rational(int number)               //Constructor
    {
       numer = number;
       denom = 1;
    }
    Rational(int number1, int number2) //Constructor
    {
      numer = number1;
      denom = number2;
    }  

    /***************
    ** ACCESSORS **
    ***************/
    const Rational add(const Rational &) const;
    const Rational subtract(const Rational &) const;
    const Rational multiply(const Rational &) const;
    const Rational divide(const Rational &) const;

    void display() const
    {
       cout << numer << "/" << denom;
    }

    friend ostream& operator<<(ostream &, const Rational &)    //FOR WIDGET
    {
       out << object.numer;
       out << '/';
       out << object.denom;

       return out;
   }

    bool operator <(const Rational& )                          //FOR WIDGET
    {
       if((numer/denom) < (other.numer/other.denom))
           return true;
       else
           return false;
    }
    bool operator >(const Rational& )                          //FOR WIDGET
    {
       if((numer/denom) > (other.numer/other.denom))
           return true;
       else
           return false;
     }        

};

Only showing the function definition for the functions i'm using, the other functions whose definitions are not shown are the ones im not gonna use during this program.

2 Answers2

1

Is something like this that you're looking for?

Edit your code like this:

  • Add to your Classes a toString function:
class Rational
{

...

public:
    QString toString() [
        return QString::number(numer) + "/" + QString::number(denom);
    }

...

}
  • Display in QTextBrowser:
else if(ui->Rational_button->isChecked())
{
    ui->main_display->setText("");

    for( Rational r : rationalvec )
    {

       ui->main_display->append( r.toString() );    // Here use toString() to append
                                                    // it->toString() if iterator
    }
}

Hope it helps you.

Tom Kim
  • 416
  • 2
  • 4
  • That worked perfectly, thanks for the help. Implemented that function to all my other classes and now they work just fine. Actually helped me out a lot to think about making other helper functions that can aid me later on. – Angel Torres Jun 02 '19 at 05:52
0

i'm unsure on how to display objects from classes that i've created into the QTextBrowser. Is there a function that can do this?

Only if you write one. It's your class, so it's your job to provide such a function.

How you go about this depends on how your class is intended to be used. If it is reasonable to think of your class as being a string (seems unlikely for a rational number), you could provide an implicit user-defined conversion to string. You should not provide an implicit conversion in other cases since implicit conversions often hamper the compiler's ability to identify bugs. An explicit conversion is another option, but often people go with a conversion function. (Examples from the standard library include stringstream::str and bitset::to_string.)

You already have most of a conversion function written. All you need is to stream your object to a std::stringstream then call that stream's str() method. Re-use your code as much as reasonable.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • I was thinking inside the box and never crossed my mind that i could actually go back and create another function in my classes that would deal with the QString class, thanks for the help and the heads up! – Angel Torres Jun 02 '19 at 05:50