I'm trying to figure out how I can use the mouseReleaseEvent of the textEdit in the simple notepad example.
http://doc.qt.io/qt-5/gettingstartedqt.html
I understand how to set it up for the Notepad class...
but I can't figure out how to access <widget class="QTextEdit" name="textEdit"> from the ui file.
The closest I have come created a second QTextEdit,
when I tried to mimic the Notepad class.
notepad.h
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
#include <QTextEdit>
namespace Ui {
class Notepad;
class textEdit;
}
{
Q_OBJECT
public:
explicit Notepad
(QWidget *parent
= 0);
~Notepad();
private slots:
void on_quitButton_clicked();
private:
Ui::Notepad *ui;
};
{
Q_OBJECT
public:
private slots:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
notepad.cpp
#include "notepad.h"
#include "ui_notepad.h"
Notepad
::Notepad(QWidget *parent
) : ui(new Ui::Notepad)
{
ui->setupUi(this);
}
Notepad::~Notepad()
{
delete ui;
}
void Notepad::on_quitButton_clicked()
{
qApp->quit();
}
textEdit
::textEdit(QWidget *parent
) :
{
copy();
}
#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();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Notepad w;
w.show();
textEdit txt(&w);
txt.show();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Qt 5.4.1 (windows 7)
Bookmarks