PDA

View Full Version : defining a widget from the ui file



ravas
23rd June 2015, 07:45
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;
}

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();
}

Qt 5.4.1 (windows 7)

anda_skoa
23rd June 2015, 09:06
if you want your own QTextEdit subclass to be instantiated, you'll need to use the "promote widget" functionality.

Make a proper header/source implementation of your text edit class and then specify that class and its header in designer when using the promote feature.

Cheers,
_

yeye_olive
23rd June 2015, 09:53
but I can't figure out how to access <widget class="QTextEdit" name="textEdit"> from the ui file.
You can access it with ui->textEdit from your Notepad class.

anda_skoa
23rd June 2015, 11:04
You can access it with ui->textEdit from your Notepad class.

The object, yes, but ravas wants to reimplement a virtual function of QTextEdit.

Cheers,
_

yeye_olive
23rd June 2015, 11:45
The object, yes, but ravas wants to reimplement a virtual function of QTextEdit.
Ah, yes. There is no way around widget promotion if he is to reimplement mouseReleaseEvent(). Alternatively, and depending on what he wants to achieve, he could install an event filter on the existing widget.

ravas
23rd June 2015, 19:32
Thanks. I can't seem to figure it out. I keep getting:


C:\1A\2015\QT\build-QuickPad-Desktop_Qt_5_4_1_MinGW_32bit-Debug\ui_notepad.h:58: error: expected type-specifier before 'textEdit'
textEdit = new textEdit(centralWidget);
^

textedit.h

#ifndef TEXTEDIT_H
#define TEXTEDIT_H

#include <QTextEdit>

namespace Ui {
class textEdit;
}

class textEdit : public QTextEdit
{
Q_OBJECT

public:
explicit textEdit(QWidget *parent);
~textEdit();

private slots:

void mouseReleaseEvent(QMouseEvent *event);

private:
Ui::textEdit *ui;
};

#endif // TEXTEDIT_H

textedit.cpp


#include "textedit.h"

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

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

void textEdit::mouseReleaseEvent(QMouseEvent *event)
{
copy();
}

anda_skoa
23rd June 2015, 21:03
You called your class "textEdit" and you seem to have used the very same character combination as the variable/object name in designer.

Rename either the class or the object.

Cheers,
_

ravas
23rd June 2015, 22:09
I thought it needed to be the same. :rolleyes:
I got it working. Thank you! :cool:

qtext.h


#ifndef 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);
}