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 can I create a new class for a widget that has access to its parent using Qt Design?
Forum Updated to NodeBB v4.3 + New Features

How can I create a new class for a widget that has access to its parent using Qt Design?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 410 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Collaxd
    wrote on 6 Mar 2023, 17:12 last edited by
    #1

    I'm new to the subject, and I'm learning some things so forgive me, I know it must be a basic subject. However, I created a blank project with the MainWindow class and then using the CTRL + N shortcut I created a new class called MyLabel that inherits from QLabel apparently .

    I want to access the text of an entry and show it in my label from the separate class, in a separate file, my MainWindow has an entry called lineEdit.

    I want to show this in my previously created label in a separate class and file

    Here is my label currently
    #ifndef MYLABEL_H
    #define MYLABEL_H

    #include <QLabel>

    class MyLabel : public QLabel
    {
    public:
    MyLabel();
    };

    #endif // MYLABEL_H

    // mylabel.cpp
    #include "mylabel.h"

    MyLabel::MyLabel()
    {
    // acess parent here.
    }

    Is there any way to do this more easily? I also tried to create a normal label in the UI and use the promote to function by right-clicking, but the file is not created. Thank you very much in advance!

    ? 1 Reply Last reply 6 Mar 2023, 18:31
    0
    • C Collaxd
      6 Mar 2023, 17:12

      I'm new to the subject, and I'm learning some things so forgive me, I know it must be a basic subject. However, I created a blank project with the MainWindow class and then using the CTRL + N shortcut I created a new class called MyLabel that inherits from QLabel apparently .

      I want to access the text of an entry and show it in my label from the separate class, in a separate file, my MainWindow has an entry called lineEdit.

      I want to show this in my previously created label in a separate class and file

      Here is my label currently
      #ifndef MYLABEL_H
      #define MYLABEL_H

      #include <QLabel>

      class MyLabel : public QLabel
      {
      public:
      MyLabel();
      };

      #endif // MYLABEL_H

      // mylabel.cpp
      #include "mylabel.h"

      MyLabel::MyLabel()
      {
      // acess parent here.
      }

      Is there any way to do this more easily? I also tried to create a normal label in the UI and use the promote to function by right-clicking, but the file is not created. Thank you very much in advance!

      ? Offline
      ? Offline
      A Former User
      wrote on 6 Mar 2023, 18:31 last edited by
      #2

      @Collaxd Hi

      what I did is

      CloudManager *AbstractCloudComponent::cloudManager()
      {
          return qobject_cast<CloudManager *> (parent()) ;
      }
      

      Such that any subclass of AbstractCloudComponent can access properties of CloudManager (which is the parent of all AbstractCloudComponent instances) by calling

      cloudManager()->anyMethodFromCloudManager() ;
      

      you have to cast the object type because parent() returns QObject *, not it's real class. Just change classes names and it should do it.

      J 1 Reply Last reply 6 Mar 2023, 19:15
      0
      • ? A Former User
        6 Mar 2023, 18:31

        @Collaxd Hi

        what I did is

        CloudManager *AbstractCloudComponent::cloudManager()
        {
            return qobject_cast<CloudManager *> (parent()) ;
        }
        

        Such that any subclass of AbstractCloudComponent can access properties of CloudManager (which is the parent of all AbstractCloudComponent instances) by calling

        cloudManager()->anyMethodFromCloudManager() ;
        

        you have to cast the object type because parent() returns QObject *, not it's real class. Just change classes names and it should do it.

        J Offline
        J Offline
        JonB
        wrote on 6 Mar 2023, 19:15 last edited by JonB 3 Jun 2023, 19:16
        #3

        @ankou29666
        Yes this works but it's pretty "dangerous" especially when you are starting out with Qt/C++ when you're developing. It would be worth:

            CloudManager *manager = qobject_cast<CloudManager *> (parent());
            Q_ASSERT(manager != nullptr);
            return manager;
        

        Up to you what you do here if parent() == nullptr. Here I leave it to assert, along with any non-CloudManager * parent.

        @Collaxd
        It's usually a sign of bad design for a child to need to access its parent or know what type it is. Parents can know about children but children simply should not need to know about parents or rely on them. Your intended behaviour can likely be better written.

        ? C 2 Replies Last reply 6 Mar 2023, 19:26
        1
        • J JonB
          6 Mar 2023, 19:15

          @ankou29666
          Yes this works but it's pretty "dangerous" especially when you are starting out with Qt/C++ when you're developing. It would be worth:

              CloudManager *manager = qobject_cast<CloudManager *> (parent());
              Q_ASSERT(manager != nullptr);
              return manager;
          

          Up to you what you do here if parent() == nullptr. Here I leave it to assert, along with any non-CloudManager * parent.

          @Collaxd
          It's usually a sign of bad design for a child to need to access its parent or know what type it is. Parents can know about children but children simply should not need to know about parents or rely on them. Your intended behaviour can likely be better written.

          ? Offline
          ? Offline
          A Former User
          wrote on 6 Mar 2023, 19:26 last edited by
          #4

          @JonB Thanks for feedback :)

          1 Reply Last reply
          0
          • J JonB
            6 Mar 2023, 19:15

            @ankou29666
            Yes this works but it's pretty "dangerous" especially when you are starting out with Qt/C++ when you're developing. It would be worth:

                CloudManager *manager = qobject_cast<CloudManager *> (parent());
                Q_ASSERT(manager != nullptr);
                return manager;
            

            Up to you what you do here if parent() == nullptr. Here I leave it to assert, along with any non-CloudManager * parent.

            @Collaxd
            It's usually a sign of bad design for a child to need to access its parent or know what type it is. Parents can know about children but children simply should not need to know about parents or rely on them. Your intended behaviour can likely be better written.

            C Offline
            C Offline
            Collaxd
            wrote on 6 Mar 2023, 19:50 last edited by Collaxd 3 Jun 2023, 20:02
            #5

            @JonB Thank you for your response to see if I understand and my tests have not been working, see my code.

            I made the ui public in the MainWindow and then applied some of your changes.

            #ifndef MYLABEL_H
            #define MYLABEL_H

            #include <QLabel>

            class MyLabel : public QLabel
            {
            Q_OBJECT
            public:
            MyLabel(QWidget *parent = nullptr);
            };

            #endif // MYLABEL_H
            #include "mainwindow.h"
            #include "mylabel.h"

            MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
            {
            MainWindow *mainWindow = qobject_cast<MainWindow *> (parent());
            qDebug() << mainWindow;

            }

            error:
            C:\Users\Colla\Desktop\Programming\cpp\untitled\mylabel.cpp:7: error: C2064: term does not evaluate to a function taking 0 arguments

            Yes, it is recommended that you update the states of your MyLabel class within the MainWindow ?

            Because I want to edit the colors of my label through mouse events, so it would be clearer to create a separate class, but the button that will control whether the mouse edits the colors or not will be in the MainWindow's menu.

            Thinking more carefully now, I can create a boolean variable in my label that I can change from within my main window to start editing. Thanks for the advice. I'll never forget it. Parents should know about their children, but children should not know about their parents :D

            ? 1 Reply Last reply 6 Mar 2023, 20:10
            0
            • C Collaxd
              6 Mar 2023, 19:50

              @JonB Thank you for your response to see if I understand and my tests have not been working, see my code.

              I made the ui public in the MainWindow and then applied some of your changes.

              #ifndef MYLABEL_H
              #define MYLABEL_H

              #include <QLabel>

              class MyLabel : public QLabel
              {
              Q_OBJECT
              public:
              MyLabel(QWidget *parent = nullptr);
              };

              #endif // MYLABEL_H
              #include "mainwindow.h"
              #include "mylabel.h"

              MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
              {
              MainWindow *mainWindow = qobject_cast<MainWindow *> (parent());
              qDebug() << mainWindow;

              }

              error:
              C:\Users\Colla\Desktop\Programming\cpp\untitled\mylabel.cpp:7: error: C2064: term does not evaluate to a function taking 0 arguments

              Yes, it is recommended that you update the states of your MyLabel class within the MainWindow ?

              Because I want to edit the colors of my label through mouse events, so it would be clearer to create a separate class, but the button that will control whether the mouse edits the colors or not will be in the MainWindow's menu.

              Thinking more carefully now, I can create a boolean variable in my label that I can change from within my main window to start editing. Thanks for the advice. I'll never forget it. Parents should know about their children, but children should not know about their parents :D

              ? Offline
              ? Offline
              A Former User
              wrote on 6 Mar 2023, 20:10 last edited by
              #6

              @Collaxd Your case is different than mine and I think you can deal with your events without the need of accessing the parent. But concerning how to achieve this, I prefer let others tell you about it ;)

              C 1 Reply Last reply 7 Mar 2023, 11:43
              0
              • ? A Former User
                6 Mar 2023, 20:10

                @Collaxd Your case is different than mine and I think you can deal with your events without the need of accessing the parent. But concerning how to achieve this, I prefer let others tell you about it ;)

                C Offline
                C Offline
                Collaxd
                wrote on 7 Mar 2023, 11:43 last edited by
                #7

                @ankou29666
                Find a simpler way to do this by creating a checkbox variable inside my label:
                QCheckBox* m_editModeCheckbox;

                Linking it with:
                m_editModeCheckbox(parent->findChild<QCheckBox*>("editMode"))

                Accessing it with:
                if (m_editModeCheckbox && m_editModeCheckbox->isChecked())

                #include "handlabel.h"

                HandLabel::HandLabel(QWidget parent, QString text) : QLabel(parent)
                , defaultBg("background-color: rgb(191, 191, 191);")
                , defaultFont("Jetbrains Mono", 12)
                , m_editModeCheckbox(parent->findChild<QCheckBox
                >("editMode"))
                {
                setText(text);
                setAlignment(Qt::AlignCenter);
                setFont(defaultFont);
                setStyleSheet(defaultBg);
                }

                void HandLabel::enterEvent(QEnterEvent *ev)
                {
                if (m_editModeCheckbox && m_editModeCheckbox->isChecked())
                {
                // fazer algo se o checkbox estiver marcado
                setStyleSheet("background-color: rgb(255, 255, 0);");
                }

                }

                void HandLabel::leaveEvent(QEvent *ev)
                {
                if (m_editModeCheckbox && m_editModeCheckbox->isChecked())
                {
                // fazer algo se o checkbox estiver marcado
                setStyleSheet(defaultBg);
                }
                }

                1 Reply Last reply
                0
                • C Collaxd has marked this topic as solved on 7 Mar 2023, 11:44

                1/7

                6 Mar 2023, 17:12

                • Login

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