Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qTextEdit focusOutEvent
Forum Updated to NodeBB v4.3 + New Features

qTextEdit focusOutEvent

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 367 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EricR
    wrote on last edited by
    #1

    I'm using Qt6 and Qt Creator. My QWidget window contains a QTextEdit widget named 'myTextEdit'. I want to be able to execute some application code when 'myTextEdit' looses focus. I see that QWidget class has a protected function 'focusOutEvent' but I don't know how to implement code that uses it. I'm requesting help with the code.

    Not sure how to embed source code in this post, but here it is:

    //
    example.h
    /
    /

    #ifndef example_H
    #define example_H

    #include <QWidget>

    QT_BEGIN_NAMESPACE
    namespace Ui {
    class example;
    }
    QT_END_NAMESPACE

    class example : public QWidget
    {
    Q_OBJECT

    public:
    example(QWidget *parent = nullptr);
    ~example();

    private slots:

    protected:

    private:
    Ui::example *ui;
    };
    #endif // example_H

    //
    example.cpp
    /
    /

    #include "example.h"
    #include "./ui_example.h" /*** contains a QTextEdit widget ***/

    example::example(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::example)
    {
    ui->setupUi(this);
    }

    example::~example()
    {
    delete ui;
    }

    /*** Need to add code to handle 'myTextEdit' loosing focus via 'focusOutEvent' perhaps ***/

    //
    main.cpp
    /
    /

    #include "example.h"

    #include <QApplication>
    #include <QLocale>
    #include <QTranslator>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "example_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            a.installTranslator(&translator);
            break;
        }
    }
    example w;
    w.show();
    return a.exec();
    

    }

    M 1 Reply Last reply
    0
    • E EricR

      I'm using Qt6 and Qt Creator. My QWidget window contains a QTextEdit widget named 'myTextEdit'. I want to be able to execute some application code when 'myTextEdit' looses focus. I see that QWidget class has a protected function 'focusOutEvent' but I don't know how to implement code that uses it. I'm requesting help with the code.

      Not sure how to embed source code in this post, but here it is:

      //
      example.h
      /
      /

      #ifndef example_H
      #define example_H

      #include <QWidget>

      QT_BEGIN_NAMESPACE
      namespace Ui {
      class example;
      }
      QT_END_NAMESPACE

      class example : public QWidget
      {
      Q_OBJECT

      public:
      example(QWidget *parent = nullptr);
      ~example();

      private slots:

      protected:

      private:
      Ui::example *ui;
      };
      #endif // example_H

      //
      example.cpp
      /
      /

      #include "example.h"
      #include "./ui_example.h" /*** contains a QTextEdit widget ***/

      example::example(QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::example)
      {
      ui->setupUi(this);
      }

      example::~example()
      {
      delete ui;
      }

      /*** Need to add code to handle 'myTextEdit' loosing focus via 'focusOutEvent' perhaps ***/

      //
      main.cpp
      /
      /

      #include "example.h"

      #include <QApplication>
      #include <QLocale>
      #include <QTranslator>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);

      QTranslator translator;
      const QStringList uiLanguages = QLocale::system().uiLanguages();
      for (const QString &locale : uiLanguages) {
          const QString baseName = "example_" + QLocale(locale).name();
          if (translator.load(":/i18n/" + baseName)) {
              a.installTranslator(&translator);
              break;
          }
      }
      example w;
      w.show();
      return a.exec();
      

      }

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2
      example::example(QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::example)
      {
      ui->setupUi(this);
      
      connect(qApp, &QApplication::focusChanged, this, [this](QWidget *old, QWidget *now)
      	{
      	if(old==ui->textEdit)
      		{
      		qDebug()<<"textEdit lose focus";
      		}
      	});
      }
      
      1 Reply Last reply
      0
      • E EricR has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved