Qt - draw inside QScrollArea in a QDialog

假如想象 提交于 2019-12-25 04:24:20

问题


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);
}

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.

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.

Any help of drawing on the QScrollArea?


回答1:


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

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
}

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



来源:https://stackoverflow.com/questions/22076136/qt-draw-inside-qscrollarea-in-a-qdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!