Problems with Open-Source Downloads read and

Info needed about drawing and the Paint System

  • I want to be able to draw say a form, perhaps a tax form. Straight lines, Text, and Textboxes all taking strings in and then converted.
    I'd like to be able to scroll, size the form, meaning clipping and redrawing as things come into the view space. I was wondering if it's mandatory to the paint system to do this or can this be done without the paint system ?

    Dumb question I know, but need to start somewhere.


  • Paint sytem?
    You won't have anything to do with the paint system. Qt provides a widget for you that will get you the scrolling you need: QScrollArea.

    Also, look into QFormLayout. It might be useful to make the form look the way you want and keep it conforming with your platform.

    Edit: Tip: Next time, please use the subject line of your question for something more informative than telling us you're a newbie. Something related to the content of actual question.

  • If you want to draw custom content within form and to that u r referring as paint system ? then you can skip that by using Qt webkit module and keeping simple html form.

  • Qt comes with a tool called designer.
    Invoke it and see how convenient it is to drag and drop available widgets like buttons and line edits into a form. The result can be further processed and compiled.

    There is a very small tutorial which lets you assemble widgets with code only (without designer):
    Try to work through it and the solution to your problem will become visible.

  • surprised that everyone is asking about the paint system.

    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

    void paintEvent(QPaintEvent *);

    QPalette palette(MyWidget::palette());
    palette.setColor(backgroundRole(), Qt::white);

    void MyWidget::paintEvent(QPaintEvent *)
    QPainter painter(this);
    painter.drawRect(1, 2, 6, 4);

    painter.drawLine(2, 8, 6, 2);


    int main(int argc, char *argv[])
    QApplication app(argc, argv);
    MyWidget widget;;
    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:

    1. Add dummy frames to parent widget
    2. Set position and size

    This is what designer does when you add a Horizontal line for example ...
    line = new QFrame(centralWidget);

        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.


  • 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.

Log in to reply