How delete parent?



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


  • Moderators

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


  • Moderators

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


  • Moderators

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



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


  • Moderators

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



  • 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??


  • Moderators

    @QT-static-prgm Just replace

    m_parent->show();
    

    with

    this->show();
    


  • 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



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


  • you understand me now :D

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.