Results 1 to 8 of 8

Thread: Wierd behaviour in a slot routine

  1. #1
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Wierd behaviour in a slot routine

    Hi,

    Firslty I'm very new to QT (as well as GUI programming) but quickly falling in love with it

    Having said that however, I have hit my first wall which is probably a lack of understanding something. I am useing eclipse with QT integration (V4.6).

    As a test I have crated a form using the designer with a simple text entry widget and a button

    I then created a slot for the buttons clicked signal

    In the routine processing the signal I read the text from the entry and set the text for the button label to reflect that.

    Copiled and ran fine. I can enter text in the entry box and when I click on the button it's label changes to what I entered.

    However the text in the entry box does not get cleared so I issued the clear method on it following the set text method on the button.

    Now when I click on th button the text in the entry box gets cleared which is fine, but so does the label of the button which I don't get.

    Below is the implementation of the slot function:

    Qt Code:
    1. void qt_test::on_enterButton_clicked()
    2. {
    3. QString buttonName = ui.lineInput->displayText();
    4. ui.enterButton->setText(buttonName);
    5. ui.lineInput->clear();
    6. }
    To copy to clipboard, switch view to plain text mode 

    ui.lineIput is of class QLineEdit
    ui.enterButton is of class QPushButton

    inially the push button has the text "Push" on it then when I push it it get cleared irrespective of what has been entered in the lineInput

    The itneresting thing is that if I do this:

    Qt Code:
    1. void qt_test::on_enterButton_clicked()
    2. {
    3. QString buttonName = ui.lineInput->displayText();
    4. buttonName += "ABC";
    5. ui.enterButton->setText(buttonName);
    6. ui.lineInput->clear();
    7. }
    To copy to clipboard, switch view to plain text mode 

    The push button text gets set to "ABC" again irrespective of what is entered in lineInput.

    Any one able to tell me why this is so?

    Thanks!

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Wierd behaviour in a slot routine

    This works here without any problem:
    Qt Code:
    1. void MainWindow::on_enterButton_clicked()
    2. {
    3. QString buttonName = ui->lineEdit->displayText();
    4. ui->pushButton->setText(buttonName);
    5. ui->lineEdit->clear();
    6. }
    To copy to clipboard, switch view to plain text mode 
    I made the form with QtCreator 1.3.0.

  3. #3
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Wierd behaviour in a slot routine

    Ok well that stumps me then

    I guess one other bit of info is that I have been doing this on Windows 7, although can't think of any dependacy on the OS off the top of my head that would cause such a thing.

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Wierd behaviour in a slot routine

    Ok, Let's change
    Qt Code:
    1. QString buttonName = ui.lineInput->displayText();
    To copy to clipboard, switch view to plain text mode 
    into this
    Qt Code:
    1. QString buttonName ( ui.lineInput->displayText());
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Wierd behaviour in a slot routine

    Ok tried that, made no differance.

    I also just updated to Qt 4.6.1 (changed all the paths appropriately too) that also made no differance

  6. #6
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Wierd behaviour in a slot routine

    For completeness I'm posting all code from this project, maybe I've stuffed something elsewhere

    main.cpp:

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


    ui_qt_test.h:

    Qt Code:
    1. #ifndef UI_QT_TEST_H
    2. #define UI_QT_TEST_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QHeaderView>
    9. #include <QtGui/QLineEdit>
    10. #include <QtGui/QPushButton>
    11. #include <QtGui/QTextBrowser>
    12. #include <QtGui/QWidget>
    13.  
    14. QT_BEGIN_NAMESPACE
    15.  
    16. class Ui_qt_testClass
    17. {
    18. public:
    19. QTextBrowser *textOut;
    20. QLineEdit *lineInput;
    21. QPushButton *enterButton;
    22. QPushButton *quitButton;
    23.  
    24. void setupUi(QWidget *qt_testClass)
    25. {
    26. if (qt_testClass->objectName().isEmpty())
    27. qt_testClass->setObjectName(QString::fromUtf8("qt_testClass"));
    28. qt_testClass->resize(661, 407);
    29. textOut = new QTextBrowser(qt_testClass);
    30. textOut->setObjectName(QString::fromUtf8("textOut"));
    31. textOut->setGeometry(QRect(80, 60, 511, 192));
    32. lineInput = new QLineEdit(qt_testClass);
    33. lineInput->setObjectName(QString::fromUtf8("lineInput"));
    34. lineInput->setGeometry(QRect(80, 270, 291, 20));
    35. enterButton = new QPushButton(qt_testClass);
    36. enterButton->setObjectName(QString::fromUtf8("enterButton"));
    37. enterButton->setGeometry(QRect(410, 270, 75, 23));
    38. quitButton = new QPushButton(qt_testClass);
    39. quitButton->setObjectName(QString::fromUtf8("quitButton"));
    40. quitButton->setGeometry(QRect(410, 360, 75, 23));
    41.  
    42. retranslateUi(qt_testClass);
    43. QObject::connect(quitButton, SIGNAL(clicked()), qt_testClass, SLOT(close()));
    44.  
    45. QMetaObject::connectSlotsByName(qt_testClass);
    46. } // setupUi
    47.  
    48. void retranslateUi(QWidget *qt_testClass)
    49. {
    50. qt_testClass->setWindowTitle(QApplication::translate("qt_testClass", "qt_test", 0, QApplication::UnicodeUTF8));
    51. enterButton->setText(QApplication::translate("qt_testClass", "Enter", 0, QApplication::UnicodeUTF8));
    52. quitButton->setText(QApplication::translate("qt_testClass", "Quit", 0, QApplication::UnicodeUTF8));
    53. } // retranslateUi
    54.  
    55. };
    56.  
    57. namespace Ui {
    58. class qt_testClass: public Ui_qt_testClass {};
    59. } // namespace Ui
    60.  
    61. QT_END_NAMESPACE
    62.  
    63. #endif // UI_QT_TEST_H
    To copy to clipboard, switch view to plain text mode 


    qt_test.h:

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


    qt_test.cpp:

    Qt Code:
    1. #include "qt_test.h"
    2. #include <QString>
    3.  
    4. qt_test::qt_test(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. ui.setupUi(this);
    8. connect(ui.enterButton, SIGNAL(clicked()), this, SLOT(on_enterButton_clicked()));
    9. }
    10.  
    11. qt_test::~qt_test()
    12. {
    13.  
    14. }
    15.  
    16. void qt_test::on_enterButton_clicked()
    17. {
    18. QString buttonName (ui.lineInput->displayText());
    19. buttonName += "ABC";
    20. ui.enterButton->setText(buttonName);
    21. ui.lineInput->clear();
    22.  
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Wierd behaviour in a slot routine

    Oh I've figured it out!!!!

    As I'm very new to this framework I got caught by the signal/slot connection method, I didn't realise that when I named the slot function on_enterButton_clicked() the framework would implicitly connect the the clicked signal for me. So I also issued an explicit connect statement in the constructor. As a result the slot function was being executed twice, and so the second time it exectued the text bas was already cleared and so the button label also got cleared.

    D'OH!!!!

    Anyway, thanks for the help, actually just knowing that it work for someone else made me dig in different directions, I was starting to loose my mind there a bit

  8. #8
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Wierd behaviour in a slot routine

    LOL!


    And nobody noticed it...

Similar Threads

  1. QXmlResultItems wierd behavior
    By ixSci in forum Qt Programming
    Replies: 0
    Last Post: 21st August 2009, 12:23
  2. Routing stdout to Plain Text Edit
    By Barry79 in forum Qt Programming
    Replies: 7
    Last Post: 2nd April 2009, 13:06
  3. Somewhat OT, line routing for diagramming tool
    By pherthyl in forum Qt Programming
    Replies: 7
    Last Post: 20th October 2008, 19:23
  4. pointers behaviour in qt and specially signals/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2008, 19:34
  5. Replies: 14
    Last Post: 19th March 2007, 08:48

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.