Results 1 to 7 of 7

Thread: Custom Signals

  1. #1
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom Signals

    Hello, I'm having a big problem with signals, this is it:

    I have many QPushButton and a QLineEdit in a widget witch the class name is myclass, their names are cmd_1, cmd_2 and cmd_3, and txt. They are declared in the class declaration like so:

    Qt Code:
    1. class myclass: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. myclass(QWidget *parent = 0);
    7. ~myclass();
    8.  
    9. private:
    10.  
    11. QPushButton *cmd_1;
    12. QPushButton *cmd_2;
    13. QPushButton *cmd_3;
    14.  
    15. QLineEdit *txt;
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    What I really need is that when you click in one of the buttons, its values, 1, 2 or 3, is written in txt widget. I have spend many many hours trying to use connect with custom signals but I just couldn't do it. I have also spend many hour in google and didn't find the answer for my questions about custom signals and emits and that stuff. For me the connect function is starting to bee a big mindf#ck. Can someone please help me understand this or just give a web link to help me.

    Thanks for now and sorry for my not so good english.
    Last edited by wysota; 23rd July 2009 at 09:59. Reason: reformatted to look better

  2. #2
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Signals

    Try this. It worked for me if I understand you correctly.

    1. In Qt designer, go to signals/slots editor.
    2. Right-click cmd_1.
    3. Choose "Go to slot..."
    4. Choose "clicked()". This will create a slot for you.
    5. In the slot, update your LineEdit txt widget. ui->txt->setText("1");
    6. Do the same for the other buttons.


    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. private slots:
    23. void on_cmd_3_clicked();
    24. void on_cmd_2_clicked();
    25. void on_cmd_1_clicked();
    26. void on_pushButton_clicked();
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    30.  
    31.  
    32. #include "mainwindow.h"
    33. #include "ui_mainwindow.h"
    34.  
    35. MainWindow::MainWindow(QWidget *parent)
    36. : QMainWindow(parent), ui(new Ui::MainWindow)
    37. {
    38. ui->setupUi(this);
    39. }
    40.  
    41. MainWindow::~MainWindow()
    42. {
    43. delete ui;
    44. }
    45.  
    46. void MainWindow::on_pushButton_clicked()
    47. {
    48. qDebug("button was pushed");
    49. }
    50.  
    51. void MainWindow::on_cmd_1_clicked()
    52. {
    53. ui->txt->setText("1");
    54. }
    55.  
    56. void MainWindow::on_cmd_2_clicked()
    57. {
    58. ui->txt->setText("2");
    59. }
    60.  
    61. void MainWindow::on_cmd_3_clicked()
    62. {
    63. ui->txt->setText("3");
    64. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Custom Signals

    Please also notice the Newbie section of this board!

    You have to establish a normal signal slot connection:

    Qt Code:
    1. class myclass: public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. myclass(QWidget *parent = 0);
    6. ~myclass();
    7.  
    8. private slots:
    9. void addTextForButton1();
    10.  
    11. private:
    12. QPushButton *cmd_1;
    13. QPushButton *cmd_2;
    14. QPushButton *cmd_3;
    15. QLineEdit *txt;
    16. }
    17.  
    18. myclass::myclass()
    19. {
    20. QObject::connect(cmd_1, SIGNAL(clicked()), this, SLOT(addTextForButton1()));
    21. }
    22.  
    23. void myclass::addTextForButton1()
    24. {
    25. txt->setText(txt->text() + "1");
    26. }
    To copy to clipboard, switch view to plain text mode 

    Read this and you also might want to use QObject::sender().

  4. #4
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Signals

    Quote Originally Posted by Lykurg View Post
    Please also notice the Newbie section of this board!
    Oh didn't notice that, thanks for the advice.

    And Thank you both for the explanation, very helpful!!

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom Signals

    I'd use a QSignalMapper.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom Signals

    you don't have to spend many hours googling - just use Qt Assistant where you can find many informations about Qt classes and mechanisms.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Signals

    Quote Originally Posted by wysota View Post
    I'd use a QSignalMapper.
    Thanks, I will explor that, it may be helpful in the future.

    Quote Originally Posted by faldżip View Post
    you don't have to spend many hours googling - just use Qt Assistant where you can find many informations about Qt classes and mechanisms.
    I did used Qt Assistant however I couldn't understand a few thinks. In some other cases it has been very helpful. For example in using keyPressEvent and so.

Similar Threads

  1. custom signals
    By talk2amulya in forum Qt Programming
    Replies: 9
    Last Post: 20th February 2009, 08:55
  2. Custom Widget - First Steps
    By sekatsim in forum Qt Programming
    Replies: 8
    Last Post: 26th June 2008, 17:19
  3. custom signals vs newbie
    By Raccoon29 in forum Newbie
    Replies: 6
    Last Post: 21st April 2008, 16:29
  4. Custom signals?
    By godot in forum Newbie
    Replies: 7
    Last Post: 14th January 2008, 19:13

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.