【问题标题】:Is there a function that can display an object from a class that I've created into a QTextBrowser?是否有一个函数可以将我创建的类中的对象显示到 QTextBrowser 中?
【发布时间】:2019-10-17 23:56:00
【问题描述】:

我正在创建一个 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;
     }        

};

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

【问题讨论】:

  • 什么是理性?显示 Rational 类。
  • @eyllanesc 完成。我已编辑问题以显示我的 Rational 课程。

标签: c++ qt qtextbrowser


【解决方案1】:

您是否正在寻找类似的东西?

像这样编辑你的代码:

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

希望对你有帮助。

【讨论】:

  • 效果很好,感谢您的帮助。将该功能实现到我的所有其他类,现在它们工作得很好。实际上帮了我很多,让我考虑制作其他可以在以后帮助我的辅助函数。
【解决方案2】:

我不确定如何显示我在 QTextBrowser 中创建的类中的对象。有没有可以做到这一点的函数?

只有当你写一个。这是你的课程,所以提供这样的功能是你的工作。

您如何处理这取决于您的课程的用途。如果将您的类视为一个字符串是合理的(对于有理数来说似乎不太可能),您可以提供一个隐含的user-defined conversionstring。在其他情况下,您不应提供隐式转换,因为隐式转换通常会妨碍编译器识别错误的能力。显式转换是另一种选择,但人们通常会使用转换函数。 (标准库中的示例包括stringstream::strbitset::to_string。)

您已经编写了大部分转换函数。您所需要做的就是将您的对象流式传输到std::stringstream,然后调用该流的str() 方法。尽可能合理地重复使用您的代码。

【讨论】:

  • 我一直在思考,从来没有想过我实际上可以返回并在我的类中创建另一个处理 QString 类的函数,感谢您的帮助和提醒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多