Qt-在QDialog中绘制QScrollArea [英] Qt - draw inside QScrollArea in a QDialog
问题描述
在Qt
5中,我有一个QDialog
窗口,在该窗口上绘制了一个圆形,如下所示:
In Qt
5, 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
中什么也没有出现.我读到我需要覆盖QScrollArea
的paintEvent
.但是我不知道该怎么做.
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屋!