About functions and lineEdit



  • Hey guys, I have some question that I need you guys to help me with

    questions :

    connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &))); // why did we put " this " keyword before slot ? what does it do beside letting the compiler know what object that hey work with.

    what does emit keyword does ? for example: emit findNext(text,cs);

    --

    Also I'm trying to let the lineedit except only intergers , I used this , is it right ? or there's another way ?

    QValidator *Valid=new QIntValidator();

    QLineEdit *edit =new QLineEdit;
    
    edit->setValidator(Valid);
    

    --

    also I'm trying to calculate what the user enters , for example I have a function that return x+y , and lets say I have a variable int named integer , and I wanted the user to enter an integer in the line text and stores the integer in " integer variable " , should I use textchanged here or what ?


  • Moderators

    Please wrap your code in '@' tags.

    The place where you use "this" is a function parameter (connect function), which specifies the receiving object for the signal. In your case, you are specifying "this", meaning the object from which you are making the connection. "this" is a C++ keyword.

    Emit keyword does not do anything, it is an empty macro. It's added in Qt just so that the code is easier to read and understand. You mark with it the point where your code emits the signal.

    Yes, int validator is ok.

    The last question: that depends on your use case. Use textChanged() if you want to respond to the change right when it happens ("live" calculation). If you want to make the computation after the user - for example - clicks a button, then place the code in a slot corresponding to that button.


  • Lifetime Qt Champion

    Hi,

    Please enclose your code in coding tags (one @ at the beginning and one at the end) It will make it readable.

    1. connect needs to know which object it is connecting. In you case a lineEdit and your custom object. You can also use the overload with only 3 arguments which implies the slots belong to the object calling connect.

    2. Yes it's one way to do it. You can also use a QSpinBox which seems more suited to your needs.

    3. Depends on how you want to react to the new value. Once it's completely entered ? While the use is still typing ? Then you have either textChanged or editingFinished

    Hope it helps


  • Moderators

    [quote author="SGaist" date="1394138001"]Hi[/quote]

    Hehe, ok, I can see we are working in parallel again :D

    I'll leave the "night shift" to you, I'm going to sleep now. Have fun :)


  • Lifetime Qt Champion

    [quote author="sierdzio" date="1394138154"][quote author="SGaist" date="1394138001"]Hi[/quote]

    Hehe, ok, I can see we are working in parallel again :D

    I'll leave the "night shift" to you, I'm going to sleep now. Have fun :)[/quote]

    That's what is called multicore mate ! ^^

    Good night ! :)



  • I'm sorry about the code guys ^^

    thanks for helping , I just wanted to know about question 3:

    lets say I have an integer x;

    and the user edited the line and I used editingfinshed to wait for the user to press enter , after pressing enter what function do I use so what the user entered " integer " get copied in the variable that I've used so I can do calculations ? and should I use connect here ?



  • I searched and found settext , but it takes strings ? i want to copy it to an int



  • @
    QObject::connect(edit,SIGNAL(editingFinished()),"what to use here ",SLOT());

    @


  • Lifetime Qt Champion

    Since you are using a QLineEdit you will need to get the string and then convert it to an integer.

    @
    QObject::connect(myLineEdit,SIGNAL(editingFinished()), this,SLOT(mySlot()));

    void MyMainWindow::mySlot()
    {
    QString numberText = myLineEdit->text();
    int number = numberText.toString()
    //etc...
    }
    @



  • Thanks buddy , I tried something I hope it will work , the problem is now I need to setlayout , but when I call that function it says :

    @
    error: C3861: 'setlayout': identifier not found
    @

    and there's only this one I didn't setlayout before and it's a new project

    these are the headers I'm using until now:

    #include <QApplication>
    #include<QLabel>
    #include<QHBoxLayout>
    #include<QLineEdit>
    #include<QIntValidator>
    #include <QWidget>


  • Lifetime Qt Champion

    You're welcome

    You wrote it wrong, it's setLayout



  • Same error man :( , even when I write the first two or three letters the function doesn't show , other functions shows like setlocal


  • Lifetime Qt Champion

    Can you show the code where this setLayout can be found ?



  • @
    #include <QApplication>
    #include<QLabel>
    #include<QHBoxLayout>
    #include<QLineEdit>
    #include<QIntValidator>
    #include <QWidget>

    int sum(int x,int y){
    return x+y;
    }

    int main(int argc, char *argv[])
    {
    int x;
    int y;
    QApplication a(argc, argv);

    QLabel *check=new QLabel(QObject::tr("Add numbers:"));
    
    QHBoxLayout *left=new QHBoxLayout;
    
    left->addWidget(check);
     QValidator *Valid=new QIntValidator();
    
    QLineEdit *edit =new QLineEdit;
    edit->setMaximumWidth(5);
    edit->setFixedWidth(5);
    check->setBuddy(edit);
    edit->setValidator(Valid);
    QObject::connect(edit,SIGNAL(editingFinished()),edit,SLOT(setText(x)));
    QLineEdit *edit1=new QLineEdit;
    edit1->setMaximumWidth(5);
    edit1->setFixedWidth(5);
    check->setBuddy(edit1);
    edit1->setValidator(Valid);
    QObject::connect(edit1,SIGNAL(editingFinished()),edit1,SLOT(setText(y)));
            left->addWidget(edit);
    QHBoxLayout *Mainlayout=new QHBoxLayout;
    Mainlayout->addLayout(left);
    

    setLayout(Mainlayout);

    sum(x,y);

    return a.exec&#40;&#41;;
    

    }

    @


  • Lifetime Qt Champion

    That won't do what you want at all.

    You are calling setLayout in main and not on a widget

    Also your connection statements are wrong.

    Please, have a look at some of Qt's example to see how things work



  • Well , that's the only way I would learn is by making mistakes :D , that's why I wanted to do it my way before trying what you guys have told me.

    by the way why can't I call it in main ? should I include something first ?



  • and could you please tell me where should I call it ?


  • Lifetime Qt Champion

    setLayout is a function of QWidget, so you need a QWidget object.



  • what if I made a class and inherited qwidget ? or there's a better way ?



  • Oh , so I could just create a pointer to that object and use that pointer to set the layout ? and if I wanted to make bigger programs I should just inherit it on my classes ?


  • Lifetime Qt Champion

    Subclass QWidget, it's the correct way to go here.

    You really should take a look at Qt's demos and examples, that will give you a good ground to build on



  • I really looked at some examples about two weeks ago " I don't have much time " since I've been busy with my midterms exams :(

    but I'll finish this one , hopefully it will work then I'll look at some examples tomorrow.



  • about converting from string to int , should always do this ? is there a way to just take an integer or any type i want from the user ?



  • I think I'll create a new class to do these things , specially that this won't work because there's no reference. is it ok if I posted on this thread tomorrow ? or it's against the rules to bump a thread with +12 hours without posting ?

    thanks man , I really appreciate your help.


  • Lifetime Qt Champion

    It's better not to bump 24 hours is the common time interval.

    If you want integers directly, use a QSpinBox, it will simplify your program


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.