Info needed about drawing and the Paint System
-
surprised that everyone is asking about the paint system.
http://doc.qt.nokia.com/4.7-snapshot/paintsystem.html
So far I haven't found out how to draw a line in a widget. I understand how to drop textbox widgets etc. I need to create these forms on the fly by reading an external file. So I'll need a form engine. What I have found so far is that QT wants to do drawline along with the painter.
what I've run across are things like this
@
#include <QtGui>class MyWidget : public QWidget
{
public:
MyWidget();protected:
void paintEvent(QPaintEvent *);
};MyWidget::MyWidget()
{
QPalette palette(MyWidget::palette());
palette.setColor(backgroundRole(), Qt::white);
setPalette(palette);
}void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkGreen);
painter.drawRect(1, 2, 6, 4);painter.setPen(Qt::darkGray); painter.drawLine(2, 8, 6, 2);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
@I know I can drop a scroller in and have but what handles the drawing updates as things come in view if it's possible to do lines and boxes without the paint system ?
[EDIT: code formatting, please use @-tags, Volker]
-
Not sure what exactly you are looking for ?
-
You are right, to draw real lines, you have to do your own painting. It was however not clear from your first question that this was the issue you were dealing with. Implementing your own widget like you did is an option then.
However, if you just want lines as separators, you might look into using QFrame. It offers multiple styles, including VLine and HLine.
-
I'd still prefer to do it in the paintEvent, but if you are averse to that one work around would be:
- Add dummy frames to parent widget
- Set position and size
This is what designer does when you add a Horizontal line for example ...
@
line = new QFrame(centralWidget);
line->setObjectName(QString::fromUtf8("line"));
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);gridLayout->addWidget(line, 0, 1, 1, 1);
@
edit: well Andre just beat me to it above .. but leaving my reply as is :)
-
vishweqeet ... you can stop reading and replying please.
Chetankjain. Thanks for your help and Andre.
It seems clear now that to do any real line drawing and for a scrollable form I need the paint system.
Another question would be ... I assume that you would have the paintevent in the widget your drawing on and draw to that widget rather than to the scroller.
I'm trying to catch up on reading about this stuff as fast as I can, all I can find is 4.0 books. I'm guessing that 4.0 and 4.1 is very similar.
thanks for all the help.
-
Your widget doesn't necessarily need to know about the scrolling. It just gets told which portion of itself to draw (event->rect()).
What goes for 4.0 goes for 4.1 as well. Some convenience things may have been added in the mean time though.
-
Don't quite get this, does this mean that a widget will automatically throw in scrolling ?
-
No. It is QScrollArea that throws in scrolling. However, usually when scrolling is required, you only draw a portion of a widget. The widget only gets told to draw a certain portion of itself through the paint event. It is not (necessarily) aware of scrolling of any kind.
That means that you have your widget's paint event:
@MyWidget::paintEvent(QPaintEvent *e)
{
QRect paintRect = e->rect();
QPainter painter(this);
painter.fillRect(paintRect, Qt::blue); // whatever you like
// do some more painting if necessary
}@And the QScrollArea around it:
@MySuperDuperProgram::MySuperDuperProgram(QWidget *parent)
{
// ...
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setWidget(new MyWidget);
}@QScrollArea takes care of scrolling, your widget takes care of whatever it is supposed to take care of.
-
Ok, I basically get it.... You create a widget for drawing, give it a paintevent for painting when needed. Hook it up to a Scrollarea. And the scroller works automagically (as it should) on the widget.
Sound good.
thanks
-
another question if you don't mind. If I drop a Scrollarea onto a Widget in QT Creator. It it automatically hooked up to the widget or do I need to do that in code ?
-
very interesting...
-
[quote author="Nevering" date="1299016007"]another question if you don't mind. If I drop a Scrollarea onto a Widget in QT Creator. It it automatically hooked up to the widget or do I need to do that in code ?[/quote]If you drop a scroll area on a widget, that widget has a scroll area, but the widget is not automatically scrolled. You would have to add the widget to the scroll area.