Results 1 to 5 of 5

Thread: QPushButton with Signals and Slots into a QLienEdit

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPushButton with Signals and Slots into a QLienEdit

    Hi everyone, (Sorry for my bad english)

    I am trying to create a Pin Code for my program, 3 numbers in the horizontal and 3 numbers in the vertical. Then when the user click in the QPushButton with the number, the text of the QPushButton is send to the QLineEdit....

    I have already done that :

    Qt Code:
    1. QPushButton *BtnPin;
    2.  
    3. char *Numeros[9] = {"1","2","3","4","5","6","7","8","9"};
    4. int posicao = 0;
    5.  
    6. for (int i=0; i<3; i++)
    7. {
    8. for (int j=0; j<3; j++)
    9. {
    10. BtnPin = new QPushButton(Numeros[posicao],this);
    11. BtnPin->setText(Numeros[posicao]);
    12. BtnPin->setMinimumSize(50,50);
    13. BtnPin->setMaximumSize(50,50);
    14. layout->addWidget(BtnPin,i,j);
    15. connect(BtnPin,SIGNAL(clicked()),this,SLOT(AddPinNumero()));
    16. posicao++;
    17. }
    18. }
    19.  
    20. ui->frame->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 

    All the buttons appears but if I click, it only returns the last number added (9), how can I solve the problem ?

    Thanks in advance,
    Luis Da Costa

  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: QPushButton with Signals and Slots into a QLienEdit

    I don't know where something is added, but you are probably looking for QSignalMapper.

  3. #3
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPushButton with Signals and Slots into a QLienEdit

    Thanks for your help, I have already tried that but it doesn't work.

    But this works :

    Qt Code:
    1. QPushButton *BtnClicado = qobject_cast<QPushButton *>(sender());
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QPushButton with Signals and Slots into a QLienEdit

    QSignalMapper can be used this way

    Qt Code:
    1. QPushButton *BtnPin;
    2. QSignalMapper* signalMapper = new QSignalMapper(this);
    3.  
    4. QStringList Numeros = QStringList()<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9";
    5. int posicao = 0;
    6.  
    7. for (int i=0; i<3; i++)
    8. {
    9. for (int j=0; j<3; j++)
    10. {
    11. BtnPin = new QPushButton(Numeros[posicao],this);
    12. BtnPin->setText(Numeros[posicao]);
    13. BtnPin->setMinimumSize(50,50);
    14. BtnPin->setMaximumSize(50,50);
    15. layout->addWidget(BtnPin,i,j);
    16. connect(BtnPin, SIGNAL(clicked()), signalMapper, SLOT(map()));
    17. signalMapper->setMapping(BtnPin, Numeros[posicao]);
    18. posicao++;
    19. }
    20. }
    21.  
    22. connect(signalMapper, SIGNAL(mapped(const QString &)),
    23. this, SLOT(AddPinNumero(const QString &)));
    To copy to clipboard, switch view to plain text mode 


    But this works :

    Qt Code:
    1. QPushButton *BtnClicado = qobject_cast<QPushButton *>(sender());
    To copy to clipboard, switch view to plain text mode 
    This might have worked, you really need to understand why this worked, weather you want to use it this way, else will run to problems later on.

  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QPushButton with Signals and Slots into a QLienEdit

    You've only got one slot for all nine buttons -- how do you tell which button was pressed?

    Note that you could in theory use QObject.sender() to get the QPushButton pointer (you'd have to cast the value to a QPushButton*) and then query QPushButton.text() to retrieve the "value" of the button.

Similar Threads

  1. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  2. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32
  3. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 15:48
  4. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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.