QTableWigetItem

原文地址: http://lists.trolltech.com/qt-interest/2007-11/thread00620-0.html

scope of object / QTableWidgetItem

Message 1 in thread

Hello,

I have the following method which gets called a few times:

void Relationship::fillTable(int relNo, QDomElement relationshipChild){
        myPersona -> slotCreateRelationship();
        QString counterpart =
relationshipChild.firstChildElement("counterpart").text();
        QTableWidgetItem *newItem = new QTableWidgetItem(counterpart);
        myPersona -> tableWidget -> setItem(relNo, 0, newItem);

        QString kind = relationshipChild.firstChildElement("kind").text();
        QTableWidgetItem *newItem2 = new QTableWidgetItem(kind);
        myPersona -> tableWidget -> setItem(relNo, 1, newItem2);

        QString duration = relationshipChild.firstChildElement("duration").text();
        QTableWidgetItem *newItem3 = new QTableWidgetItem(duration);
        myPersona -> tableWidget -> setItem(relNo, 2, newItem3);
}

Basically the relNo value changes with every call. I am puzzeled by a few
things:

1.
I declare for example QTableWidgetItem *newItem again for every call of this
method. On the one hand side I would expect that I get a problem that an
object is declared multiple times. But then the objects only lives in the
scope of that method. But it must live beyond that because the item is
still displayed after the method has finished.

2.
I have one and the same QTableWidgetItem used multiple times. Each time I
assign a different text. I would expect that the text can only be assigned
once and if I change it, it will be changed at any location (table cell).
Why can I have the same object with different texts?

Thanks!

--
 [ signature omitted ] 


Message 2 in thread

Hi,

>         QTableWidgetItem *newItem = new QTableWidgetItem(counterpart);
>         QTableWidgetItem *newItem2 = new QTableWidgetItem(kind);
>         QTableWidgetItem *newItem3 = new QTableWidgetItem(duration);
> 1.
> I declare for example QTableWidgetItem *newItem again for every call of this
> method. On the one hand side I would expect that I get a problem that an
> object is declared multiple times. But then the objects only lives in the
> scope of that method. But it must live beyond that because the item is
> still displayed after the method has finished.

Well, that is not an Qt issue, but a basic c++ question. What makes you
think you declare the same object more than once? You are calling "new"
here every time, so this is what it does: it gives you a "new" element,
which is created dynamically. AND: dynamically allocated objects do not
only live in the scope of the method, they live until they are
"delete"d.


So for example

	void Relationship::createElements(int counterpart)
	{
		QTableWidgetItem *newItem = new 	QTableWidgetItem(counterpart);
		newItem = new QTableWidgetItem(counterpart);
		newItem = new QTableWidgetItem(counterpart);

		qDebug() << (int) newItem;
	}

would result in the creation of three different QTableWidgetItems.
newItem "points" to the last one created, the other two previously
created objects still "live", although they are not accessible anymore,
since you do not have any pointer to them.

> 2.
> I have one and the same QTableWidgetItem used multiple times. Each time I
> assign a different text. I would expect that the text can only be assigned
> once and if I change it, it will be changed at any location (table cell).
> Why can I have the same object with different texts?

In fact you do not have the same object, but create a new in each call.
So there is no problem in having different texts.

It seems as if you should have a look into some c++ book and read on the
new/delete topic....

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值