0

I wish to add items in a ListWidget, which is a private member of a class, through a friend function. Actually, i am trying this sample snippet to use friend function for more classes to update their ListWidgets from a single function.

I need guidance in using friend function in my case.

Kindly forgive my ignorance on the topic, any help is appreciated.

    class InBoxTab : public QWidget
    {
    Q_OBJECT

    public:
        InBoxTab(QWidget *parent = 0);
       // InBoxTab();
        ~InBoxTab();

    public slots:
        void hello();
        friend void adda(); // friend function
    private:
        QListWidget* listWidget1; //data member accessed by friend function
    };



    void adda()
    {
        InBoxTab I;

        I.listWidget1->insertItem(1,QString("added frm fn"));

        I.listWidget1->update();
    }


InBoxTab::InBoxTab(QWidget *parent) :
        QWidget(parent)
{
        listWidget1 = new QListWidget(this);

        QListWidgetItem* item = new QListWidgetItem("Item 1 added frm tab1 ");

        listWidget1->addItem(item);
        adda();   // Call to friend function

        QVBoxLayout* layout = new QVBoxLayout(this);
        layout->addWidget(listWidget1);
        this->setLayout(layout);
}
dipeshtech
  • 384
  • 1
  • 5
  • 16
  • I am not getting any error message. No Build issues. But, my private data member is also not updated by friend function and that itself is the PROBLEM. – dipeshtech Mar 23 '11 at 22:18

2 Answers2

0
void adda()
{
    InBoxTab I;

    I.listWidget1->insertItem(1,QString("added frm fn"));

    I.listWidget1->update();
}

InBoxTab::InBoxTab(QWidget *parent) :
          QWidget(parent)
{
    // ...

    adda();   // Call to friend function

    // ..
 }

In the function adda(), a new object called I is created. So, constructor is called and constructor inturn calls again adda() and the process goes on . I see an infinite recursion which is the problem.


Edit:

InBoxTab(QWidget *parent = 0); // Since parent is initialized to 0 if nothing 
                               // is passed to constructor up instantiation 

InBoxTab I; // Invokes the above constructor and an infinite recursion results.
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Yeah..!! Right..!! i tried it putting in another slot, but that also doesn't update the list. – dipeshtech Mar 23 '11 at 21:50
  • @dipeshtech- I am not aware of `qt` but a question to ask. Why are you thinking of having a `friend` function in the design ? – Mahesh Mar 23 '11 at 21:56
  • I am thinking of using Friend function because i wanted to update (add/remove) items in listwidget (private members) of 2 classes, this i thought, in order to implement a feature in my project. I have also tried Friend Classes , and i don't get ANY ERRORS, but NO List is updated too. I am working on Nokia QT SDK – dipeshtech Mar 23 '11 at 22:13
0

As far as I can see, the 'adda' function does not affect anything. It returns nothing, and only operates on 'I' which is deleted when 'adda' is finished.

An example of how I believe you could use a friend function would be if you declared/defined 'adda' as:

void adda(InBoxTab *I)
{
        I->listWidget1->insertItem(1,QString("added frm fn"));
        I->listWidget1->update();
}

... Although in that particular case there is no reason not to make 'adda' a member of the InBoxTab instead.

ursus
  • 16
  • 1