В Qt я пытаюсь выделить элемент, когда я его нажимаю, а затем нарисую линию от ее конца до текущей позиции курсора. Я могу либо выделить его, либо нарисовать линию, но не обе. Вот мои классы:overriding paint() и mouseEvents() QGraphicsItem
// ucPin.h
#pragma once
#include "stdafx.h"
class ucPin : public QGraphicsLineItem{
qreal x1, y1, x2, y2;
bool isClicked;
QGraphicsLineItem * li;
public :
ucPin (qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent = 0);
void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
void mousePressEvent (QGraphicsSceneMouseEvent * event);
void mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
void mouseMoveEvent (QGraphicsSceneMouseEvent *event);
};
// ucPin.cpp
#include "ucPin.h"
ucPin::ucPin (qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent) :
QGraphicsLineItem(x1, y1, x2, y2, parent){
this->x1 = x1; this->y1 = y1; this->x2 = x2; this->y2 = y2;
this->setFlags(QGraphicsItem::ItemIsFocusable
| QGraphicsItem::ItemIsSelectable);
isClicked = false;
li = NULL;
}
void ucPin::paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
QStyleOptionGraphicsItem newOption (*option);
newOption.state =QStyle::State_None;
QGraphicsLineItem::paint(painter, &newOption, widget);
//painter-> setRenderHint(QPainter::Antialiasing);
if (option->state & QStyle::State_Selected) {
QPen outline;
outline.setColor(Qt::green);
outline.setWidth(6);
setPen(outline);
}
else{
QPen outline; outline.setWidth(3);
setPen(outline);
}
}
void ucPin::mousePressEvent(QGraphicsSceneMouseEvent* event) {
/*QPointF pos = event->pos();
QGraphicsLineItem * li = new QGraphicsLineItem(0,0, pos.x(), pos.y(), this);*/
isClicked = true;
}
void ucPin::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
isClicked = false;
}
void ucPin::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
if(NULL != li) { delete li; li = NULL;}
li = new QGraphicsLineItem(0,0, event->pos().x(), event->pos().y(), this);
}
Я думаю, при реализации переопределение MouseEvent, событие краски для выбранного состояния не дозвонились? Как можно обойти это?
Я уверен, вы увидите, что вы хотите, если вы замените код в 'ucPin :: mouseMoveEvent' на' this-> setLine (0, 0, event-> pos(). X(), event-> pos () .y()) '. Почему вы создаете эту вещь 'li' в любом случае, если вы наследуете' QGraphicsLineItem'? – Rostislav
Я хотел бы увидеть объект ucPin, выделенный. Вы, как вы указали, очень важны QGraphicsLine. Затем я хочу сделать еще одну строку и присоединиться к ней с оригиналом. Следовательно, li .. –
Хорошо, имеет смысл. Посмотрите, помогает ли мой ответ. – Rostislav