defining a widget from the ui file
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
Code:
#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
notepad.cpp
Code:
#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();
}
main.cpp
Code:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Notepad w;
w.show();
textEdit txt(&w);
txt.show();
return a.exec();
}
Qt 5.4.1 (windows 7)
Re: defining a widget from the ui file
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,
_
Re: defining a widget from the ui file
Quote:
Originally Posted by
ravas
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.
Re: defining a widget from the ui file
Quote:
Originally Posted by
yeye_olive
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,
_
Re: defining a widget from the ui file
Quote:
Originally Posted by
anda_skoa
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.
Re: defining a widget from the ui file
Thanks. I can't seem to figure it out. I keep getting:
Code:
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
Code:
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QTextEdit>
namespace Ui {
class textEdit;
}
{
Q_OBJECT
public:
explicit textEdit
(QWidget *parent
);
~textEdit();
private slots:
private:
Ui::textEdit *ui;
};
#endif // TEXTEDIT_H
textedit.cpp
Code:
#include "textedit.h"
textEdit
::textEdit(QWidget *parent
) :ui(new Ui::textEdit)
{
ui->setupUi(this);
}
textEdit::~textEdit()
{
delete ui;
}
{
copy();
}
Re: defining a widget from the ui file
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,
_
Re: defining a widget from the ui file
I thought it needed to be the same. :rolleyes:
I got it working. Thank you! :cool:
qtext.h
Code:
#ifndef QTEXT_H
#define QTEXT_H
#include <QTextEdit>
{
Q_OBJECT
public:
private slots:
};
#endif // QTEXT_H
qtext.cpp
Code:
#include "qtext.h"
{
copy();
}