用代码自定义一个独立显示的Widget,要在里面画图,同时加上滚动条,
滚动条:
printerYuLanPainter = new QPainter(); test = new QScrollArea(); printerYuLanWidget = new QWidget(test); printerYuLanWidget->setMaximumSize(720,1000); printerYuLanWidget->setMinimumSize(720,1000); printerYuLanWidget->close(); test->setWidget(printerYuLanWidget);
画图:
this->printerYuLanWidget->installEventFilter(this);//注册触发机制
//重写eventFilter bool MainWindow::eventFilter(QObject *watched, QEvent *e) { if(watched == this->printerYuLanWidget) { if(e->type() == QEvent::Paint) { qDebug()<<"this->printerYuLanWidget" << this->printerYuLanWidget->width()<< " "<<this->printerYuLanWidget->height(); printTest(this->printerYuLanPainter,this->printerYuLanWidget); return true; } } return QWidget::eventFilter(watched, e); }
printTest函数为绘图函数。