Results 1 to 14 of 14

Thread: toolbutton action is not happening.

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default toolbutton action is not happening.

    Hi All
    I've a tool button . i'm adding some action to that toolbutton. when i trigger that action i need some methode to b executed. i did like following.
    Qt Code:
    1. header file
    2. ------------------
    3. QAction *m_action;
    4. actionlist();
    5.  
    6. source file
    7. -----------------------
    8. ui->toolButton->addAction(action);
    9.  
    10. constructor()
    11. {
    12. connect(m_action,SIGNAL( triggered()),
    13. this, SLOT(claamethode()));
    14. }
    15. void actionlist()
    16. {
    17. m_action = new QAction(m_strTempName,ui->toolButton_3);
    18. // assiging m_action here
    19. ui->toolbutton->addaction(m_action);
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    But connect methode is not working. how to solve this?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toolbutton action is not happening.

    Are you sure that m_action is not null when you establish the connection?

  3. #3
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by phillip_Qt View Post
    Hi All
    I've a tool button . i'm adding some action to that toolbutton. when i trigger that action i need some methode to b executed. i did like following.
    Qt Code:
    1. header file
    2. ------------------
    3. QAction *m_action;
    4. actionlist();
    5.  
    6. source file
    7. -----------------------
    8. ui->toolButton->addAction(action);
    9.  
    10. constructor()
    11. {
    12. connect(m_action,SIGNAL( triggered()),
    13. this, SLOT(claamethode()));
    14. }
    15. void actionlist()
    16. {
    17. m_action = new QAction(m_strTempName,ui->toolButton_3);
    18. // assiging m_action here
    19. ui->toolbutton->addaction(m_action);
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    But connect methode is not working. how to solve this?
    What is this

  4. #4
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by yogeshgokul View Post
    What is this
    Hi Yogesh. Below is the complete code.

    Qt Code:
    1. Header file
    2. ------------------
    3.  
    4. class MainWindow : public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. MainWindow(QWidget *parent = 0);
    10. ~MainWindow();
    11.  
    12. private:
    13. Ui::MainWindow *ui;
    14.  
    15.  
    16. private:
    17. //! To collect the list of classrooms and computers added to the coressponding classroom
    18. QList<QTreeWidgetItem *> *m_ClassRoom;
    19. QList<QTreeWidgetItem *> m_computersAddedToCalssroom;
    20. QStringList m_strlistClassRoom;
    21. QTreeWidgetItem *m_treelist;
    22. QList<QTreeWidgetItem *> temp;
    23.  
    24.  
    25. private slots:
    26. //! To add Computers
    27. void on_toolButton_3_clicked();
    28. void addComputers();
    29. //! To add classroom
    30. void addClassRoom();
    31.  
    32. private slots:
    33. //! To handle the right click event in The right side bar
    34. void ContextMenuDisplayMethod(const QPoint &);
    35.  
    36. private:
    37. QMenu contextMenu;
    38.  
    39. private:
    40. //! treewidget context menu
    41. QAction * m_actionClassroom;
    42. QAction * m_actionComputer;
    43. QList<QAction *> m_actionsList;
    44.  
    45. private:
    46. //SingleClass SingleClassobj;
    47. QString m_strIpEntered;
    48. QString m_strClassRoomSel;
    49. QString m_strUserNameEnter;
    50. clientSettingsDialog m_clientSettingsDlgobj;
    51.  
    52. //! To add sub Items in tree widget
    53. QModelIndex m_modelIndex;
    54.  
    55.  
    56. //! To add sub Items in tree widget
    57. QTreeWidgetItem *m_Childlist ;
    58. int index;
    59.  
    60. //! Menu showing the list of added classroom from classroom button
    61. QMenu m_classRoomListMenu;
    62. QStringList m_strlistMenu;
    63. QString m_strTempName;
    64.  
    65. private slots:
    66. void openClassInClientArea();
    67.  
    68. private:
    69. void test();
    70. QAction *acttest;
    71. QMenu menu;
    72. };
    73.  
    74. #endif // MAINWINDOW_H
    75.  
    76.  
    77. source file
    78. -------------------
    79.  
    80. SingleClass SingleClassobj;
    81.  
    82.  
    83. MainWindow::MainWindow(QWidget *parent)
    84. : QMainWindow(parent), ui(new Ui::MainWindow)
    85. {
    86. ui->setupUi(this);
    87. QWidget::showMaximized();
    88.  
    89. m_ClassRoom = new QList<QTreeWidgetItem *>;
    90.  
    91. ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    92. ui->treeWidget->setColumnCount(2);
    93. ui->treeWidget->setColumnWidth(0,200);
    94. QStringList columns;
    95. columns << tr( "Classrooms/computers" ) << tr( "IP-address" );
    96. ui->treeWidget->setHeaderLabels(columns);
    97. connect(ui->treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
    98. this, SLOT(ContextMenuDisplayMethod(const QPoint &)));
    99. //!
    100. m_actionClassroom = new QAction(tr("Add ClassRoom"),ui->treeWidget);
    101. m_actionComputer = new QAction(tr("Add Computer"),ui->treeWidget);
    102.  
    103. //! For context menu display methode in treewidget
    104. connect(m_actionClassroom, SIGNAL(triggered()), this, SLOT(addClassRoom()));
    105. connect(m_actionComputer, SIGNAL(triggered()),this, SLOT(addComputers()));
    106.  
    107. contextMenu.addAction(m_actionClassroom);
    108. contextMenu.addAction(m_actionComputer);
    109.  
    110. //! To hide client setting dialog
    111. m_clientSettingsDlgobj.hide();
    112.  
    113. acttest = new QAction(m_strTempName,ui->toolButton_3);
    114.  
    115. }
    116.  
    117. MainWindow::~MainWindow()
    118. {
    119. delete ui;
    120. }
    121.  
    122. void MainWindow::addComputers()
    123. {
    124. if(m_ClassRoom->size() == 0)
    125. {
    126. if( QMessageBox::question( window(), tr( "Missing classroom" ),
    127. tr( "Before adding computers "
    128. "you have to "
    129. "create at least one "
    130. "classroom.\nDo you "
    131. "want to create a new "
    132. "classrom now?" ),
    133. QMessageBox::No ) ==
    134. {
    135. return;
    136. }
    137. addClassRoom();
    138. }
    139. else
    140. {
    141. if(m_clientSettingsDlgobj.exec())
    142. {
    143. m_strIpEntered = m_clientSettingsDlgobj.getIP();;
    144. m_strClassRoomSel = m_clientSettingsDlgobj.getSelClassRoom();
    145. m_strUserNameEnter = m_clientSettingsDlgobj.getUserName();
    146. if(m_strUserNameEnter.isEmpty())
    147. {
    148. m_strUserNameEnter = m_strIpEntered;
    149. }
    150. temp = ui->treeWidget->findItems(m_strClassRoomSel , Qt::MatchExactly);
    151. QTreeWidgetItemIterator it(ui->treeWidget);
    152.  
    153. for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
    154. {
    155.  
    156. QTreeWidgetItem *parent = ui->treeWidget->topLevelItem(i);
    157. if(parent->text(0) == m_strClassRoomSel)
    158. {
    159. m_Childlist = new QTreeWidgetItem(parent);
    160. m_Childlist->setText(0, m_strUserNameEnter);
    161. m_Childlist->setText(1, m_strIpEntered);
    162.  
    163. return;
    164. }
    165. }
    166. }
    167.  
    168. }
    169. }
    170.  
    171. void MainWindow::addClassRoom()
    172. {
    173. bool bOk = false;
    174. QString classroom_name = QInputDialog::getText( this,
    175. tr( "New classroom" ),
    176. tr( "Please enter the name of the classroom you "
    177. "want to create." ),
    178. QLineEdit::Normal, tr( "New classroom" ), &bOk );
    179.  
    180. if(!classroom_name.isEmpty())
    181. {
    182. bool bTemp = false;
    183. for(int i = 0; i < m_strlistClassRoom.size(); i++)
    184. {
    185. if(m_strlistClassRoom.at(i) == classroom_name)
    186. bTemp = true;
    187. }
    188. if(bTemp == false)
    189. {
    190. m_strlistClassRoom.append(classroom_name);
    191. m_treelist = new QTreeWidgetItem;
    192. m_treelist->setText(0,classroom_name);
    193. m_ClassRoom->append(m_treelist);
    194.  
    195. ui->treeWidget->addTopLevelItem(m_treelist);
    196.  
    197. SingleClassobj.setSingleClass(classroom_name);
    198. }
    199.  
    200. }
    201.  
    202. for(int i = 0; i <SingleClassobj.getSingleCLass().size() ; i++)
    203. {
    204. m_clientSettingsDlgobj.setClassComboBox(SingleClassobj.getSingleCLass().at(i));
    205.  
    206. }
    207.  
    208. //! To set menu in classroom toolButton
    209. test();
    210.  
    211. }
    212.  
    213.  
    214. void MainWindow::ContextMenuDisplayMethod(const QPoint &pt)
    215. {
    216. contextMenu.popup(QCursor::pos());
    217. contextMenu.exec(QCursor::pos());
    218. }
    219.  
    220. void MainWindow::on_toolButton_3_clicked()
    221. {
    222.  
    223. qDebug()<<"INside toolButton";
    224. ui->toolButton_3->showMenu();
    225. menu.exec();
    226.  
    227. }
    228.  
    229. void MainWindow::openClassInClientArea()
    230. {
    231. // QAction * act = m_classRoomListMenu.activeAction();
    232. QAction * act = ui->toolButton_3->defaultAction();
    233.  
    234. QString strClassroom = act->text();
    235.  
    236. qDebug()<<"Class rRoom : "<<strClassroom;
    237. //QList<QTreeWidget> treewidgetchildren = ui->treeWidget->findChildren<QTreeWidget>(strClassroom);
    238.  
    239. }
    240.  
    241. void MainWindow::test()
    242. {
    243. bool bTemp = false;
    244.  
    245. for(int i = 0; i < m_strlistClassRoom.size(); i++)
    246. {
    247. bTemp = false;
    248. m_strTempName = ("ClassRoom ")+ m_strlistClassRoom.at(i);
    249. acttest = new QAction(m_strTempName,ui->toolButton_3);
    250. for(int j = 0; j < m_strlistMenu.size(); j++)
    251. {
    252. if(m_strlistMenu.at(j) == m_strTempName)
    253. {
    254. bTemp = true;
    255. }
    256.  
    257. }
    258. if(bTemp == false)
    259. {
    260. acttest->setText(m_strTempName);
    261. m_strlistMenu.append(m_strTempName);
    262. //m_classRoomListMenu.addAction(m_strTempName);
    263. qDebug()<<"MainWindow::test() : acttest value : "<<acttest->text();
    264. ui->toolButton_3->addAction(acttest);
    265. }
    266. }
    267. connect(acttest, SIGNAL(triggered()),
    268. this, SLOT(openClassInClientArea()));
    269.  
    270. }
    To copy to clipboard, switch view to plain text mode 

    Now i'm able to trigger the action. but how to get, which action is triggered. i mean in open openClassInClientArea() how ll i know that which action is triggered.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toolbutton action is not happening.

    1. the connect must inside the loop!
    2. to find what button/action was clicked use a QSignalMapper or QObject::sender().

  6. #6
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by Lykurg View Post
    1. the connect must inside the loop!
    2. to find what button/action was clicked use a QSignalMapper or QObject::sender().
    Thank u Lykurg for quick reply.
    [*]the connect must inside the loop!
    do u mean like
    Qt Code:
    1. void MainWindow::test()
    2. {
    3. bool bTemp = false;
    4.  
    5. for(int i = 0; i < m_strlistClassRoom.size(); i++)
    6. {
    7. bTemp = false;
    8. m_strTempName = ("ClassRoom ")+ m_strlistClassRoom.at(i);
    9. acttest = new QAction(m_strTempName,ui->toolButton_3);
    10. for(int j = 0; j < m_strlistMenu.size(); j++)
    11. {
    12. if(m_strlistMenu.at(j) == m_strTempName)
    13. {
    14. bTemp = true;
    15. }
    16. }
    17. if(bTemp == false)
    18. {
    19. acttest->setText(m_strTempName);
    20. m_strlistMenu.append(m_strTempName);
    21. //m_classRoomListMenu.addAction(m_strTempName);
    22. qDebug()<<"MainWindow::test() : acttest value : "<<acttest->text();
    23. ui->toolButton_3->addAction(acttest);
    24. }
    25. connect(acttest, SIGNAL(triggered()),
    26. this, SLOT(openClassInClientArea()));
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    and also
    Qt Code:
    1. void MainWindow::openClassInClientArea()
    2. {
    3. QString strClassroom = acttest->sender()->objectName();
    4. }
    To copy to clipboard, switch view to plain text mode 
    is giving error as objectname() is private methode of QObject. Can u plz suggest me how to do?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by phillip_Qt View Post
    Can u plz suggest me how to do?
    Yes, read the docs to QObject::sender()

    Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
    Qt Code:
    1. void MainWindow::openClassInClientArea()
    2. {
    3. qWarning() << QObject::sender();
    4. }
    To copy to clipboard, switch view to plain text mode 
    when you click the different buttons.

  8. #8
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by Lykurg View Post
    Yes, read the docs to QObject::sender()

    Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
    Qt Code:
    1. void MainWindow::openClassInClientArea()
    2. {
    3. qWarning() << QObject::sender();
    4. }
    To copy to clipboard, switch view to plain text mode 
    when you click the different buttons.
    Thank u very much
    I put following inside the loop
    Qt Code:
    1. connect(acttest, SIGNAL(triggered()),
    2. this, SLOT(openClassInClientArea()));
    3. strClassroom = acttest->text();
    To copy to clipboard, switch view to plain text mode 

    and its workig fine.
    Thank u very much.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  9. #9
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by Lykurg View Post
    Yes, read the docs to QObject::sender()

    Better would be using a QSignalMapper, but if you want use the sender() see what is the output of
    Qt Code:
    1. void MainWindow::openClassInClientArea()
    2. {
    3. qWarning() << QObject::sender();
    4. }
    To copy to clipboard, switch view to plain text mode 
    when you click the different buttons.
    Hi
    m_classroomActionList->sender()->objectName(); is giving error.
    error: `QObject* QObject::sender() const' is protected.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by phillip_Qt View Post
    Hi
    m_classroomActionList->sender()->objectName(); is giving error.
    error: `QObject* QObject::sender() const' is protected.
    Because you have to use
    Qt Code:
    1. QObject::sender();//or
    2. sender();
    To copy to clipboard, switch view to plain text mode 

    and NOT
    Qt Code:
    1. m_classroomActionList->sender();
    To copy to clipboard, switch view to plain text mode 
    !!! Like I wrote in the small example!

  11. #11
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by Lykurg View Post
    Because you have to use
    Qt Code:
    1. QObject::sender();//or
    2. sender();
    To copy to clipboard, switch view to plain text mode 

    and NOT
    Qt Code:
    1. m_classroomActionList->sender();
    To copy to clipboard, switch view to plain text mode 
    !!! Like I wrote in the small example!
    But
    Qt Code:
    1. strClassroom = QObject::sender()->objectName();
    To copy to clipboard, switch view to plain text mode 
    is returning a null string .
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  12. #12
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by phillip_Qt View Post
    But
    Qt Code:
    1. strClassroom = QObject::sender()->objectName();
    To copy to clipboard, switch view to plain text mode 
    is returning a null string .
    I'm stuck with the problem. Plz help me.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: toolbutton action is not happening.

    maybe it's because your sender object has no name! QObject::sender()->objectName() works perfect for me. And by the way, you don't need that name:
    Qt Code:
    1. if (QObject::sender() == ui->button1)
    2. //...
    To copy to clipboard, switch view to plain text mode 

    But that is not a good style, so better use QSignalMapper...

  14. #14
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toolbutton action is not happening.

    Quote Originally Posted by Lykurg View Post
    maybe it's because your sender object has no name! QObject::sender()->objectName() works perfect for me. And by the way, you don't need that name:
    Qt Code:
    1. if (QObject::sender() == ui->button1)
    2. //...
    To copy to clipboard, switch view to plain text mode 

    But that is not a good style, so better use QSignalMapper...
    Thank u Lykurg.
    i did like following

    Qt Code:
    1. QSignalMapper * signalMapper;// data member and allocate memory in c'tor
    2.  
    3. connect(m_classroomActionList, SIGNAL(triggered()),
    4. this, SLOT(openClassInClientArea()));
    5.  
    6. connect(m_classroomActionList, SIGNAL(triggeredd()),
    7. signalMapper, SLOT(map()));
    8.  
    9. connect(signalMapper, SIGNAL(mapped(const QString &)),
    10. this, SIGNAL(clicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    but signalMapper->objectName(); is returning null. can u plz tell me whats the mistake in my code. i'm totally stuck with this problem since last 2 days.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

Similar Threads

  1. QTreeView default drag action
    By onamatic in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2010, 07:37
  2. Problems compiling after adding an action in a menu
    By grub87 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 17:58
  3. Problem with action triggers
    By zuck in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2009, 15:08
  4. Replies: 12
    Last Post: 21st November 2008, 04:42
  5. No action checked in an exclusive action group
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 06:19

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.