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

Is there a function that can display an object from a class that I've created into a QTextBrowser?

本文关键字:对象 QTextBrowser 创建 函数 有一个 显示 是否      更新时间:2023-10-16

我正在创建一个GUI,用于存储和显示各种数据类型的对象,例如int,double,string和我创建的其他三个类,即Rational,Date和Complex。这些对象存储在相同类型的链表中。对于 int、double 和字符串,我不得不将用户输入到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

我遇到了一个"语义问题">,没有从"理性"到 QString/const std::string 的可行转换。我似乎找不到将这些对象转换或显示到 QTextBrowser 中的方法。

编辑:这是理性类

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

希望对您有所帮助。

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

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

你如何做到这一点取决于你的类打算如何使用。如果将您的类视为字符串是合理的(对于有理数似乎不太可能),则可以提供隐式用户定义的string转换。在其他情况下,不应提供隐式转换,因为隐式转换通常会妨碍编译器识别 bug 的能力。显式转换是另一种选择,但人们通常使用转换功能。(标准库中的示例包括stringstream::strbitset::to_string

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