1

int、double、string などのさまざまなデータ型のオブジェクトと、Rational、Date、Complex という私が作成した他の 3 つのクラスを格納および表示する GUI を作成しています。これらのオブジェクトは、同じタイプのリンク リストに格納されます。int、double、および string の場合、ユーザーが QPlainTextEdit に入力した値をリストに保存してQTextBrowser表示する際に問題が発生しましたが、作成したクラスからオブジェクトを表示する方法がわかりませんQTextBrowser に。これを行うことができる機能はありますか?

現在、 「Rational(3,4);」の形式でオブジェクトを受け取る Rational クラスを使用しています。"3/4"などの分数のように表示します。「3/4」の形式のユーザー入力からオブジェクトを作成し、それらをリンクされたリストにプッシュすることができましたが、それらを 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

「Rational」から QString/const std::string への実行可能な変換がない「セマンティックな問題」 が発生しています。これらのオブジェクトを QTextBrowser に変換または表示する方法が見つからないようです。

編集:ここにRationalクラスがあります

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

};

私が使用している関数の関数定義のみを表示しています。定義が表示されていない他の関数は、このプログラムでは使用しない関数です。

4

2 に答える 2