Results 1 to 9 of 9

Thread: Slot - Signal with parameter

  1. #1
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Slot - Signal with parameter

    Hi, can you help me, please...

    i have..

    Qt Code:
    1. for(x) ( QPushButton * pb = new QPushButton; ... )
    To copy to clipboard, switch view to plain text mode 

    x - is 5x or 15x, ... but when i click on button, how i can know which button was pressed ?..
    some example will help me..

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot - Signal with parameter

    QSignalMapper:

    Qt Code:
    1. for (...) {
    2. QPushButton *button = new QPushButton(...);
    3. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    4. signalMapper->setMapping(...);
    5. }
    6.  
    7. connect(signalMapper, SIGNAL(mapped(argtype)), this, SIGNAL(clicked(argtype)));
    To copy to clipboard, switch view to plain text mode 

    argtype can be int, QString, anything supported by QSignalMapper

  3. #3
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot - Signal with parameter

    thank you for quick reply ;-)

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Slot - Signal with parameter

    Use Sender()
    eg: Button *clickedButton = qobject_cast<Button *>(sender());

    Qt Code:
    1. void Calculator::unaryOperatorClicked()
    2. {
    3. Button *clickedButton = qobject_cast<Button *>(sender());
    4. QString clickedOperator = clickedButton->text();
    5. double operand = display->text().toDouble();
    6. double result = 0.0;
    7.  
    8. if (clickedOperator == tr("Sqrt")) {
    9. if (operand < 0.0) {
    10. abortOperation();
    11. return;
    12. }
    13. result = sqrt(operand);
    14. } else if (clickedOperator == tr("x\262")) {
    15. result = pow(operand, 2.0);
    16. } else if (clickedOperator == tr("1/x")) {
    17. if (operand == 0.0) {
    18. abortOperation();
    19. return;
    20. }
    21. result = 1.0 / operand;
    22. }
    23. display->setText(QString::number(result));
    24. waitingForOperand = true;
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot - Signal with parameter

    sender() violates the object-oriented principle of modularity, which is why you should not use it unless there's no alternative (and in this case, there is).

    In your example, you could remove 'clickedButton' from your code completely, and have 'clickedOperator' passed to it as a parameter, which is much more readable.

  6. #6
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot - Signal with parameter

    Hi a have a new problem...
    when i want change properties of created button, program go to run-time error.

    Qt Code:
    1. void create () (
    2. for (...) QPushButton * b = new QPushButton;
    3. Struct.b[Struct.top++]=b;
    4. )
    To copy to clipboard, switch view to plain text mode 

    theStruct is declare in class MainWindow in public. And in othe fun.:

    Qt Code:
    1. Struct.b[..]->setText("change");
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slot - Signal with parameter

    Hi, look at the calc example bundled with Qt in examples. Looks like your case.

    Cheers,
    -- tanuki

  8. #8
    Join Date
    Apr 2013
    Location
    luxemburg
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot - Signal with parameter

    thanks again but i dont get it just yet...

    all the widgets have to know about the myData Object in MainWindow. so is it better to solve this by Singleton or do i give all the widgets a reference to my Data in the constructor, Or is there another way how it should be done?

    Qt Code:
    1. class myMessage : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. myMessage(QObject *parent = 0,myData*);
    6. public slots:
    7. void showMesssage();
    8. void showMesssage(QString *s);
    9. void newMeesageText(QString *s);
    10. private:
    11. QMessageBox *message;
    12.  
    13. };
    14. //and in MainWIndow i make:
    15. myData * data;
    16. myMessage *mess(this,data);
    To copy to clipboard, switch view to plain text mode 
    in that way all the widgets have the pointer to the data in themselve and i i update it in another widget the changes are accessable

    I hope you get my question

    thank

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Slot - Signal with parameter

    Post does not make any sense in this thread
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Replies: 3
    Last Post: 3rd May 2009, 14:15
  3. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  4. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  5. Replies: 12
    Last Post: 18th September 2008, 15:04

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.