From your comment what I think you are trying to achieve is a form which contains your mytextedit and a pushbutton.
What you need to do to get this working is adding a QTextEdit widget to your form (assuming you have a MainWindow with according ui file). You then rightclick on the added QTextEdit and click in the context menu on 'Promote to...' In the promoted class field you fill in 'MyTextEdit' and in the header field it should say 'mytextedit.h'.
Next you click 'add' and 'promote'. Now you have a form with your subclassed textedit on it. You can then add a QPushbutton to the form. And have some text setted in your textedit.
The according code:
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mytextedit.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mytextedit.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->textEdit->setText("Hello world!");
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->textEdit->setText("Hello world!");
}
To copy to clipboard, switch view to plain text mode
mytextedit.h:
#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H
#include <QTextEdit>
{
Q_OBJECT
public:
explicit MyTextEdit
(QWidget *parent
= 0);
private:
signals:
public slots:
};
#endif // MYTEXTEDIT_H
#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H
#include <QTextEdit>
class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit MyTextEdit(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *e);
private:
QString str;
signals:
public slots:
};
#endif // MYTEXTEDIT_H
To copy to clipboard, switch view to plain text mode
mytextedit.cpp:
#include "mytextedit.h"
#include <QDebug>
#include <QMouseEvent>
MyTextEdit
::MyTextEdit(QWidget *parent
) :{
}
{
qDebug() << e->pos();
}
#include "mytextedit.h"
#include <QDebug>
#include <QMouseEvent>
MyTextEdit::MyTextEdit(QWidget *parent) :
QTextEdit(parent)
{
}
void MyTextEdit::mousePressEvent(QMouseEvent *e)
{
qDebug() << e->pos();
QTextEdit::mousePressEvent(e);
}
To copy to clipboard, switch view to plain text mode
Bookmarks