Results 1 to 4 of 4

Thread: Slots in LineEdit

  1. #1
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Slots in LineEdit

    Hi,

    I'm trying to 'connect' the value of a QSlider to a LineEdit's setText, but it doesn't work. On compiling there are no errors, but the value doesn't get into LineEdit...
    Here is the code i've used:
    #include <QApplication>
    #include <QSlider>
    #include <QLineEdit>
    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);
    QSlider *sldValue = new QSlider(Qt::Horizontal);
    QLineEdit *leValue = new QLineEdit;
    QObject::connect(sldValue, SIGNAL(valueChanged(int)), leValue, SLOT(setText("Test")));
    // i just use the text 'Test' for now; this doesn't work also
    // the widgets are then added to a layout
    return app.exec();
    }
    With this example i just want to put the text 'Test' into LineEdit when the slider has changed of value. Can't be that hard?

    I'm keeping the example very short, because i want this time to get QT right (i've abanded QT many times because i find it very difficult to learn)

    Thanx in advance,
    Misko

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slots in LineEdit

    Slots don't work that way.
    The signature(the parameters) of the signal must match the signature of the slot( but a slot can have fewer parameters than the signal, since it can ignore the extra ones).

    You will eventually need to create a class for the main window and add a slot in your class( the following is the implementation of the slot):
    Qt Code:
    1. void SomeClass::updateTextEdit(int value)
    2. {
    3. QString str = QString("%1").arg(value);
    4. leValue.setText(str);
    5. }
    To copy to clipboard, switch view to plain text mode 
    The line edit and the slider should be members in your class.
    Then, you can connect like:
    Qt Code:
    1. connect(sldValue, SIGNAL(valueChanged(int)), this, SLOT(updateTextEdit(int)));
    To copy to clipboard, switch view to plain text mode 
    This is pretty similar of what you want to do. It will set the current value of the slider in the line edit.

    Regards

  3. #3
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slots in LineEdit

    Hi Marcel,

    Thx for your advice!
    It took me some time to implement the class, because i'm still totally newbie with creating classes, but now it works!!!
    1 question: in your reply you've put leValue.setText(str);
    It gave me the error: request for member 'setText' in leValue, which is of non-class type 'QLineEdit'
    Instead i've put leValue->setText(str); and it works, but don't ask me why?

    Regards,
    Misko

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slots in LineEdit

    It works because leValue is a pointer.
    I probably typed "." by mistake.

    As a rule, remember that whenever you have a pointer to an object you can access its members with "->", as opposed to when you have the object allocated on the stack and you access them with "."( member selection by reference).

    Regards

Similar Threads

  1. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 00:24
  2. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28
  3. Order of signals and slots
    By Morea in forum Newbie
    Replies: 1
    Last Post: 26th February 2006, 22:09
  4. subclassing or redrawing a lineEdit
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 18:26
  5. Missing slots
    By Mariane in forum Newbie
    Replies: 1
    Last Post: 5th February 2006, 01:50

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.