c++ - 是否有一个函数可以将我创建的类中的对象显示到 QTextBrowser 中?

标签 c++ qt qtextbrowser

我正在创建一个 GUI,用于存储和显示各种数据类型的对象,例如 int、double、string 和我创建的其他三个类,即 Rational、Date 和 Complex。这些对象存储在相同类型的链表中。对于 int、double 和 string,我不得不将用户在 QPlainTextEdit 中输入的值存储到列表中并将它们显示到 QTextBrowser 中,但是,我我不确定如何显示我在 QTextBrowser 中创建的类中的对象。有没有可以做到这一点的功能?

我目前正在使用我的 Rational 类,它以 "Rational(3,4);" 的形式接收对象,并将它们显示为分数,例如 "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;
     }        

};

仅显示我正在使用的函数的函数定义,其他未显示定义的函数是我不会在此程序中使用的函数。

最佳答案

您正在寻找这样的东西吗?

像这样编辑你的代码:

  • 向您的类添加一个 toString 函数:
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
    }
}

希望对你有帮助。

关于c++ - 是否有一个函数可以将我创建的类中的对象显示到 QTextBrowser 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56410226/

相关文章:

python - 将 blob 图像从 sqlite db 插入 QPixMap 到 PyQt5 中的 QTextBrowser

c++ - Qt TextBrowser水平滚动条

html - 如何在 QTextBrowser (Html) 中设置文本颜色? (PyQt)

c++ - 不允许抽象类的对象

qt - GTK WebAssembly 在不久的将来支持吗?

c++ - 不能将标准 C++ 库包含在 MinGW 中

database - QSqlDatabase & QSqlQuery 的正确方法是什么?

c++ - 为什么在存在 decltype 的情况下范围解析失败?

c++ - 字母数字字符的正则表达式,.@&,’()+/: and one hyphen only

c++ - 如何正确处理信号,以便 gperftools CPU 分析器仍然有效?