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.
Is something like this that you're looking for?
Edit your code like this:
toString
function:class Rational
{
...
public:
QString toString() [
return QString::number(numer) + "/" + QString::number(denom);
}
...
}
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.