Solved How to use qobject_cast with inherited class
-
Hello everyone, I have created a class called TextEdit, which inherits from QTextEdit.
On my mainwindow I have a tabWidget and everytime I create a new tab a TextEdit should appear.
But that's not the problem.
How can I use qobject_cast to access the widget inside of the tab?
Cause now whenever I try it says:C:\Qt\6.1.2\mingw81_64\include\QtCore\qobject.h:449: error: static assertion failed: qobject_cast requires the type to have a Q_OBJECT macro
The code to get the textedit is this:
TextEdit* MainWindow::getTabTextEdit() { return qobject_cast<TextEdit*>(ui->tabWidget->widget(ui->tabWidget->currentIndex())); }
Can you help me please?
-
@HenkCoder said in How to use qobject_cast with inherited class:
How do I make the thing with Q_OBJECT macro then?
class TextEdit : public QTextEdit { Q_OBJECT public:
and since we're at it, lets do a proper constructor for a Qt class as well:
public: explicit TextEdit(QWidget *parent = nullptr) : QTextEdit(parent) {}
-
@HenkCoder the error message is really self-explanatory:
qobject_cast requires the type to have a Q_OBJECT macro
you didn't add the Q_OBJECT macro in your header file.
but assuming, your
TextEdit
is derived from QWidget, somewhere and you placed it via the designer, than yes, the cast should work, if you place the macro -
Or you maybe forgot a
Q
?qobject_cast<QTextEdit*>
? -
@VRonin No, I inherited QTextEdit class.
-
@J-Hilk
Hi, actually, no.
I just have a blank tabWidget and in the Mainwindow constructor it creates a tab with the textEdit in it.
Btw TextEdit is derived from QTextEdit.
How do I make the thing with Q_OBJECT macro then?
This is the code in the header:class TextEdit : public QTextEdit { public: bool canInsertFromMimeData(const QMimeData* source) const { return source->hasImage() || source->hasUrls() || QTextEdit::canInsertFromMimeData(source); } void insertFromMimeData(const QMimeData* source) { if (source->hasImage()) { static int i = 1; QUrl url(QString("dropped_image_%1").arg(i++)); dropImage(url, qvariant_cast<QImage>(source->imageData())); } else if (source->hasUrls()) { foreach (QUrl url, source->urls()) { QFileInfo info(url.toLocalFile()); if (QImageReader::supportedImageFormats().contains(info.suffix().toLower().toLatin1())) dropImage(url, QImage(info.filePath())); else dropTextFile(url); } } else { QTextEdit::insertFromMimeData(source); } } private: void dropImage(const QUrl& url, const QImage& image) { if (!image.isNull()) { document()->addResource(QTextDocument::ImageResource, url, image); textCursor().insertImage(url.toString()); } } void dropTextFile(const QUrl& url) { QFile file(url.toLocalFile()); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) textCursor().insertText(file.readAll()); } };
Hope it helps.
-
@HenkCoder said in How to use qobject_cast with inherited class:
class TextEdit : public QTextEdit
{
public:class TextEdit : public QTextEdit { Q_OBJECT public: using QTextEdit::QTextEdit; // this is unrelated but forwards the constructor of QTextEdit
-
@HenkCoder said in How to use qobject_cast with inherited class:
How do I make the thing with Q_OBJECT macro then?
class TextEdit : public QTextEdit { Q_OBJECT public:
and since we're at it, lets do a proper constructor for a Qt class as well:
public: explicit TextEdit(QWidget *parent = nullptr) : QTextEdit(parent) {}
-
Thanks guys! it worked