Hi.

Look at this small example. It’s not fully functional but as example it’s good 
enough, I hope...

From: Jean Richard Lima 
Sent: Wednesday, October 29, 2014 8:34 PM
To: Interest@qt-project.org 
Subject: [Interest] Position y() of widget inside Layout

Hello!

How can I get the y () position of a widget inside another? 

I have 3 widgets within another widget when and try: geometry()->y() it returns 
me is always 0 when applied to the layout, if he gives me shot, but I need the 
layout and position, as I am dragging one over the other to switch places. 

Thank you.

              Jean Richard Lima
        Consultor de Gestão e TIC
Software House de Projetos Específicos


--------------------------------------------------------------------------------
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
#include <QWidget>
#include <QPainter>
#include <QHBoxLayout>
#include <QApplication>
#include <QMouseEvent>


class Widget
        :       public QWidget
{
public:
        explicit Widget( const QColor & color, QWidget * parent = 0 )
                :       QWidget( parent )
                ,       m_color( color )
        {}

        QSize sizeHint() const
        {
                return QSize( 100, 75 );
        }

protected:
        void paintEvent( QPaintEvent * )
        {
                QPainter p( this );

                p.setBrush( m_color );

                p.drawRect( rect() );
        }

private:
        QColor m_color;
};


class Stack
        :       public QWidget
{
public:
        Stack()
                :       m_layout( 0 )
                ,       m_clicked( false )
                ,       m_deltaX( 0 )
                ,       m_deltaY( 0 )
        {
                m_layout = new QHBoxLayout( this );

                Widget * w1 = new Widget( Qt::red, this );
                m_layout->addWidget( w1 );

                Widget * w2 = new Widget( Qt::green, this );
                m_layout->addWidget( w2 );

                Widget * w3 = new Widget( Qt::blue, this );
                m_layout->addWidget( w3 );

                setLayout( m_layout );

                resize( 350, 100 );
        }

protected:
        void mousePressEvent( QMouseEvent * e )
        {
                if( e->button() == Qt::LeftButton )
                {
                        m_clicked = true;
                        m_pos = e->pos();
                }

                e->accept();
        }

        void mouseMoveEvent( QMouseEvent * e )
        {
                if( m_clicked )
                {
                        const QPoint pos = e->pos();

                        int index = -1;

                        QLayoutItem * item = findLayoutItem( pos, index );

                        if( item && index >= 0 )
                        {
                                const QRect r = item->geometry();

                                if( item->widget() )
                                        item->widget()->raise();

                                const int deltaX = pos.x() - m_pos.x();
                                const int deltaY = pos.y() - m_pos.y();

                                m_deltaX += deltaX;
                                m_deltaY += deltaY;

                                if( qAbs( m_deltaX ) >= r.width() / 2 )
                                {
                                        if( m_deltaX > 0 )
                                        {
                                                if( index < m_layout->count() - 
1 )
                                                {
                                                        item = 
m_layout->takeAt( index );

                                                        m_layout->insertItem( 
index + 1, item );

                                                        m_deltaX = 0;
                                                        m_deltaY = 0;
                                                }
                                        }
                                        else
                                        {
                                                if( index > 0 )
                                                {
                                                        item = 
m_layout->takeAt( index );

                                                        m_layout->insertItem( 
index - 1, item );

                                                        m_deltaX = 0;
                                                        m_deltaY = 0;
                                                }
                                        }
                                }

                                else
                                        item->setGeometry( QRect( r.x() + 
deltaX,
                                                r.y() + deltaY, r.width(), 
r.height() ) );
                        }

                        m_pos = pos;
                }

                e->accept();
        }

        void mouseReleaseEvent( QMouseEvent * e )
        {
                m_clicked = false;

                m_deltaX = 0;
                m_deltaY = 0;

                m_layout->update();

                e->accept();
        }

private:
        QLayoutItem * findLayoutItem( const QPoint & pos, int & index )
        {
                index = -1;

                if( !m_layout->isEmpty() )
                {
                        for( int i = 0; i < m_layout->count(); ++i )
                                if( m_layout->itemAt( i )->geometry().contains( 
pos ) )
                                {
                                        index = i;

                                        return m_layout->itemAt( i );
                                }
                }

                return 0;
        }

private:
        QHBoxLayout * m_layout;
        bool m_clicked;
        QPoint m_pos;
        int m_deltaX;
        int m_deltaY;
};


int main( int argc, char ** argv )
{
        QApplication app( argc, argv );

        Stack s;
        s.show();

        return app.exec();
}
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to