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. using mouse events of a QTextEdit made with the designer
Forum Updated to NodeBB v4.3 + New Features

using mouse events of a QTextEdit made with the designer

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.4k Views 2 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.
  • ravasR Offline
    ravasR Offline
    ravas
    wrote on last edited by ravas
    #1

    My goal is to use mouseReleaseEvent() to copy the selection of a QTextEdit that was created in designer <widget class="QTextEdit" name="textEdit">.

    With the following code, a secondary QTextEdit is created. My desired feature does work... however, I didn't want to create a second QTextEdit, I wanted to deal with the existing one. How can I accomplish that?

    notepad.h

    #ifndef NOTEPAD_H
    #define NOTEPAD_H
    
    #include <QMainWindow>
    #include <QTextEdit>
    
    namespace Ui {
    class Notepad;
    class textEdit;
    }
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit Notepad(QWidget *parent = 0);
        ~Notepad();
    
    private slots:
        void on_quitButton_clicked();
    
    private:
        Ui::Notepad *ui;
    };
    
    
    class textEdit : public QTextEdit
    {
        Q_OBJECT
    
    public:
        textEdit(QWidget *parent);
    
    private slots:
    
        void mouseReleaseEvent(QMouseEvent *event);
    
    };
    
    #endif // NOTEPAD_H
    

    notepad.cpp

    #include "notepad.h"
    #include "ui_notepad.h"
    
    
    Notepad::Notepad(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Notepad)
    {
        ui->setupUi(this);
    }
    
    Notepad::~Notepad()
    {
        delete ui;
    }
    
    
    void Notepad::on_quitButton_clicked()
    {
        qApp->quit();
    
    }
    
    textEdit::textEdit(QWidget *parent) :
        QTextEdit(parent){}
    
    void textEdit::mouseReleaseEvent(QMouseEvent *event)
    {
        copy();
    }
    

    main.cpp

    #include "notepad.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Notepad w;
        w.show();
        textEdit txt(&w);
        txt.show();
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can can use an event filter. More details there

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ravasR 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You can can use an event filter. More details there

        Hope it helps

        ravasR Offline
        ravasR Offline
        ravas
        wrote on last edited by
        #3

        @SGaist I don't understand. What am I connecting the filter to? Notepad? It seems like I would need to check what widget the event is from. This leaves me with the same problem... I just need to know how to define the class that already exists in the .ui file.

        1 Reply Last reply
        0
        • ravasR Offline
          ravasR Offline
          ravas
          wrote on last edited by
          #4

          The solution is to right click on the textEdit and then choose Promote.
          I named the promoted class as qText and created the following files.

          qtext.h

          #define QTEXT_H
          
          #include <QTextEdit>
          
          class qText : public QTextEdit
          {
              Q_OBJECT
          
          public:
          
              explicit qText(QWidget *parent);
          
          private slots:
          
              void mouseReleaseEvent(QMouseEvent *event);
          
          };
          
          #endif // QTEXT_H
          

          qtext.cpp

          #include "qtext.h"
          
          qText::qText(QWidget *parent) :
              QTextEdit(parent) {}
          
          void qText::mouseReleaseEvent(QMouseEvent *event)
          {
              copy();
              QTextEdit::mouseReleaseEvent(event);
          }
          
          1 Reply Last reply
          0

          • Login

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