Results 1 to 20 of 20

Thread: Problem in updating listwidget of friend class

  1. #1
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Unhappy Problem in updating listwidget of friend class

    I am unable to update (addItem) to the private data member (i.e a listwidget) of friend class (here Spam_MessagesWidget).

    I am trying to implement a feature in my application by which the selected message can be moved from Inbox Tab to Other Tab i.e removing from one listwidget and adding to other list widget.

    To help me, just check the recentmessageswidget.cpp file and reportAsSpam() method in it.

    The code is running and i am able to remove the selected item but unable to add it other tab listwidget. (Tested on Nokia QT SDK V1.0)

    Qt Code:
    1. QListWidgetItem* RecentMessagesWidget::removeitemI()
    2. {
    3. int cr = m_messageListWidget->currentRow();
    4. QListWidgetItem *temp = new QListWidgetItem(m_messageListWidget);
    5. temp = m_messageListWidget->takeItem(cr);
    6. m_messageListWidget->update();
    7.  
    8. return(temp);
    9. }
    10.  
    11.  
    12. //This function is called from a slot rspam() , in tabmain.cpp
    13. void RecentMessagesWidget :: reportAsSpam()
    14. {
    15. QListWidgetItem *temp = removeitemI();
    16.  
    17. // trying to add the removed item from "RecentMessagesWidget Class" Listwidget to "Spam_MessagesWidget friend class Listwidget"
    18.  
    19. spm = new Spam_MessagesWidget(this, SpamMessagesCount);
    20. spm->additemS(temp);
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    Your help will be appreciated and acknowledged.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in updating listwidget of friend class

    I think you are doing something weird here:
    Qt Code:
    1. QListWidgetItem *temp = new QListWidgetItem(m_messageListWidget);
    2. temp = m_messageListWidget->takeItem(cr);
    To copy to clipboard, switch view to plain text mode 
    You create new item and then immediately assign different value for the temp pointer, why ? takeItem returns a pointer to QListWidgetItem, there is no need to "create" anything in "remove" method:
    Qt Code:
    1. QListWidgetItem* RecentMessagesWidget::removeitemI()
    2. {
    3. int cr = m_messageListWidget->currentRow();
    4. QListWidgetItem *temp = m_messageListWidget->takeItem(cr);
    5. m_messageListWidget->update();
    6. return(temp);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Another thing is the reportAsSpam method - you create new Spam_MessagesWidget on every new spam message reported, I think it's not what you meant. Keep the Spam_MessagesWidget as private member of RecentMessagesWidget class, and add new spam reports to that widget instead of creating new one each time.

  3. #3
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    Actually "spm" is already a private member of RecentMessagesWidget class. (You can check out in attached code). I also tried after removing the line:

    Qt Code:
    1. spm = new Spam_MessagesWidget(this, SpamMessagesCount);
    To copy to clipboard, switch view to plain text mode 

    and with only these lines in reportAsSpam() [I also applied the correction in removeitemI() as suggested.]
    Qt Code:
    1. QListWidgetItem *temp = removeitemI();
    2. spm->additemS(temp);
    To copy to clipboard, switch view to plain text mode 

    But , now the application crashes as soon as i click on action "Report as Spam", previously it was removing the selected item though not adding in the other tab listwidget.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in updating listwidget of friend class

    I also tried after removing the line
    Ok, but do you initialize the spm pointer in constructor (or somewhere else) ? If not, then it's obvious that it will crash.

  5. #5
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    I am not sure exactly how to initialize it, but i tried the following way, but still it removes but doesn't add it to other list.

    I initialized it in RecentMessagesWidget class setupUi() method:

    Qt Code:
    1. spm = new Spam_MessagesWidget(this, SpamMessagesCount); // [added this line]
    To copy to clipboard, switch view to plain text mode 

    I have tried with simple strings also but faced the same problem. I am really struggling with the problem, Please help!!

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in updating listwidget of friend class

    setupUi() is automatically generated, don't change it because next time you run uic on the .ui file your changes will be overwritten. Initialize the private member in class constructor.

  7. #7
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    No , this time also the same thing continues. The selected item is removed but not added to the other list.

    As suggested, i changed here:

    Qt Code:
    1. RecentMessagesWidget::RecentMessagesWidget(QWidget* parent, unsigned int maxRecent)
    2. :
    3. QWidget(parent),
    4. m_messageListWidget(0),
    5. m_statusLabel(0),
    6. m_layout(0),
    7. m_maxRecent(maxRecent),
    8. m_service(new QMessageService(this)),
    9. m_state(Unloaded)
    10. {
    11. setupUi();
    12.  
    13. spm = new Spam_MessagesWidget(this, SpamMessagesCount); // I added here [initialization]
    14.  
    15. connect(m_service,SIGNAL(messagesFound(const QMessageIdList&)),this,SLOT(messagesFound(const QMessageIdList&)));
    16. connect(m_service,SIGNAL(stateChanged(QMessageService::State)),this,SLOT(stateChanged(QMessageService::State)));
    17.  
    18. //register for message update notifications
    19.  
    20. connect(&m_manager, SIGNAL(messageUpdated(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)),
    21. this, SLOT(messageUpdated(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)));
    22. connect(&m_manager, SIGNAL(messageRemoved(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)),
    23. this, SLOT(messageRemoved(const QMessageId&, const QMessageManager::NotificationFilterIdSet&)));
    24.  
    25. m_storeFilterId = m_manager.registerNotificationFilter(QMessageFilter::byStandardFolder(QMessage::InboxFolder));
    26. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in updating listwidget of friend class

    maybe show us Spam_MessagesWidget::additemS code.

  9. #9
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    Here it goes:

    Qt Code:
    1. void Spam_MessagesWidget :: additemS(QListWidgetItem* temp)
    2. {
    3.  
    4. m_messageListWidget->insertItem(0,temp);
    5.  
    6. m_messageListWidget->update();
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem in updating listwidget of friend class

    You never call setLayout(m_layout) after creating layout objects, try to call this->setLayout(m_layout); after preparing ui elements.

  11. #11
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    Still No Effect...!! I guess the function additemS() is not getting called from the RecentMessagesWidget class. I don't understand why the friend class function is not being called from here.

    I am looking for a simple working example using friend classes or friend function that can update on the private members of both the classes. I started with this simple example.

  12. #12
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    I guess the sole problem is here:

    I am posing the problem in a different form:

    Qt Code:
    1. //File recentmessageswidget.h [A simple class Containing 2 listwidgets]
    2.  
    3. class RecentMessagesWidget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit RecentMessagesWidget(QWidget *parent = 0);
    8. ~RecentMessagesWidget();
    9.  
    10. public:
    11. QListWidget* I_messageListWidget;
    12. QListWidget* S_messageListWidget;
    13.  
    14. public slots:
    15.  
    16. void processResults();
    17. void reportAsSpam();
    18.  
    19. };
    20.  
    21. //recentmessageswidget.cpp
    22.  
    23. #include "recentmessageswidget.h"
    24.  
    25. RecentMessagesWidget::RecentMessagesWidget(QWidget *parent) :
    26. QWidget(parent)
    27. {
    28. I_messageListWidget = new QListWidget(this);
    29. S_messageListWidget = new QListWidget(this);
    30. processResults();
    31. }
    32. RecentMessagesWidget::~RecentMessagesWidget()
    33. {
    34.  
    35. }
    36.  
    37. void RecentMessagesWidget::processResults()
    38. {
    39. //I_messageListWidget = new QListWidget(this);
    40. I_messageListWidget->addItem(new QListWidgetItem("I1"));
    41. I_messageListWidget->addItem(new QListWidgetItem("I2"));
    42. I_messageListWidget->addItem(new QListWidgetItem("I3"));
    43.  
    44. //S_messageListWidget = new QListWidget(this);
    45. S_messageListWidget->addItem(new QListWidgetItem("S1"));
    46. S_messageListWidget->addItem(new QListWidgetItem("S2"));
    47. S_messageListWidget->addItem(new QListWidgetItem("S3"));
    48.  
    49. S_messageListWidget->insertItem(4,QString("In fn PR")); // added in the list
    50.  
    51. }
    52. void RecentMessagesWidget :: reportAsSpam()
    53. {
    54. int cr = I_messageListWidget->currentRow();
    55. QListWidgetItem *temp = I_messageListWidget->takeItem(cr);
    56. //I_messageListWidget->update();
    57. //S_messageListWidget = new QListWidget(this);
    58. S_messageListWidget->insertItem(0,temp); // PROBLEM is here...
    59. S_messageListWidget->insertItem(5,QString("spm tab in fn")); // not added in the list
    60.  
    61. S_messageListWidget->update();
    62. I_messageListWidget->update();
    63.  
    64. }
    65.  
    66.  
    67. //In file tabmain.h
    68.  
    69. class TabMain : public QMainWindow
    70. {
    71. Q_OBJECT
    72. public:
    73. TabMain(QWidget *parent = 0);
    74. ~TabMain();
    75. private:
    76. void buildTabMenuBar(int index);
    77.  
    78. signals:
    79.  
    80. public slots:
    81. void activeTabChanged(int index);
    82. private:
    83. QTabWidget* tabWidget;
    84.  
    85. };
    86.  
    87. class InBoxTab : public QWidget
    88. {
    89. Q_OBJECT
    90.  
    91. public:
    92. InBoxTab(QWidget *parent = 0);
    93. ~InBoxTab();
    94. void setupUi();
    95. public slots:
    96. void hello();
    97. void rspam();
    98. private:
    99. RecentMessagesWidget* m_recentMessagesWidget;
    100. };
    101.  
    102. class SpamBoxTab: public QWidget
    103. {
    104. Q_OBJECT
    105.  
    106. public:
    107. SpamBoxTab(QWidget *parent = 0);
    108. ~SpamBoxTab();
    109. void setupUi();
    110. public slots:
    111. void hello();
    112. private:
    113.  
    114. RecentMessagesWidget* sm_recentMessagesWidget;
    115.  
    116.  
    117. };
    118.  
    119.  
    120.  
    121. TabMain::TabMain(QWidget *parent) :
    122. QMainWindow(parent)
    123. {
    124. setContextMenuPolicy(Qt::NoContextMenu);
    125. this->setWindowTitle("SMSAssassin Alpha");
    126.  
    127. tabWidget = new QTabWidget(this);
    128. tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
    129.  
    130. QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int)));
    131.  
    132. InBoxTab* widget1 = new InBoxTab();
    133. tabWidget->addTab(widget1, " INBOX ");
    134. SpamBoxTab* widget2 = new SpamBoxTab();
    135. tabWidget->addTab(widget2, " SPAMBOX ");
    136. //adda();
    137. setCentralWidget(tabWidget);
    138.  
    139.  
    140.  
    141. #ifdef Q_OS_SYMBIAN
    142. QWidgetList widgets = QApplication::allWidgets();
    143. QWidget* w = 0;
    144. foreach(w,widgets)
    145. {
    146. w->setContextMenuPolicy(Qt::NoContextMenu);
    147. }
    148. #endif
    149.  
    150. }
    151. TabMain::~TabMain()
    152. {
    153.  
    154. }
    155. void TabMain::activeTabChanged(int index)
    156. {
    157. buildTabMenuBar(index);
    158. }
    159. void TabMain::buildTabMenuBar(int index)
    160. {
    161. QMenuBar* menubar = menuBar();
    162. menubar->clear();
    163.  
    164. switch(index)
    165. {
    166. case 0:
    167. {
    168. menubar -> addAction("Inbox user Menu", tabWidget->widget(index),SLOT( hello() ) );
    169. menubar -> addAction("Report as Spam", tabWidget->widget(index), SLOT( rspam() ) );
    170. break;
    171. }
    172. case 1:
    173. {
    174. menubar -> addAction("SpamBox user Menu", tabWidget->widget(index), SLOT(hello()) );
    175. break;
    176.  
    177. }
    178. default:
    179. {
    180. break;
    181. }
    182. }
    183. }
    184.  
    185. InBoxTab::InBoxTab(QWidget *parent) :
    186. QWidget(parent),
    187. m_recentMessagesWidget(0)
    188. {
    189. setupUi();
    190. }
    191.  
    192. InBoxTab:: ~InBoxTab()
    193. {
    194.  
    195. }
    196.  
    197. void InBoxTab::setupUi()
    198. {
    199. QVBoxLayout* vbl = new QVBoxLayout(this);
    200. m_recentMessagesWidget = new RecentMessagesWidget(this);
    201. vbl->addWidget(m_recentMessagesWidget->I_messageListWidget);
    202. this->setLayout(vbl);
    203. }
    204. void InBoxTab :: rspam()
    205. {
    206. m_recentMessagesWidget->reportAsSpam();
    207. }
    208.  
    209. void InBoxTab::hello()
    210. {
    211. QString message = QString("Hello from %1").arg(metaObject()->className());
    212. QMessageBox* messageBox = new QMessageBox(QMessageBox::Information, "QTabsExample", message,
    213. QMessageBox::Ok, this);
    214. messageBox->exec();
    215. delete messageBox;
    216. messageBox = 0;
    217. }
    218.  
    219.  
    220. SpamBoxTab::SpamBoxTab(QWidget *parent) :
    221. QWidget(parent)
    222. {
    223. setupUi();
    224.  
    225. }
    226. void SpamBoxTab::setupUi()
    227. {
    228. QVBoxLayout* vbl = new QVBoxLayout(this);
    229. sm_recentMessagesWidget = new RecentMessagesWidget(this);
    230. vbl->addWidget(sm_recentMessagesWidget->S_messageListWidget);
    231. this->setLayout(vbl);
    232. }
    233.  
    234.  
    235.  
    236. SpamBoxTab::~SpamBoxTab()
    237. {
    238.  
    239. }
    240. void SpamBoxTab::hello()
    241. {
    242. QString message = QString("Hello from %1").arg(metaObject()->className());
    243. QMessageBox* messageBox = new QMessageBox(QMessageBox::Information, "QTabs", message,
    244. QMessageBox::Ok, this);
    245.  
    246. messageBox->exec();
    247. delete messageBox;
    248. messageBox = 0;
    249. }
    To copy to clipboard, switch view to plain text mode 

    The problem is same, item is removed from one tab but not gets added to other tab.

  13. #13
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in updating listwidget of friend class

    Attached is a very minimalist example of two listWidgets that shows that the code you think is problematic, is actually working. I don't know where and why there is an error in your program but you have to look for it somewhere else.
    Attached Files Attached Files

  14. #14
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    In my case the problem is that on selection the item is removed from one Listwidget of one TAB but doesn't get added in other TAB.

    May be my problem is due to the TAB thing. Attached is my project, You can check it, It doesn't work but doesn't throws any error too.
    Attached Files Attached Files

  15. #15
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in updating listwidget of friend class

    Besides your title and I don't see anywhere in your code to be declared a friend (as in C++ friend) class.
    Your main widget is a tabBar that creates InBoxTar and SpamBoxTab both of type RecentMessageWidget. Now, in the RecentMessageWidget's constructor there are created two list widget (I_.. and S_...)
    So what you have is
    Tabbar----InBoxTab------RecentMessageWidget's ---I_messageListWidget (1)+S_messageListWidget (2)
    Tabbar----SpamBoxTab---RecentMessageWidget's---I_messageListWidget (3)+S_messageListWidget (4)
    (2) and (3) are invisible because they are not added in the layout, but does not mean they don't exist. What you are doing is a connection between (1) and (2), though what you want is a connection between (1) and (4) which simply exist in different objects.

    My suggestion: Create only one listWidget. Maybe it's better RecentMessage to be a subclass of QListWidget. Add a method that returns the currentItem, and a method that reports this QListWidgetItem.
    Qt Code:
    1. QListWidgetItem* RecentMessagesWidget::getCurrentItem();
    2. void RecentMessagesWidget::reportAsSpam(QListWidgetItem *temp);
    To copy to clipboard, switch view to plain text mode 
    So in your tabBar class you can call something like
    Qt Code:
    1. spamBox->reportAsSpam(inBox->getCurrentItem());
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    Friend is in my original code which is in my first thread. Initially, i thought my problem is due to friend class.

    And, Why i want to keep 2 listwidgets is that i wish to maintain 2 different lists containing different items in 2 tabs.

  17. #17
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in updating listwidget of friend class

    Quote Originally Posted by dipeshtech View Post
    And, Why i want to keep 2 listwidgets is that i wish to maintain 2 different lists containing different items in 2 tabs.
    Since this is by design then it's ok, but you have to let the two objects(1 and 4 in my previous post) to be known to eachother.

  18. #18
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    If you have browsed my code, can you please help me in getting it work, the way i want it. If you can point out the changes to made (in my code) for this. I am just a month old to it.

  19. #19
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in updating listwidget of friend class

    See the changes made for the QListWidgetItem to be passed from Inbox object to SpamBox object.
    Please note that I don't "agree" with your implementation. You are making things too complicated without reason (but I will not redesign your classes )

    NOTE: I had to comment out many headers that were not in my system to compile but they are irrelevant to your problem
    Attached Files Attached Files

  20. The following user says thank you to Rhayader for this useful post:

    dipeshtech (25th March 2011)

  21. #20
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem in updating listwidget of friend class

    Rhayader Thanks A TON...!! for your help and time. I learned few very very important things.

    Forgive my ignorance on the problem. But, i am still thinking that there were 2 classes with 2 listwidget as members, i was thinking to have a friend function sort of thing which can be called as slot.

    Actually, i also don't like my implementation but currently i am experimenting and i am not aware of many things. And sorry for those headers, that belong to my original project, i forgot to remove it for discussion here.

    But thanks a Ton !
    --
    Dipesh

Similar Threads

  1. Problem in updating XML file
    By Qt_Kid in forum Qt Programming
    Replies: 11
    Last Post: 27th February 2011, 10:54
  2. Replies: 7
    Last Post: 2nd September 2010, 19:42
  3. Problem with QListWidget Updating
    By flob in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2009, 17:05
  4. friend class in TC++ book
    By mickey in forum General Programming
    Replies: 11
    Last Post: 25th August 2008, 22:08
  5. ListWidget itemClicked problem
    By Keemosabi in forum Newbie
    Replies: 2
    Last Post: 11th August 2008, 22:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.