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 delete parent?

How delete parent?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 4.1k 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by A Former User
    #1

    Hi,

    i have a custom QTreeWidget to have a transparent background and allow moving via drag.

    #include "ChannelListWidget.h"
    #include <QMouseEvent>
    
    ChannelListWidget::ChannelListWidget()
    	: QTreeWidget()
    {
    	m_parent = new QWidget;
    	m_parent->setAttribute(Qt::WA_TranslucentBackground);
    	m_parent->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    	m_parent->setGeometry(320,200,1,1);
    
    	setHeaderHidden(true);
    	setExpandsOnDoubleClick(false);
    	setStyleSheet("padding:2px;border-radius:5px;border-style:solid;border-width:1px;border-color:rgb(100,100,100); background-color:rgba(50,50,50,150); color:rgb(255,255,255);");
    	setParent(m_parent);
    
    	resize(200, 400);
    	m_parent->adjustSize();
    	m_parent->show();
    }
    
    ChannelListWidget::~ChannelListWidget()
    {
    	m_parent->deleteLater();
    }
    
    void ChannelListWidget::mousePressEvent(QMouseEvent * event)
    {
    	if (event->button() == Qt::LeftButton)
    		isMouseDown = true;
    }
    
    void ChannelListWidget::mouseReleaseEvent(QMouseEvent * event)
    {
    	isMouseDown = false;
    }
    
    void ChannelListWidget::mouseMoveEvent(QMouseEvent * event)
    {
    	if (isMouseDown)
    		m_parent->move(event->globalPos());
    }
    
    #pragma once
    #include <QWidget>
    #include <QPoint>
    #include <QTreeWidget>
    
    class ChannelListWidget : public QTreeWidget
    {
    	Q_OBJECT
    
    public:
    	explicit ChannelListWidget();
    	~ChannelListWidget();
    
    protected:
    	void mousePressEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
    	void mouseReleaseEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
    	void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
    
    private:
    	bool isMouseDown = false;
    	QWidget *m_parent;
    
    public:
    
    };
    
    

    Does the deleteLater works to kill the parent?? It does not crash anymore (before i just type delete m_parent) but i want to be sure that this will delete the parent.

    jsulmJ 3 Replies Last reply
    0
    • QT-static-prgmQ QT-static-prgm

      Hi,

      i have a custom QTreeWidget to have a transparent background and allow moving via drag.

      #include "ChannelListWidget.h"
      #include <QMouseEvent>
      
      ChannelListWidget::ChannelListWidget()
      	: QTreeWidget()
      {
      	m_parent = new QWidget;
      	m_parent->setAttribute(Qt::WA_TranslucentBackground);
      	m_parent->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
      	m_parent->setGeometry(320,200,1,1);
      
      	setHeaderHidden(true);
      	setExpandsOnDoubleClick(false);
      	setStyleSheet("padding:2px;border-radius:5px;border-style:solid;border-width:1px;border-color:rgb(100,100,100); background-color:rgba(50,50,50,150); color:rgb(255,255,255);");
      	setParent(m_parent);
      
      	resize(200, 400);
      	m_parent->adjustSize();
      	m_parent->show();
      }
      
      ChannelListWidget::~ChannelListWidget()
      {
      	m_parent->deleteLater();
      }
      
      void ChannelListWidget::mousePressEvent(QMouseEvent * event)
      {
      	if (event->button() == Qt::LeftButton)
      		isMouseDown = true;
      }
      
      void ChannelListWidget::mouseReleaseEvent(QMouseEvent * event)
      {
      	isMouseDown = false;
      }
      
      void ChannelListWidget::mouseMoveEvent(QMouseEvent * event)
      {
      	if (isMouseDown)
      		m_parent->move(event->globalPos());
      }
      
      #pragma once
      #include <QWidget>
      #include <QPoint>
      #include <QTreeWidget>
      
      class ChannelListWidget : public QTreeWidget
      {
      	Q_OBJECT
      
      public:
      	explicit ChannelListWidget();
      	~ChannelListWidget();
      
      protected:
      	void mousePressEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
      	void mouseReleaseEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
      	void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
      
      private:
      	bool isMouseDown = false;
      	QWidget *m_parent;
      
      public:
      
      };
      
      

      Does the deleteLater works to kill the parent?? It does not crash anymore (before i just type delete m_parent) but i want to be sure that this will delete the parent.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @QT-static-prgm You mean m_parent is the parent? It isn't. It is just a member variable in ChannelListWidget. Your ChannelListWidget does not have any parent.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • QT-static-prgmQ QT-static-prgm

        Hi,

        i have a custom QTreeWidget to have a transparent background and allow moving via drag.

        #include "ChannelListWidget.h"
        #include <QMouseEvent>
        
        ChannelListWidget::ChannelListWidget()
        	: QTreeWidget()
        {
        	m_parent = new QWidget;
        	m_parent->setAttribute(Qt::WA_TranslucentBackground);
        	m_parent->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
        	m_parent->setGeometry(320,200,1,1);
        
        	setHeaderHidden(true);
        	setExpandsOnDoubleClick(false);
        	setStyleSheet("padding:2px;border-radius:5px;border-style:solid;border-width:1px;border-color:rgb(100,100,100); background-color:rgba(50,50,50,150); color:rgb(255,255,255);");
        	setParent(m_parent);
        
        	resize(200, 400);
        	m_parent->adjustSize();
        	m_parent->show();
        }
        
        ChannelListWidget::~ChannelListWidget()
        {
        	m_parent->deleteLater();
        }
        
        void ChannelListWidget::mousePressEvent(QMouseEvent * event)
        {
        	if (event->button() == Qt::LeftButton)
        		isMouseDown = true;
        }
        
        void ChannelListWidget::mouseReleaseEvent(QMouseEvent * event)
        {
        	isMouseDown = false;
        }
        
        void ChannelListWidget::mouseMoveEvent(QMouseEvent * event)
        {
        	if (isMouseDown)
        		m_parent->move(event->globalPos());
        }
        
        #pragma once
        #include <QWidget>
        #include <QPoint>
        #include <QTreeWidget>
        
        class ChannelListWidget : public QTreeWidget
        {
        	Q_OBJECT
        
        public:
        	explicit ChannelListWidget();
        	~ChannelListWidget();
        
        protected:
        	void mousePressEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
        	void mouseReleaseEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
        	void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
        
        private:
        	bool isMouseDown = false;
        	QWidget *m_parent;
        
        public:
        
        };
        
        

        Does the deleteLater works to kill the parent?? It does not crash anymore (before i just type delete m_parent) but i want to be sure that this will delete the parent.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @QT-static-prgm Now I see: setParent(m_parent) - WHY? Why is the parent member variable? It is not how parent/child should be done in Qt. Parent OWNS the child - your member variable should not own the instance.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • QT-static-prgmQ QT-static-prgm

          Hi,

          i have a custom QTreeWidget to have a transparent background and allow moving via drag.

          #include "ChannelListWidget.h"
          #include <QMouseEvent>
          
          ChannelListWidget::ChannelListWidget()
          	: QTreeWidget()
          {
          	m_parent = new QWidget;
          	m_parent->setAttribute(Qt::WA_TranslucentBackground);
          	m_parent->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
          	m_parent->setGeometry(320,200,1,1);
          
          	setHeaderHidden(true);
          	setExpandsOnDoubleClick(false);
          	setStyleSheet("padding:2px;border-radius:5px;border-style:solid;border-width:1px;border-color:rgb(100,100,100); background-color:rgba(50,50,50,150); color:rgb(255,255,255);");
          	setParent(m_parent);
          
          	resize(200, 400);
          	m_parent->adjustSize();
          	m_parent->show();
          }
          
          ChannelListWidget::~ChannelListWidget()
          {
          	m_parent->deleteLater();
          }
          
          void ChannelListWidget::mousePressEvent(QMouseEvent * event)
          {
          	if (event->button() == Qt::LeftButton)
          		isMouseDown = true;
          }
          
          void ChannelListWidget::mouseReleaseEvent(QMouseEvent * event)
          {
          	isMouseDown = false;
          }
          
          void ChannelListWidget::mouseMoveEvent(QMouseEvent * event)
          {
          	if (isMouseDown)
          		m_parent->move(event->globalPos());
          }
          
          #pragma once
          #include <QWidget>
          #include <QPoint>
          #include <QTreeWidget>
          
          class ChannelListWidget : public QTreeWidget
          {
          	Q_OBJECT
          
          public:
          	explicit ChannelListWidget();
          	~ChannelListWidget();
          
          protected:
          	void mousePressEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
          	void mouseReleaseEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
          	void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
          
          private:
          	bool isMouseDown = false;
          	QWidget *m_parent;
          
          public:
          
          };
          
          

          Does the deleteLater works to kill the parent?? It does not crash anymore (before i just type delete m_parent) but i want to be sure that this will delete the parent.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @QT-static-prgm If parent is deleted it deletes its children. So, if you delete parent in child destructor, parent will try to delete the child which is already being deleted.
          What do you want to achieve putting parent inside child?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • QT-static-prgmQ Offline
            QT-static-prgmQ Offline
            QT-static-prgm
            wrote on last edited by
            #5

            @jsulm

            OK let's start at the beginning:
            I'm doing a overlayplugin, so the objects should be semitransparent. In order to get that working i need to lay an Widget under each object (label, treeview,...). The labels need just to be displayed so it's fine, i can use the stock variant. But my TreeView should be moveable. Since i have framelessWindowHint in order to get the semitransparency working i need to move the window an other way (drag and drop) But the TreeWidget is OVER the widget, so i need to implement the drag/drop für the TreeWidget and move the parent under it.

            So now you may understand my problem. Btw. the deleteLater does not crash always. But sometimes and i don't know why

            jsulmJ 1 Reply Last reply
            0
            • QT-static-prgmQ QT-static-prgm

              @jsulm

              OK let's start at the beginning:
              I'm doing a overlayplugin, so the objects should be semitransparent. In order to get that working i need to lay an Widget under each object (label, treeview,...). The labels need just to be displayed so it's fine, i can use the stock variant. But my TreeView should be moveable. Since i have framelessWindowHint in order to get the semitransparency working i need to move the window an other way (drag and drop) But the TreeWidget is OVER the widget, so i need to implement the drag/drop für the TreeWidget and move the parent under it.

              So now you may understand my problem. Btw. the deleteLater does not crash always. But sometimes and i don't know why

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @QT-static-prgm No, I still don't understand your problem. Why do you set a parent at all if it is just a member variable in ChannelListWidget? Just do not set any parent in ChannelListWidget. Why it is crashing I explained before.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • QT-static-prgmQ Offline
                QT-static-prgmQ Offline
                QT-static-prgm
                wrote on last edited by
                #7

                But if it is not the parent the TreeWidget is not displayed in the widget. Or is there a different way to make a widget being displayed on a QWidget??

                jsulmJ J.HilkJ 2 Replies Last reply
                0
                • QT-static-prgmQ QT-static-prgm

                  But if it is not the parent the TreeWidget is not displayed in the widget. Or is there a different way to make a widget being displayed on a QWidget??

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @QT-static-prgm Just replace

                  m_parent->show();
                  

                  with

                  this->show();
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • QT-static-prgmQ Offline
                    QT-static-prgmQ Offline
                    QT-static-prgm
                    wrote on last edited by
                    #9

                    But then i cannot have a semitransparent background. There needs to be Widget with setAttribute(Qt::WA_TranslucentBackground); under it to enable a semitransparent background for the treewidget

                    1 Reply Last reply
                    0
                    • QT-static-prgmQ QT-static-prgm

                      But if it is not the parent the TreeWidget is not displayed in the widget. Or is there a different way to make a widget being displayed on a QWidget??

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @QT-static-prgm

                      I'm not sure if, if i understand you.

                      Do you want the m_parent = new QWidget; to be a backgroundWidget of your QTreeWidget that automatically moves with your Drag/DropTreewidget?

                      If yes, than something simple like this should do the trick:

                      //Stuff for your Treewidget
                            setHeaderHidden(true);
                      	setExpandsOnDoubleClick(false);
                      	setStyleSheet("padding:2px;border-radius:5px;border-style:solid;border-width:1px;border-color:rgb(100,100,100); background-color:rgba(50,50,50,150); color:rgb(255,255,255);");
                      
                      
                      //Stuff for background widget
                              m_child = new QWidget(this);
                      	m_child ->setAttribute(Qt::WA_TranslucentBackground);
                      	m_child ->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
                      	m_child->show();
                              ....
                      
                              m_child ->setSize(this->size());//Adjust Size
                              m_child->lower() //this moves the child 'under' the parent
                      
                      //Dont forget m_child->deleteLater() in your destructor
                      

                      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
                      0
                      • QT-static-prgmQ Offline
                        QT-static-prgmQ Offline
                        QT-static-prgm
                        wrote on last edited by
                        #11

                        you understand me now :D

                        i tried it, but with that i don't have a Frameless window anymore.

                        QT-static-prgmQ 1 Reply Last reply
                        0
                        • QT-static-prgmQ QT-static-prgm

                          you understand me now :D

                          i tried it, but with that i don't have a Frameless window anymore.

                          QT-static-prgmQ Offline
                          QT-static-prgmQ Offline
                          QT-static-prgm
                          wrote on last edited by
                          #12

                          Seams that i cannot bypass a 2nd custom class. That way it goes:
                          https://git.rwth-aachen.de/carstenf/qtTsOverlay/blob/master/ChannelListWidget.h

                          1 Reply Last reply
                          0

                          • Login

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