1
질문 : GraphicsView의 paintEvent를 호출 할 수 없거나 QPainter를 완전히 작동시키는 방법을 모르겠습니다.QGraphicsView 하위 클래스를 사용하여 QPainter로 선을 그립니다.
- qtcreator의 디자이너를 사용하여 QGraphicsView를 기본 창에 배치하고 있습니다.
- I의 (
class Draw
)을paintEvent
메인 창에서이 서브 클래스 (Draw draw
)의 인스턴스를 개최를 오버라이드 (override)를 QGraphicsView을 하위 분류 - 가 나는
new QGraphicsScene
을 만들어 모두에 할당 (나는 다른 복잡한 도면을 이동하고 싶었)QGraphicsView's
(ui->graphicsView
과draw
)Draw
을 그릴 때 시각적으로는ui->graphicsview
에도 적용됩니다 (희망 사항). - 나는 새로운
scene
객체를 사용하여 그리는, 나는 그러나 (눈에 보이는 결과를 얻을 때 확신, 나는 그리기하지만QPainter
의scene
객체를 사용하지 않습니다. 그 이유를 내가 희망을 또 다른 질문에 대한 이유 따라서 필수적이지는 않습니다.) 따라서QGraphicsView
을 서브 클래스 화하고 paintEvent를 재정의하면QPainter p(this)
이 쉽게 생성됩니다. - 이벤트가 발생하면
Draw
개체에서paintEvent()
을 호출하려고 시도하는Window.cpp
에MyRepaint()
을 호출합니다. 그러나 작동하지 않습니다.
메인 창 'Window.cpp'Draw.h :
Window::Window(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::Window),
draw(*this) //my own QGraphicsView instance (see class below)
{
ui->setupUi(this);
//assigning a new scene to draw and to window's graphicsView
this->scene = new QGraphicsScene(this);
this->draw.setScene(scene);
this->ui->graphicsView->setScene(scene);
}
void Window::MyRepaint()
{
qInfo() << "Repaint - start" << this->draw.scene();
this->draw.scene()->update(this->draw.sceneRect());
this->draw.repaint();
this->draw.viewport()->update();
/*only the following line made the paintEvent executed eventually but without a visible result and with an error in the output*/
this->draw.paintEvent(NULL);
qInfo() << "Repaint - end";
}
이를 QGraphicsView, 파일 서브 클래스
class Window;
class Draw : public QGraphicsView{
private:
Window& parent;
public:
Draw(Window &parent);
void paintEvent(QPaintEvent*e) override;
};
Draw.cpp을
void Draw::paintEvent(QPaintEvent *e)
{
qInfo() << "trying to draw";
QPainter p(this);
p.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
p.drawLine(0, 0, 200, 200);
}
출력 :
Repaint - start QGraphicsScene(0x15c7eca8)
trying to draw
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::viewport: Painter not active
QPainter::end: Painter not active, aborted
Repaint - end
아마 내가 완전히 잘못된 접근 방식을 선택했습니다.
예 나는 그 페이지를 봤는데, 그 불필요하게 너무 긴 기사. 최소한의 예제와 튜토리얼이 필요합니다. 이제 내가 언급 한 "판촉 행사"를보고 있는데, 나는 그것에 대해 몰랐다. –
https://bitbucket.org/K117/qtlineexample/src 내가 작성할 수있는 가장 간단한 회선 예입니다. – K117
프로모션. 디자이너에서 위젯의 RMB를 클릭하고 "승격 대상 ..."을 선택하고 파생 클래스의 이름을 입력하십시오. 위젯을 파생 된 클래스로 승격시킬 수 있습니다. – K117