
Originally Posted by
rkurvakat
what Im trying to do is create a drawing( a simple line graph) that scrolls horizontally since it can grow larger than the display size.
Im confused if I really need to use QGraphicsView for this or can I just draw it on a QWidget using the paint(). Not really clear about this.
Also how can I make it scroll horizontally if I were to use either ?
thanks
Ive answered the above query myself with the below mentioned solutions for the benefit of others and also for the gurus to comment.
Now if I draw something more than the viewing area I get a scroll.
What I dont understand is , which one do I use, QGraphicsView or QWidget:: paintEvent() for drawing simple 2D graphics, for example a simple line graph with x and y axis.
What is the advantage/disadvantage of both ?
QWIDGET SOLUTION
================
Code:
class MyWidget : public QWidget
{
public:
MyWidget() { }
protected:
void paintEvent(QPaintEvent *event)
{
QPen pen(Qt::yellow,Qt::SolidLine);
QPainter painter(this);
painter.setPen(pen);
//painter.drawLine(20, 40, 1250, 40);
painter.drawRect(20, 40, 1250, 580);
}
};
int main(int argc, char* argv[])
{
QMainWindow mw;
QScrollArea s;
MyWidget w;
s.setWidget(&w);
// NONE OF THESE LINES MADE IT WORK. WHAT COULD BE THE REASON ?
//w.setBaseSize(1000,400);
//w.setMaximumSize(1000,400);
//w.setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
//s.setWidgetResizable(true);
mw.setCentralWidget(&s);
// THIS LINE DID THE TRICK, why not the above setBaseSize or MaximumSize ?
w.resize(1000,400);
mw.show();
app.exec();
}
QGRAPHICSVIEW/SCENE SOLUTION
============================
Code:
int main(int argc, char*argv[])
{
QGraphicsScene scene;
scene.addLine(50,40,1750,40, QPen(Qt::yellow, Qt::SolidLine));
QGraphicsView view(&scene);
view.setScene(&scene);
view.setBackgroundBrush(QBrush(Qt::black));
view.setInteractive(true);
view.setSceneRect(0,0,2000,400);
view.setDragMode(QGraphicsView::ScrollHandDrag);
view.show();
app.exec();
}