using mouse events of a QTextEdit made with the designer
-
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(); }
-
-
@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.
-
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); }