我希望通過朋友函數在ListWidget(它是一個類的私有成員)中添加項目。實際上,我正在嘗試使用這個示例代碼片段來使用更多類的朋友函數來從單個函數更新其ListWidgets。在QT中使用Friend函數的問題
我需要指導在我的情況下使用朋友功能。
請原諒我對這個話題的無知,任何幫助表示讚賞。
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);
}
什麼是錯誤信息? – Mahesh 2011-03-23 21:35:40
那麼你的問題是什麼? – TonyK 2011-03-23 21:35:42
我沒有收到任何錯誤消息。沒有構建問題。但是,我的私人數據成員也沒有被朋友功能更新,這本身就是問題所在。 – dipeshtech 2011-03-23 22:18:49