是否有一个函数可以显示我创建的类中的对象到 QTextBrowser 中?
Is there a function that can display an object from a class that I've created into a QTextBrowser?
我正在创建一个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::str
和bitset::to_string
。
您已经编写了大部分转换函数。您所需要的只是将对象流式传输到std::stringstream
然后调用该流的str()
方法。尽可能合理地重用您的代码。
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 是否有一个函数可以显示我创建的类中的对象到 QTextBrowser 中?