Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there in Qt forms onChange event?

Is there in Qt something like Form.onChange in Delphi?

I found some changeEvent method but when I wrote connect connect(this, SIGNAL(this->changeEvent),this, SLOT(checkIfSomethingChanged()));

and tried to check it like that

void importdb_module::checkIfSomethingChanged(){

    QMessageBox::information(0, "", "Test");
}

I realized that it not works.

I want to check some condition everytime when something changed in my form, how to do that?

like image 714
DanilGholtsman Avatar asked Nov 06 '13 10:11

DanilGholtsman


2 Answers

The changeEvent slot is a virtual, protected function found in QWidget. Therefore, if you inherit from QWidget or any QWidget derived class, you'll be able to override that function. For example: -

class MyForm : public QWidget
{
    protected slots:
        virtual void changeEvent(QEvent * event);
}


void MyForm::changeEvent(QEvent* event)
{
    // Do something with the event    
}

If you wanted to know outside of the event that the form has been changed, you can add a signal to the form and emit it from the changeEvent to pass the event on: -

class MyForm : public QWidget
{
   signals:
        void FormChanged(QEvent* event);

    protected slots:
        virtual void changeEvent(QEvent * event);
}


void MyForm::changeEvent(QEvent* event)
{
    emit FormChanged(event);
}

Now connect another class to the new signal, using Qt 5 connect syntax: -

connect(myFormObject, &MyForm::FormChanged, someclassObject, &SomeClass::HandleFormChanged);
like image 79
TheDarkKnight Avatar answered Sep 28 '22 01:09

TheDarkKnight


This cannot work, because you mixed up two concepts: events and signal/slots. To make it work, you need to override your class' changeEvent() virtual function. Something like this:

void MyWidget::changeEvent(QEvent *event)
{
    QMessageBox::information(0, "", "Test");
}
like image 30
vahancho Avatar answered Sep 28 '22 03:09

vahancho