Results 1 to 5 of 5

Thread: Override a signal

  1. #1
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Override a signal

    I want to set the text of a label when I click a button.

    I have a button called 'pushButton' and a label called 'label'.

    My form class is like this (buttonform.h):

    Qt Code:
    1. #ifndef BUTTONFORM_H
    2. #define BUTTONFORM_H
    3.  
    4. #include "ui_buttonform.h"
    5.  
    6. class ButtonForm : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ButtonForm(QWidget *parent = 0);
    12.  
    13. private slots:
    14. //void pushButton_Clicked();
    15.  
    16. signals:
    17. void pushButton_Clicked();
    18. private:
    19. Ui::ButtonForm ui;
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    In buttonform.cpp, I had this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "buttonform.h"
    4. #include <iostream>
    5.  
    6. ButtonForm::ButtonForm(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. ui.setupUi(this);
    10. }
    11.  
    12. void ButtonForm::pushButton_Clicked()
    13. {
    14. ui.label->setText("hello");
    15. }
    To copy to clipboard, switch view to plain text mode 

    but it tells me that pushButton_Clicked() is already defined in moc_buttonform.cxx.

    How should I go about doing this?

    Thanks!

    Dave

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Override a signal

    Hi, you are implementing a signal, which is already done by the moc for you and therefore you get the error.
    I think what you want is the line you commented: a slot to connect the button's clicked() signal.

    Ginsengelf

  3. #3
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Re: Override a signal

    Ok, I guess the reason I didn't know how to do that is that I didn't know how to associate the slot with a particular widget (the pushButton). I think the answer is that you don't - you just associate it with the form:

    The problem now is that it says "No such slot ButtonForm:ushButton_SetLabelText(QString& txt)

    Here is what I have:

    buttonform.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "buttonform.h"
    4. #include <iostream>
    5.  
    6. ButtonForm::ButtonForm(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. ui.setupUi(this);
    10.  
    11.  
    12. connect( this->ui.pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText(QString& txt)) );
    13.  
    14. }
    15.  
    16. void ButtonForm::pushButton_SetLabelText(QString& txt)
    17. {
    18. this->ui.label->setText(txt);
    19. }
    To copy to clipboard, switch view to plain text mode 


    buttonform.h
    Qt Code:
    1. #ifndef BUTTONFORM_H
    2. #define BUTTONFORM_H
    3.  
    4. #include "ui_buttonform.h"
    5.  
    6. class ButtonForm : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ButtonForm(QWidget *parent = 0);
    12.  
    13. private slots:
    14. void pushButton_SetLabelText(QString& txt);
    15.  
    16. signals:
    17. //void pushButton_Clicked();
    18. private:
    19. Ui::ButtonForm ui;
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "buttonform.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. ButtonForm buttonform;
    9.  
    10. buttonform.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I feel like this is very close!

    Thanks for your help,

    Dave

  4. #4
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Re: Override a signal

    Ah, got it. The problem was the Clicked() signal did not pass a QString

    I changed it to:
    Qt Code:
    1. void pushButton_SetLabelText();
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. ButtonForm::ButtonForm(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5. connect( this->ui.pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText()) );
    6.  
    7.  
    8.  
    9. }
    10.  
    11. void ButtonForm::pushButton_SetLabelText()
    12. {
    13. this->ui.label->setText("hello");
    14. }
    To copy to clipboard, switch view to plain text mode 

    I thought one of the main points of the Signal/Slot idea was to stop type problems like this (e.g. passing nothing to something expecting a QString)?

    Dave

  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: Override a signal

    You don't need the connect call, just name your function like so:

    Qt Code:
    1. void on_pushButton_clicked()
    2. {
    3. // foo
    4. }
    To copy to clipboard, switch view to plain text mode 

    and everything is done for you

    You only need the connect call if you want to call your function something different than the normal.

Similar Threads

  1. override the default cursor
    By lytfyre in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2008, 20:00
  2. ActiveQt: can I override lcid?
    By a550ee in forum Qt Programming
    Replies: 0
    Last Post: 3rd October 2007, 07:44
  3. override wheelEvent for QScrollBar
    By ChasW in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 09:50
  4. QTextEdit setText override?
    By ksierens in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2006, 17:59
  5. override minimumSize function
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2006, 05:28

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.