Results 1 to 20 of 20

Thread: Problem in updating listwidget of friend class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

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

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

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

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

  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

    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.

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

  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

    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

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

  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

    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.

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

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
  •  
Qt is a trademark of The Qt Company.