Finally, I made that, using void QObject::installEventFilter ( QObject * filterObj ) but it is another problem. I am using QtCreator 2.0. See that:
#ifndef ENTER_H
#define ENTER_H
#include <QObject>
#include <QEvent>
#include <QKeyEvent>
#include <mainwindow.h>
{
Q_OBJECT
public:
enter();
protected:
};
#endif // ENTER_H
//###############################################
#include "enter.h"
enter::enter()
{
}
{
if (event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if(keyEvent->key()==Qt::Key_Return)
{
// PLACE 1
}
}
return QObject::eventFilter(obj, event
);
}
#ifndef ENTER_H
#define ENTER_H
#include <QObject>
#include <QEvent>
#include <QKeyEvent>
#include <mainwindow.h>
class enter : public QObject
{
Q_OBJECT
public:
enter();
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // ENTER_H
//###############################################
#include "enter.h"
enter::enter()
{
}
bool enter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if(keyEvent->key()==Qt::Key_Return)
{
// PLACE 1
}
}
return QObject::eventFilter(obj, event);
}
To copy to clipboard, switch view to plain text mode
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <enter.h>
namespace Ui
{
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void licz();
private:
Ui::MainWindow *ui;
private slots:
void on_textBrowser_textChanged();
};
#endif // MAINWINDOW_H
//###############################################
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textBrowser->installEventFilter(new enter());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::licz() //PLACE 2
{
ui->label->setText(ui->label->text()+"0");
}
void MainWindow::on_textBrowser_textChanged()
{
licz();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <enter.h>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void licz();
private:
Ui::MainWindow *ui;
private slots:
void on_textBrowser_textChanged();
};
#endif // MAINWINDOW_H
//###############################################
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textBrowser->installEventFilter(new enter());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::licz() //PLACE 2
{
ui->label->setText(ui->label->text()+"0");
}
void MainWindow::on_textBrowser_textChanged()
{
licz();
}
To copy to clipboard, switch view to plain text mode
How to in PLACE 1 call method licz() from PLACE 2 ? I think it should be some slot,signal or emit but I do not have idea how to do it. Can someone help?
Bookmarks