Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use qobject_cast with inherited class
Forum Updated to NodeBB v4.3 + New Features

How to use qobject_cast with inherited class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 870 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HenkCoder
    wrote on 31 Aug 2021, 12:36 last edited by
    #1

    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?

    J 1 Reply Last reply 31 Aug 2021, 12:38
    0
    • H HenkCoder
      31 Aug 2021, 13:10

      @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.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 31 Aug 2021, 13:16 last edited by
      #7

      @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) {}
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • H HenkCoder
        31 Aug 2021, 12:36

        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?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 31 Aug 2021, 12:38 last edited by J.Hilk
        #2

        @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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        H 1 Reply Last reply 31 Aug 2021, 13:10
        3
        • V Offline
          V Offline
          VRonin
          wrote on 31 Aug 2021, 12:42 last edited by
          #3

          Or you maybe forgot a Q? qobject_cast<QTextEdit*>?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          H 1 Reply Last reply 31 Aug 2021, 13:00
          1
          • V VRonin
            31 Aug 2021, 12:42

            Or you maybe forgot a Q? qobject_cast<QTextEdit*>?

            H Offline
            H Offline
            HenkCoder
            wrote on 31 Aug 2021, 13:00 last edited by
            #4

            @VRonin No, I inherited QTextEdit class.

            1 Reply Last reply
            0
            • J J.Hilk
              31 Aug 2021, 12:38

              @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

              H Offline
              H Offline
              HenkCoder
              wrote on 31 Aug 2021, 13:10 last edited by
              #5

              @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.

              J 1 Reply Last reply 31 Aug 2021, 13:16
              0
              • V Offline
                V Offline
                VRonin
                wrote on 31 Aug 2021, 13:14 last edited by VRonin
                #6

                @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
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                H 1 Reply Last reply 31 Aug 2021, 13:27
                2
                • H HenkCoder
                  31 Aug 2021, 13:10

                  @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.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 31 Aug 2021, 13:16 last edited by
                  #7

                  @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) {}
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  3
                  • V VRonin
                    31 Aug 2021, 13:14

                    @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
                    
                    H Offline
                    H Offline
                    HenkCoder
                    wrote on 31 Aug 2021, 13:27 last edited by
                    #8

                    Thanks guys! it worked

                    1 Reply Last reply
                    1

                    8/8

                    31 Aug 2021, 13:27

                    • Login

                    • Login or register to search.
                    8 out of 8
                    • First post
                      8/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved