{
public:
explicit Widget
(QWidget * parent
= 0) , mLeft()
, mRight()
{
mLeftLabel
->setPixmap
(QPixmap("logo.png"));
mRightLabel
->setPixmap
(QPixmap("logo.png"));
gridLayout->addWidget(mLeftLabel, 0, 0, 1, 1);
gridLayout->addWidget(mRightLabel, 0, 1, 1, 1);
stackedLayout->addWidget(mTopWidget);
stackedLayout->addWidget(mBottomWidget);
mTopWidget->installEventFilter(this);
}
protected:
{
QPoint l
= mLeftLabel
->mapFromParent
(w
);
QPoint r
= mRightLabel
->mapFromParent
(w
);
qDebug() << "Widget" << w;
if(mLeftLabel->rect().contains(l))
{
qDebug() << "Widget::mLeftLabel" << l;
mLeft = w;
update();
}
if(mRightLabel->rect().contains(r))
{
qDebug() << "Widget::mRightLabel" << r;
mRight = w;
update();
}
}
{
if(object != mTopWidget)
return false;
if(event
->type
() == QEvent::Paint) {
QPainter painter
(static_cast<QWidget
*>
(object
));
QPen pen
= painter.
pen();
pen.setWidth(5);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
painter.drawLine(mLeft, mRight);
}
return false;
}
private:
};
class Widget : public QWidget
{
public:
explicit Widget(QWidget * parent = 0)
: QWidget(parent)
, mLeftLabel(new QLabel)
, mRightLabel(new QLabel)
, mTopWidget(new QWidget)
, mBottomWidget(new QWidget)
, mLeft()
, mRight()
{
QGridLayout * gridLayout = new QGridLayout(mBottomWidget);
mLeftLabel->setPixmap(QPixmap("logo.png"));
mRightLabel->setPixmap(QPixmap("logo.png"));
gridLayout->addWidget(mLeftLabel, 0, 0, 1, 1);
gridLayout->addWidget(mRightLabel, 0, 1, 1, 1);
QStackedLayout * stackedLayout = new QStackedLayout(this);
stackedLayout->addWidget(mTopWidget);
stackedLayout->addWidget(mBottomWidget);
stackedLayout->setStackingMode(QStackedLayout::StackAll);
mTopWidget->installEventFilter(this);
}
protected:
void mousePressEvent(QMouseEvent * event)
{
QPoint w = event->pos();
QPoint l = mLeftLabel->mapFromParent(w);
QPoint r = mRightLabel->mapFromParent(w);
qDebug() << "Widget" << w;
if(mLeftLabel->rect().contains(l))
{
qDebug() << "Widget::mLeftLabel" << l;
mLeft = w;
update();
}
if(mRightLabel->rect().contains(r))
{
qDebug() << "Widget::mRightLabel" << r;
mRight = w;
update();
}
}
bool eventFilter(QObject * object, QEvent * event)
{
if(object != mTopWidget)
return false;
if(event->type() == QEvent::Paint)
{
QPainter painter(static_cast<QWidget*>(object));
QPen pen = painter.pen();
pen.setWidth(5);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
painter.drawLine(mLeft, mRight);
}
return false;
}
private:
QLabel * mLeftLabel;
QLabel * mRightLabel;
QWidget * mTopWidget;
QWidget * mBottomWidget;
QPoint mLeft;
QPoint mRight;
};
To copy to clipboard, switch view to plain text mode
Bookmarks