Results 1 to 18 of 18

Thread: custom slot/signal

  1. #1
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default custom slot/signal

    Hi Im new here and Id like to ask a few questions please,

    In my program there is a QLineEdit and a QLabel. What I want to do is when the user enter something in the LineEdit, I want to do some processing with it then displaying the result in QLabel. I was thinking of something like
    connect(lineEdit, SIGNAL(returnPressed()), qLabel, SLOT(setText("asdf")))
    but after some reading i found out that you cannot pass values in connect and besides with the above code i cannot process the input from lineedit. I read the documentation about signals and slots and I understood it somehow but I just didnt understand it enough to write my own slot/signal.

    so can somebody please help me to understand and write my own slot/signal. thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: custom slot/signal

    Let's say you have a main window containing those widgets. Then the code could look something like this:

    header:
    Qt Code:
    1. ...
    2. class MainWindow: public QMainWindow
    3. {
    4. Q_OBJECT // <- this macro is important
    5.  
    6. public:
    7. MainWindow(...);
    8. ~MainWindow();
    9. ...
    10.  
    11. // your own custom slot declaration looks like this:
    12. private slots:
    13. void processInput();
    14. };
    To copy to clipboard, switch view to plain text mode 

    implementation:
    Qt Code:
    1. MainWindow::MainWindow(...) : ...
    2. {
    3. // connection between line edit and your custom slot
    4. connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(processInput()));
    5. }
    6.  
    7. // custom slot implementation
    8. void MainWindow::processInput()
    9. {
    10. // process the line edit text
    11. QString text = process(lineEdit->text());
    12.  
    13. // set the result
    14. qLabel->setText(text);
    15. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    thank you for the speedy reply, now here is the code

    Qt Code:
    1. #include<QApplication>
    2. #include<QLabel>
    3. #include<QLineEdit>
    4. #include<QString>
    5.  
    6. class myWidget:public QWidget{
    7. Q_OBJECT
    8.  
    9. public:
    10. myWidget(QWidget *parent=0);
    11. private slots:
    12. void processInput();
    13. };
    14.  
    15. myWidget::myWidget(QWidget *parent)
    16. :QWidget(parent)
    17. {
    18. QLabel *display=new QLabel(this);
    19.  
    20. QLineEdit *lineEdit=new QLineEdit;
    21.  
    22. connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(processInput()));
    23.  
    24. }
    25.  
    26. void myWidget::processInput(){
    27. QString text=lineEdit->text();
    28. display->setText(text);
    29. }
    30.  
    31. int main(int argc, char **argv){
    32. QApplication app(argc, argv);
    33. myWidget window;
    34.  
    35. window.show();
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

    and i got undeclared error of lineEdit and display

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    The problem is that you declare lineEdit as a local variable in the constructor. If you want to access it in a different method, you have to declare it as a member variable.

  5. #5
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    you have declared the objects in the constructor, so they are available only there.

    you should do something like that

    Qt Code:
    1. class myWidget:public QWidget{
    2. Q_OBJECT
    3. public:
    4. myWidget(QWidget *parent=0);
    5. private slots:
    6. void processInput();
    7. private:
    8. QLabel display;
    9. QLineEdit lineEdit;
    10. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    now i got new error, undefined reference to vtable for myWidget

    Qt Code:
    1. #include<QApplication>
    2. #include<QLabel>
    3. #include<QLineEdit>
    4. #include<QString>
    5.  
    6. class myWidget:public QWidget{
    7. Q_OBJECT
    8.  
    9. public:
    10. myWidget(QWidget *parent=0);
    11. private slots:
    12. void processInput();
    13. private:
    14. QLabel *display;
    15. QLineEdit *lineEdit;
    16. };
    17.  
    18. myWidget::myWidget(QWidget *parent)
    19. :QWidget(parent)
    20. {
    21. display=new QLabel;
    22. lineEdit=new QLineEdit;
    23.  
    24. connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(processInput()));
    25.  
    26. }
    27.  
    28. void myWidget::processInput(){
    29. QString text=lineEdit->text();
    30. display->setText(text);
    31. }
    32.  
    33. int main(int argc, char **argv){
    34. QApplication app(argc, argv);
    35. myWidget window;
    36.  
    37. window.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: custom slot/signal

    Try adding line:
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    somewhere in your main.cpp.

    You will have to include the moc file by hand in case you don't have separate class header files.
    J-P Nurmi

  8. #8
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    so i just add the include line to main.cpp right? i did that but the compiler said that there is no such file or directory

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    Quote Originally Posted by awnjoor
    i did that but the compiler said that there is no such file or directory
    How do you compile your application?

  10. #10
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    kay, i deleted everything in the folder, except main.cpp. Then i did qmake -project then qmake and then make. Now the error is in the moc file

    here is my moc file

    Qt Code:
    1. /****************************************************************************
    2. ** Meta object code from reading C++ file 'main.cpp'
    3. **
    4. ** Created: Mon Jul 17 06:56:41 2006
    5. ** by: The Qt Meta Object Compiler version 59 (Qt 4.1.4)
    6. **
    7. ** WARNING! All changes made in this file will be lost!
    8. *****************************************************************************/
    9.  
    10. #if !defined(Q_MOC_OUTPUT_REVISION)
    11. #error "The header file 'main.cpp' doesn't include <QObject>."
    12. #elif Q_MOC_OUTPUT_REVISION != 59
    13. #error "This file was generated using the moc from 4.1.4. It"
    14. #error "cannot be used with the include files from this version of Qt."
    15. #error "(The moc has changed too much.)"
    16. #endif
    17.  
    18. static const uint qt_meta_data_myWidget[] = {
    19.  
    20. // content:
    21. 1, // revision
    22. 0, // classname
    23. 0, 0, // classinfo
    24. 1, 10, // methods
    25. 0, 0, // properties
    26. 0, 0, // enums/sets
    27.  
    28. // slots: signature, parameters, type, tag, flags
    29. 10, 9, 9, 9, 0x08,
    30.  
    31. 0 // eod
    32. };
    33.  
    34. static const char qt_meta_stringdata_myWidget[] = {
    35. "myWidget\0\0processInput()\0"
    36. };
    37.  
    38. const QMetaObject myWidget::staticMetaObject = {
    39. { &QWidget::staticMetaObject, qt_meta_stringdata_myWidget,
    40. qt_meta_data_myWidget, 0 }
    41. };
    42.  
    43. const QMetaObject *myWidget::metaObject() const
    44. {
    45. return &staticMetaObject;
    46. }
    47.  
    48. void *myWidget::qt_metacast(const char *_clname)
    49. {
    50. if (!_clname) return 0;
    51. if (!strcmp(_clname, qt_meta_stringdata_myWidget))
    52. return static_cast<void*>(const_cast<myWidget*>(this));
    53. return QWidget::qt_metacast(_clname);
    54. }
    55.  
    56. int myWidget::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    57. {
    58. _id = QWidget::qt_metacall(_c, _id, _a);
    59. if (_id < 0)
    60. return _id;
    61. if (_c == QMetaObject::InvokeMetaMethod) {
    62. switch (_id) {
    63. case 0: processInput(); break;
    64. }
    65. _id -= 1;
    66. }
    67. return _id;
    68. }
    To copy to clipboard, switch view to plain text mode 

    i dont know how to output the errors to a file so here is the image

    http://us.share.geocities.com/dymlg/1.bmp

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    Quote Originally Posted by awnjoor
    kay, i deleted everything in the folder, except main.cpp. Then i did qmake -project then qmake and then make.
    Then it should work, unless your project is located in "qt" directory (in such case you must add "TARGET += something" to your .pro file).

    Quote Originally Posted by awnjoor
    Now the error is in the moc file
    All moc files correct. The error must be in some other place.

    Quote Originally Posted by awnjoor
    i dont know how to output the errors to a file so here is the image
    Just cut & paste them. If you are using windows console, you can find cut & paste tools after you left-click the icon in the top-left corner of the window.

    Quote Originally Posted by awnjoor
    "This page is not available."

    You can post images here --- just click "Manage Attachements" button below the editor, but please don't post BMPs (even MS Paint can save images as PNGs or GIFs).

  12. #12
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    >>Then it should work, unless your project is located in "qt" directory (in such case you must add "TARGET += something" to your .pro file)

    no, my project is not located in qt directory.

    here is the error

    In file included from main.cpp:6:
    release/main.moc:38: error: `myWidget' has not been declared
    release/main.moc:43: error: `myWidget' has not been declared
    release/main.moc:44: error: non-member function `const QMetaObject* metaObject()
    ' cannot have `const' method qualifier
    release/main.moc:48: error: `myWidget' has not been declared
    release/main.moc: In function `void* qt_metacast(const char*)':
    release/main.moc:52: error: `myWidget' has not been declared
    release/main.moc:52: error: expected `>' before '*' token
    release/main.moc:52: error: expected `(' before '*' token
    release/main.moc:52: error: expected primary-expression before '>' token
    release/main.moc:52: error: invalid use of `this' in non-member function
    release/main.moc:52: error: expected `)' before ';' token
    release/main.moc:53: error: cannot call member function `virtual void* QWidget::
    qt_metacast(const char*)' without object
    release/main.moc: At global scope:
    release/main.moc:56: error: `myWidget' has not been declared
    release/main.moc: In function `int qt_metacall(QMetaObject::Call, int, void**)':

    release/main.moc:58: error: cannot call member function `virtual int QWidget::qt
    _metacall(QMetaObject::Call, int, void**)' without object
    release/main.moc:63: error: `processInput' undeclared (first use this function)
    release/main.moc:63: error: (Each undeclared identifier is reported only once fo
    r each function it appears in.)
    mingw32-make[1]: *** [release\main.o] Error 1
    mingw32-make[1]: Leaving directory `C:/Thesis/temp'
    mingw32-make: *** [release-all] Error 2

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    Quote Originally Posted by awnjoor
    In file included from main.cpp:6:
    Move #include "main.moc" directive to the end of file.

  14. #14
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    ::bows down::

    thank you all. now jacek would you care to explain why i have to move the moc directive down

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slot/signal

    Quote Originally Posted by awnjoor
    why i have to move the moc directive down
    Because main.moc contains implementation of myWidget methods (which were added by Q_OBJECT macro) and in C++ you have to place method implementation after corresponding class definition.

  16. #16
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    sigh, im trying to do the custom slot/signal again but it is not working. here is the code

    Qt Code:
    1. #include<QTableWidgetItem>
    2. #include<QApplication>
    3. #include<QWidget>
    4. #include<QPushButton>
    5. #include<QLabel>
    6. #include<QFont>
    7. #include<QLineEdit>
    8. #include<QTableWidget>
    9. #include<QVBoxLayout>
    10. #include<QHBoxLayout>
    11. #include<QStringList>
    12. #include<QtSql/QSqlDatabase>
    13. #include<QtSql/QSqlQuery>
    14. #include<QString>
    15. #include<QObject>
    16.  
    17. class myWidget:public QWidget{
    18. Q_OBJECT
    19.  
    20. public:
    21. myWidget(QWidget *parent=0);
    22.  
    23. private slots:
    24. void processInput();
    25.  
    26. private:
    27. QLabel *displayItem;
    28. QLineEdit *itemnumber;
    29. };
    30.  
    31. myWidget::myWidget(QWidget *parent)
    32. :QWidget(parent)
    33. {
    34. setWindowState(Qt::WindowFullScreen);
    35.  
    36. QLabel *title = new QLabel;
    37. title->setFont(QFont("Verdana", 30, 0));
    38. title->setText("title");
    39. title->setAlignment(Qt::AlignHCenter);
    40.  
    41. QLabel *item=new QLabel;
    42. item->setFont(QFont("verdana", 15, QFont::Bold));
    43. item->setText("Item");
    44.  
    45. displayItem=new QLabel;
    46. //displayItem->setFont(QFont("verdana", 15, QFont::Bold));
    47. //displayItem->setText("asdf");
    48.  
    49. itemnumber=new QLineEdit;
    50. itemnumber->setMaxLength(10);
    51.  
    52. QLabel *quantity=new QLabel;
    53. quantity->setFont(QFont("verdana", 15, QFont::Bold));
    54. quantity->setText("Quantity");
    55.  
    56. QLineEdit *quantitynumber=new QLineEdit;
    57. quantitynumber->setMaxLength(10);
    58.  
    59. QStringList columnLabels;
    60. columnLabels << "Item" << "#" << "TOTAL";
    61.  
    62. QTableWidget *tableWidget=new QTableWidget;
    63. tableWidget->setRowCount(1);
    64. tableWidget->setColumnCount(3);
    65. tableWidget->setHorizontalHeaderLabels(columnLabels);
    66. tableWidget->setColumnWidth(0, 250);
    67. tableWidget->setColumnWidth(1, 30);
    68. tableWidget->setColumnWidth(2, 100);
    69.  
    70. QHBoxLayout *hLeft1=new QHBoxLayout;
    71. hLeft1->addWidget(item);
    72. hLeft1->addWidget(itemnumber);
    73.  
    74. QHBoxLayout *hLeft2=new QHBoxLayout;
    75. hLeft1->addWidget(quantity);
    76. hLeft1->addWidget(quantitynumber);
    77.  
    78. vLeft->addLayout(hLeft1);
    79. vLeft->addLayout(hLeft2);
    80.  
    81. hLeft->addLayout(vLeft);
    82. hLeft->addStretch(0);
    83.  
    84. QVBoxLayout *mainLeft=new QVBoxLayout;
    85. mainLeft->addWidget(displayItem);
    86. mainLeft->addStretch(0);
    87. mainLeft->addLayout(hLeft);
    88. mainLeft->addStretch(0);
    89.  
    90. QHBoxLayout *mainBottom=new QHBoxLayout;
    91. mainBottom->addLayout(mainLeft);
    92. mainBottom->addWidget(tableWidget);
    93.  
    94. QVBoxLayout *mainLayout=new QVBoxLayout(this);
    95. mainLayout->addWidget(title);
    96. mainLayout->addSpacing(80);
    97. mainLayout->addLayout(mainBottom);
    98. setLayout(mainLayout);
    99.  
    100. itemnumber->setFocus();
    101. //itemnumber->setText("asdf");
    102.  
    103. connect(itemnumber, SIGNAL(returnPressed()), this, SIGNAL(processInput()));
    104.  
    105. }
    106.  
    107. void myWidget::processInput(){
    108. //QString text=itemnumber->text();
    109. itemnumber->setText("asfd");
    110. }
    111.  
    112. int main(int argc, char *argv[]){
    113. QApplication app(argc, argv);
    114. myWidget window;
    115.  
    116. window.show();
    117. return app.exec();
    118. }
    119.  
    120. #include "client.moc"
    To copy to clipboard, switch view to plain text mode 

    everything compiles fine, it just doesnt do the processInput() when i press enter in itemnumber lineedit.

    EDIT:
    it seems that item number is not emitting the returnPressed() signal because i tried replacing it with

    Qt Code:
    1. connect(itemnumber, SIGNAL(returnPressed()), qApp, SIGNAL(quit()));
    To copy to clipboard, switch view to plain text mode 

    and when i pressed enter nothing happened.
    Last edited by awnjoor; 17th July 2006 at 06:07.

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: custom slot/signal

    The latter should be SLOT:
    connect(itemnumber, SIGNAL(returnPressed()), this, SLOT(processInput()));
    J-P Nurmi

  18. #18
    Join Date
    Jul 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slot/signal

    and i wonder how did i miss that one ..

    thank you

Similar Threads

  1. Custom plugin for a layout
    By cocheci in forum Qt Tools
    Replies: 2
    Last Post: 12th June 2006, 19:36
  2. Replies: 5
    Last Post: 16th May 2006, 21:38
  3. Custom Qt dll name
    By tony007 in forum Installation and Deployment
    Replies: 2
    Last Post: 10th March 2006, 16:29
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. Replies: 4
    Last Post: 2nd March 2006, 00:11

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.