QTableWigetItem

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

scope of object / QTableWidgetItem

Message 1 in thread

  • Subject: scope of object / QTableWidgetItem
  • From: KH
  • Date: Mon, 19 Nov 2007 20:15:09 +0800
  • Delivered-to: [email protected]
  • List-help:
  • List-post:
  • List-subscribe:
  • List-unsubscribe:
  • Resent-from: qt-interest@xxxxxxxxxxxxx
  • Resent-message-id:
  • Resent-sender: qt-interest-request@xxxxxxxxxxxxx
  • To: qt-interest@xxxxxxxxxxxxx

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

  • Subject: Re: scope of object / QTableWidgetItem
  • From: Ralf Neubersch
  • Date: Mon, 19 Nov 2007 13:29:23 +0100
  • Delivered-to: [email protected]
  • Hop-count: 1
  • List-help:
  • List-post:
  • List-subscribe:
  • List-unsubscribe:
  • Resent-from: qt-interest@xxxxxxxxxxxxx
  • Resent-message-id:
  • Resent-sender: qt-interest-request@xxxxxxxxxxxxx
  • To: qt-interest@xxxxxxxxxxxxx

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

 

你可能感兴趣的:(Qt)