Results 1 to 7 of 7

Thread: connect a QPushButton matrix with a function

  1. #1
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default connect a QPushButton matrix with a function

    Hello, here I am again!

    I'm trying to connect the clicked() events of the buttons of a QPushButton matrix with a custom function. The problem is that the function needs the indexes of the clicked button...in the connect() reference is told that I cannot do such a thing:
    Qt Code:
    1. p = new QPushButton*[nrow];
    2. for (int i=0; i<nrow; i++)
    3. p[i] = new QPushButton[ncol];
    4.  
    5. for (int i=0; i<nrow; i++)
    6. for (int j=0; j<ncol; j++)
    7. p[i][j].connect(&p[i][j],SIGNAL(clicked()), actObj, SLOT(triggerFunc(int i, int j)));
    To copy to clipboard, switch view to plain text mode 

    actObj is an object which holds the slot triggerFunc(int, int).
    I need to index the button of p that was clicked and call the triggerFunc() function using i and j properly...are there other ways to connect a widget, object, button, etc, with a function?

    thanks again

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect a QPushButton matrix with a function

    Look like signal mapper is what you need

  3. #3
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a QPushButton matrix with a function

    yeah, I thiks it will help! I'll try it tomorrow...good night and thanks a lot!

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect a QPushButton matrix with a function

    Also do remember the number of arguments of SLOT must be equal to or less than the arguments of the SIGNAL

  5. #5
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a QPushButton matrix with a function

    but how do I assign an argument of the SLOT to an argument of the SIGNAL? The order must be the same?

  6. #6
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a QPushButton matrix with a function

    I'm having troubles with the code here:

    inputmatrix.h:
    Qt Code:
    1. #ifndef INPUTMATRIX_H
    2. #define INPUTMATRIX_H
    3. #include <QPushButton>
    4. #include <QObject>
    5. #include <QSignalMapper>
    6.  
    7. class InputMatrix : QObject
    8. {
    9. Q_OBJECT
    10. private:
    11. int nrow;
    12. int ncol;
    13. QSignalMapper *signalMapper;
    14. public:
    15. InputMatrix();
    16. InputMatrix(int nrow, int ncol);
    17. ~InputMatrix();
    18. void Show(QWidget*);
    19. QPushButton GetItem(int i,int j);
    20. public slots:
    21. void SetItem(const QString &ID);
    22. };
    23.  
    24. #endif // INPUTMATRIX_H
    To copy to clipboard, switch view to plain text mode 


    inputmatrix.cpp:
    Qt Code:
    1. #include "inputmatrix.h"
    2. #include <QPushButton>
    3. #include "mainwindow.h"
    4. #include <QSignalMapper>
    5. #include <QString>
    6. #include <QMessageBox>
    7. #include <QChar>
    8.  
    9. InputMatrix::InputMatrix()
    10. {}
    11.  
    12. InputMatrix::InputMatrix(int nr, int nc)
    13. {
    14. ncol=nc;
    15. nrow = nr;
    16. p = new QPushButton*[nrow];
    17. for (int i=0; i<nrow; i++)
    18. p[i] = new QPushButton[ncol];
    19.  
    20. }
    21.  
    22. void InputMatrix::Show(QWidget *obj)
    23. {
    24. int alt = (obj->height()-100)/nrow;
    25.  
    26. signalMapper= new QSignalMapper(this);
    27.  
    28. const QString ID = "ij"; // this will identify the buttons in the PushButton matrix
    29. for (int i=0; i<nrow; i++)
    30. {
    31. ID.insert(0,(Qchar) i); // FIRST ERROR
    32. for (int j=0; j<ncol; j++)
    33. {
    34. ID.insert(1,(QChar) j); // SECOND ERROR
    35. p[i][j].setParent(obj); // obj = window on which I'll show the matrix
    36. p[i][j].setGeometry(50+(j*alt), 70 +(i*alt),alt, alt);
    37. connect(&p[i][j], SIGNAL(clicked()), signalMapper, SLOT(map()));
    38. signalMapper->setMapping(&p[i][j], ID);
    39. p[i][j].show();
    40. }
    41. }
    42. connect(signalMapper, SIGNAL(mapped(const QString &)),
    43. this, SLOT(SetItem(const QString &)));
    44.  
    45. }
    46.  
    47. void InputMatrix::SetItem(const QString & ID)
    48. {
    49.  
    50. QString aux = "You have pressed ";
    51. aux.append(ID);
    52. msg.setText(aux);
    53. msg.exec();
    54.  
    55. }
    56.  
    57. QPushButton InputMatrix::GetItem(int i,int j)
    58. {
    59. //return p[i][j];
    60.  
    61. }
    62.  
    63. InputMatrix::~InputMatrix()
    64. {
    65. for (int i=0; i<nrow; i++)
    66. delete [] p[i];
    67. delete p;
    68.  
    69. }
    To copy to clipboard, switch view to plain text mode 

    the error is the same at both of the rows I commented in the code. It is:
    passing 'const QString' as 'this' argument of 'QString& QString::insert(int, QChar)' discards qualifier
    why I'm wrong?

  7. #7
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect a QPushButton matrix with a function

    solved simply I declared a const ID QString so the compiler was warning me that I was changing a const string

    but now I have a "undefined reference to 'vtable for InputMatrix' " error on the constructor and destructor prototype lines of code...

    SOLVED: I only had to run "qmake"
    Last edited by harmodrew; 6th August 2010 at 11:30.

Similar Threads

  1. connect a qpushbutton a slot
    By Lycus HackerEmo in forum Newbie
    Replies: 13
    Last Post: 29th March 2010, 09:14
  2. QPushButton function
    By maider in forum Qt Programming
    Replies: 7
    Last Post: 22nd October 2009, 15:01
  3. Replies: 1
    Last Post: 12th January 2009, 18:05
  4. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38
  5. Replies: 3
    Last Post: 26th September 2006, 12:16

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.