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?
Printable View
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?
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.)
Or you can use event filter - see
void QObject::installEventFilter ( QObject * filterObj )
at
http://doc.trolltech.com/latest/qobj...allEventFilter
I can not understand how to use installEventFilter, can somebody help me? Also I find class QShortcut and I wrote this:
but also it is not working.Code:
#include "kczatownik.h" #include "ui_kczatownik.h" { ui->setupUi(this); connect(skrot,SIGNAL(activated()),this,SLOT(on_textBrowser_textChanged())); } KCzatownik::~KCzatownik() { delete ui; } void KCzatownik::on_textBrowser_textChanged() { bar->setValue(bar->maximum()); }
Finally, I made that, using void QObject::installEventFilter ( QObject * filterObj ) but it is another problem. I am using QtCreator 2.0. See that:
Code:
#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(keyEvent->key()==Qt::Key_Return) { // PLACE 1 } } }
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?Code:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <enter.h> namespace Ui { class MainWindow; } { Q_OBJECT public: ~MainWindow(); void licz(); private: Ui::MainWindow *ui; private slots: void on_textBrowser_textChanged(); }; #endif // MAINWINDOW_H //############################################### #include "mainwindow.h" #include "ui_mainwindow.h" 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(); }
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.