Results 1 to 6 of 6

Thread: QTextBrowser and ENTER key

  1. #1
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTextBrowser and ENTER key

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextBrowser and ENTER key

    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.)

  3. #3
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and ENTER key

    Or you can use event filter - see
    void QObject::installEventFilter ( QObject * filterObj )
    at
    http://doc.trolltech.com/latest/qobj...allEventFilter

  4. #4
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and ENTER key

    I can not understand how to use installEventFilter, can somebody help me? Also I find class QShortcut and I wrote this:
    Qt Code:
    1. #include "kczatownik.h"
    2. #include "ui_kczatownik.h"
    3.  
    4. KCzatownik::KCzatownik(QWidget *parent) : QMainWindow(parent) , ui(new Ui::KCzatownik)
    5. {
    6. ui->setupUi(this);
    7. skrot = new QShortcut(QKeySequence("Enter"),this);
    8. connect(skrot,SIGNAL(activated()),this,SLOT(on_textBrowser_textChanged()));
    9. }
    10.  
    11. KCzatownik::~KCzatownik()
    12. {
    13. delete ui;
    14. }
    15.  
    16.  
    17. void KCzatownik::on_textBrowser_textChanged()
    18. {
    19. QScrollBar *bar = ui->textBrowser->verticalScrollBar();
    20. bar->setValue(bar->maximum());
    21. }
    To copy to clipboard, switch view to plain text mode 
    but also it is not working.

  5. #5
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and ENTER key

    Finally, I made that, using void QObject::installEventFilter ( QObject * filterObj ) but it is another problem. I am using QtCreator 2.0. See that:
    Qt Code:
    1. #ifndef ENTER_H
    2. #define ENTER_H
    3. #include <QObject>
    4. #include <QEvent>
    5. #include <QKeyEvent>
    6. #include <mainwindow.h>
    7. class enter : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. enter();
    12. protected:
    13. bool eventFilter(QObject *obj, QEvent *event);
    14. };
    15.  
    16. #endif // ENTER_H
    17. //###############################################
    18. #include "enter.h"
    19.  
    20. enter::enter()
    21. {
    22. }
    23.  
    24. bool enter::eventFilter(QObject *obj, QEvent *event)
    25. {
    26. if (event->type() == QEvent::KeyPress)
    27. {
    28.  
    29. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    30. if(keyEvent->key()==Qt::Key_Return)
    31. {
    32. // PLACE 1
    33. }
    34. }
    35. return QObject::eventFilter(obj, event);
    36. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <enter.h>
    6.  
    7. namespace Ui
    8. {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. void licz();
    19. private:
    20. Ui::MainWindow *ui;
    21. private slots:
    22. void on_textBrowser_textChanged();
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    26. //###############################################
    27. #include "mainwindow.h"
    28. #include "ui_mainwindow.h"
    29.  
    30. MainWindow::MainWindow(QWidget *parent) :
    31. QMainWindow(parent),
    32. ui(new Ui::MainWindow)
    33. {
    34. ui->setupUi(this);
    35. ui->textBrowser->installEventFilter(new enter());
    36. }
    37.  
    38. MainWindow::~MainWindow()
    39. {
    40. delete ui;
    41. }
    42.  
    43. void MainWindow::licz() //PLACE 2
    44. {
    45. ui->label->setText(ui->label->text()+"0");
    46. }
    47.  
    48. void MainWindow::on_textBrowser_textChanged()
    49. {
    50. licz();
    51. }
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextBrowser and ENTER key

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to disable enter
    By phillip_Qt in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2012, 07:52
  2. Replies: 1
    Last Post: 14th September 2008, 23:05
  3. Widget enter event
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 20th April 2008, 22:40
  4. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 23rd October 2006, 23:34
  5. QEvent::Enter
    By incapacitant in forum Newbie
    Replies: 6
    Last Post: 22nd March 2006, 08:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.