Qt-在QDialog中绘制QScrollArea [英] Qt - draw inside QScrollArea in a QDialog

查看:836
本文介绍了Qt-在QDialog中绘制QScrollArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt 5中,我有一个QDialog窗口,在该窗口上绘制了一个圆形,如下所示:

In Qt5, I have a QDialog window on which I have drawn a circle as follows:

void MyDialog::paintEvent(QPaintEvent *pe)
{
       QPainter painter(this);
       painter.setRenderHint(QPainter::Antialiasing,true);
       QPen pen(Qt::blue,2);
       painter.setPen(pen);
       QRect r=QRect(0,0,100,100);
       painter.drawEllipse(r);
}

如果我绘制一个较大的圆(例如使用QRect(0,0,500,500);),则大于对话框窗口的圆将被裁剪.因此,我将QScrollArea拖动到对话框窗口上,并决定在其上进行绘制,以便自动添加滚动条.可以使用ui->scrollArea来访问QScrollArea.

If I draw a larger circle, for example by using QRect(0,0,500,500);, the circle being greater than the dialog window is clipped. So I dragged a QScrollArea onto the the dialog window and decide to draw onto that so that scroll bars are automatically added. The QScrollArea can be accessed using ui->scrollArea.

我通过设置QPainter painter(ui->scrollArea);更改了上面的代码.但是,QScrollArea中什么也没有出现.我读到我需要覆盖QScrollAreapaintEvent.但是我不知道该怎么做.

I changed the above code by setting QPainter painter(ui->scrollArea);. However, nothings appears in the QScrollArea. I read that I need to override the paintEvent of QScrollArea. But I don't know how to do this.

QScrollArea上绘图有帮助吗?

推荐答案

在QScrollArea上绘制也不是您想要的,因为QScrollArea实际上具有视口小部件.

Drawing on the QScrollArea is not what you want either because the QScrollArea actually has a viewport widget.

创建另一个继承QWidget的类.覆盖paintEvent()方法和您提到的绘画.然后,将小部件添加到对话框的滚动区域.

Create another class which inherits QWidget. Override the paintEvent() method and to the painting you mention. Then, add the widget to the scroll area in your dialog.

MyDialog::MyDialog()
{
  QScrollArea *pScrl = new QScrollArea(this);
  pScrl->setWidget(new MyWidget(pScrl));
  ... // use a layout to put the scroll area in the dialog
}

要使其真正有用,您需要将MyWidget实例的大小调整为要绘制的圆圈的大小.

To really make it useful you will need to resize the MyWidget instance to the size of the circle that you want to draw.

这篇关于Qt-在QDialog中绘制QScrollArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆