View Full Version : QTextBrowser and ENTER key
szarek
28th June 2010, 19:53
I have a TextBrowser in my program. How to do that it will be respond to press ENTER key identical as is implement signal returnPressed() in lineEdit?
Lykurg
28th June 2010, 22:15
Subcalss QTextBrowser and reimp the key press event. Check for enter and emit a proper signal. (This will works but I fear that it won't be intuitive for your users.)
Vit Stepanek
29th June 2010, 10:06
Or you can use event filter - see
void QObject::installEventFilter ( QObject * filterObj )
at
http://doc.trolltech.com/latest/qobject.html#installEventFilter
szarek
6th July 2010, 19:56
I can not understand how to use installEventFilter, can somebody help me? Also I find class QShortcut and I wrote this:
#include "kczatownik.h"
#include "ui_kczatownik.h"
KCzatownik::KCzatownik(QWidget *parent) : QMainWindow(parent) , ui(new Ui::KCzatownik)
{
ui->setupUi(this);
skrot = new QShortcut(QKeySequence("Enter"),this);
connect(skrot,SIGNAL(activated()),this,SLOT(on_tex tBrowser_textChanged()));
}
KCzatownik::~KCzatownik()
{
delete ui;
}
void KCzatownik::on_textBrowser_textChanged()
{
QScrollBar *bar = ui->textBrowser->verticalScrollBar();
bar->setValue(bar->maximum());
}
but also it is not working.
szarek
9th July 2010, 22:53
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>
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);
}
#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();
}
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?
wysota
10th July 2010, 20:47
Move the event filter from the "enter" object to the "MainWindow" object if all the former does is to intercept the event. Then you can call the method directly.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.