Results 1 to 5 of 5

Thread: Connection not properly done.... lineEdit

  1. #1
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Connection not properly done.... lineEdit

    Hi:

    I'm new in this forum... trying to test QT. My background comes from MS Visual Studio.

    So, I'm having some troubles figuring out this connection (signal, slot) thing... I'm more used to the onLoad, onClose, etc... of MS

    The problem is silly. Very silly indeed.

    I'm using designer. A simple main window with a slider and a spin connected.

    I have a lineEdit where I want to display whatever I want depending on the value of the slider... not just the value of the slider... maybe some text saying something...

    So, It seems that I have to create a new connection... which it is not working as I expected... well, it's not working at all... I guess that maybe somebody here could have a look at the code and in 2 seconds see the error... it would help me to understand how this system works. Overall it looks really nice, but I don't see 100% clear the Event handling...

    Thanks!!!
    Regards,
    Hipogrito

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



    GTMain.c
    Qt Code:
    1. #include "GTMain.h"
    2. #include "ui_GTerm.h"
    3.  
    4. GTMain::GTMain(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. // init the ui
    8. ui.setupUi(this);
    9. connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), ui.lineEdit, SLOT(updateText()));
    10.  
    11. }
    12.  
    13.  
    14. void GTMain::updateText()
    15. {
    16. ui.lineEdit->clear();
    17. ui.lineEdit->setText("Modifing...");
    18. ui.lineEdit->show();
    19.  
    20. // Whatever... I just want to clear the line and show the new text...
    21. // I don't care at the beginning what to show, but I just want to be able to add whatever here in this method
    22. }
    To copy to clipboard, switch view to plain text mode 


    Now the file coming from the Designer:
    ui_GTerm.h
    Qt Code:
    1. ********************************************************************************
    2. ** Form generated from reading ui file 'GTerm.ui'
    3. **
    4. ** Created: Mon Apr 2 15:01:26 2007
    5. ** by: Qt User Interface Compiler version 4.2.0
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_GTERM_H
    11. #define UI_GTERM_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QComboBox>
    18. #include <QtGui/QGroupBox>
    19. #include <QtGui/QLineEdit>
    20. #include <QtGui/QMainWindow>
    21. #include <QtGui/QMenu>
    22. #include <QtGui/QMenuBar>
    23. #include <QtGui/QSlider>
    24. #include <QtGui/QSpinBox>
    25. #include <QtGui/QStatusBar>
    26. #include <QtGui/QWidget>
    27.  
    28. class Ui_GTerm
    29. {
    30. public:
    31. QAction *actionAbout;
    32. QAction *actionQuit;
    33. QWidget *centralwidget;
    34. QGroupBox *groupBox;
    35. QSpinBox *spinBox;
    36. QComboBox *comboBox;
    37. QSlider *verticalSlider;
    38. QLineEdit *lineEdit;
    39. QMenuBar *menubar;
    40. QMenu *menuHelp;
    41. QMenu *menuFile;
    42. QStatusBar *statusbar;
    43.  
    44. void setupUi(QMainWindow *GTerm)
    45. {
    46. GTerm->setObjectName(QString::fromUtf8("GTerm"));
    47. actionAbout = new QAction(GTerm);
    48. actionAbout->setObjectName(QString::fromUtf8("actionAbout"));
    49. actionQuit = new QAction(GTerm);
    50. actionQuit->setObjectName(QString::fromUtf8("actionQuit"));
    51. centralwidget = new QWidget(GTerm);
    52. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    53. groupBox = new QGroupBox(centralwidget);
    54. groupBox->setObjectName(QString::fromUtf8("groupBox"));
    55. groupBox->setGeometry(QRect(30, 10, 131, 271));
    56. spinBox = new QSpinBox(groupBox);
    57. spinBox->setObjectName(QString::fromUtf8("spinBox"));
    58. spinBox->setGeometry(QRect(50, 90, 55, 29));
    59. comboBox = new QComboBox(groupBox);
    60. comboBox->setObjectName(QString::fromUtf8("comboBox"));
    61. comboBox->setGeometry(QRect(10, 30, 101, 26));
    62. verticalSlider = new QSlider(groupBox);
    63. verticalSlider->setObjectName(QString::fromUtf8("verticalSlider"));
    64. verticalSlider->setGeometry(QRect(20, 90, 16, 160));
    65. verticalSlider->setOrientation(Qt::Vertical);
    66. lineEdit = new QLineEdit(centralwidget);
    67. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    68. lineEdit->setGeometry(QRect(90, 400, 421, 29));
    69. GTerm->setCentralWidget(centralwidget);
    70. menubar = new QMenuBar(GTerm);
    71. menubar->setObjectName(QString::fromUtf8("menubar"));
    72. menubar->setGeometry(QRect(0, 0, 633, 25));
    73. menuHelp = new QMenu(menubar);
    74. menuHelp->setObjectName(QString::fromUtf8("menuHelp"));
    75. menuFile = new QMenu(menubar);
    76. menuFile->setObjectName(QString::fromUtf8("menuFile"));
    77. GTerm->setMenuBar(menubar);
    78. statusbar = new QStatusBar(GTerm);
    79. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    80. GTerm->setStatusBar(statusbar);
    81.  
    82. menubar->addAction(menuFile->menuAction());
    83. menubar->addAction(menuHelp->menuAction());
    84. menuHelp->addAction(actionAbout);
    85. menuFile->addAction(actionQuit);
    86.  
    87. retranslateUi(GTerm);
    88.  
    89. QSize size(633, 633);
    90. size = size.expandedTo(GTerm->minimumSizeHint());
    91. GTerm->resize(size);
    92.  
    93. QObject::connect(verticalSlider, SIGNAL(sliderMoved(int)), spinBox, SLOT(setValue(int)));
    94. QObject::connect(spinBox, SIGNAL(valueChanged(int)), verticalSlider, SLOT(setValue(int)));
    95. QObject::connect(verticalSlider, SIGNAL(sliderMoved(int)), lineEdit, SLOT(update()));
    96.  
    97. QMetaObject::connectSlotsByName(GTerm);
    98. } // setupUi
    99.  
    100. void retranslateUi(QMainWindow *GTerm)
    101. {
    102. GTerm->setWindowTitle(QApplication::translate("GTerm", "GTERM v003", 0, QApplication::UnicodeUTF8));
    103. actionAbout->setText(QApplication::translate("GTerm", "About", 0, QApplication::UnicodeUTF8));
    104. actionQuit->setText(QApplication::translate("GTerm", "Quit", 0, QApplication::UnicodeUTF8));
    105. groupBox->setTitle(QApplication::translate("GTerm", "Control 1", 0, QApplication::UnicodeUTF8));
    106. comboBox->clear();
    107. comboBox->addItem(QApplication::translate("GTerm", "Element01", 0, QApplication::UnicodeUTF8));
    108. comboBox->addItem(QApplication::translate("GTerm", "Element02", 0, QApplication::UnicodeUTF8));
    109. comboBox->addItem(QApplication::translate("GTerm", "Element03", 0, QApplication::UnicodeUTF8));
    110. comboBox->addItem(QApplication::translate("GTerm", "Element04", 0, QApplication::UnicodeUTF8));
    111. menuHelp->setTitle(QApplication::translate("GTerm", "Help", 0, QApplication::UnicodeUTF8));
    112. menuFile->setTitle(QApplication::translate("GTerm", "File", 0, QApplication::UnicodeUTF8));
    113. } // retranslateUi
    114.  
    115. };
    116.  
    117. namespace Ui {
    118. class GTerm: public Ui_GTerm {};
    119. } // namespace Ui
    120.  
    121. #endif // UI_GTERM_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 2nd April 2007 at 21:41. Reason: missing [code] tags

  2. #2
    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: Connection not properly done.... lineEdit

    Quote Originally Posted by hipogrito View Post
    connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), ui.lineEdit, SLOT(updateText()));
    It should be:
    Qt Code:
    1. connect( ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()) );
    To copy to clipboard, switch view to plain text mode 
    as the updateText() slot is defined in GTMain.

    You can achieve the same using automatic connections, simply by naming your slot "on_verticalSlider_sliderMoved(int)".

  3. #3
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connection not properly done.... lineEdit

    Hi:

    I found the bug:

    connect(ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()));

    Thus the signal is sent fromt the Slider to the Main Form.


    So, If I have understood this system correctly, I think that if I want to connect the Slider to the Line Edit directly and I want to do whatever I want, not the standard connections that appear in Designer, I have to create a Custom Widget in Designer which allows this communication. I guess that ideally that approach could be nicer, theoretically, but I guess that sending the connection directly to the Main Form is much faster and not very ugly.

    Any other ideas of how doing this?

    Thanks,

    Regards
    hipogrito

  4. #4
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connection not properly done.... lineEdit

    Hello:

    Quote Originally Posted by jacek View Post
    It should be:
    Qt Code:
    1. connect( ui.verticalSlider, SIGNAL(sliderMoved(int)), this, SLOT(updateText()) );
    To copy to clipboard, switch view to plain text mode 
    as the updateText() slot is defined in GTMain.

    You can achieve the same using automatic connections, simply by naming your slot "on_verticalSlider_sliderMoved(int)".
    Oooooooooops, I was writing at the same time as you

    Thank you very much for your solutions!

    Let's see if within some days/weeks I could jump from the newbie to the normal forum

    Regards
    hipogrito

  5. #5
    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: Connection not properly done.... lineEdit

    Quote Originally Posted by hipogrito View Post
    if I want to connect the Slider to the Line Edit directly and I want to do whatever I want, not the standard connections that appear in Designer, I have to create a Custom Widget in Designer which allows this communication.
    Yes, you can connect only to those slots that already exist.

    Quote Originally Posted by hipogrito View Post
    Any other ideas of how doing this?
    I would simply use QSpinBox instead of QLineEdit.

  6. The following user says thank you to jacek for this useful post:

    hipogrito (2nd April 2007)

Similar Threads

  1. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  2. Replies: 3
    Last Post: 22nd June 2006, 16:27
  3. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06
  4. Can I launch a dial-up connection in Windows?
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 9th February 2006, 12:32
  5. subclassing or redrawing a lineEdit
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 18:26

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.