Results 1 to 4 of 4

Thread: Painting on QMdiArea

  1. #1
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Painting on QMdiArea

    From some messages I've read, I believe it's possible to paint directly onto a QMdiArea, but obviously I am not doing it right. This is not surprising since my C++ skills are very limited.
    In Qt Creator I have added a QMdiArea called mdiArea_test inside a QWidget. I try to draw an ellipse and a rectangle + text within mdiArea_test with the following code, but it does not generate anything:

    QBrush brush;
    brush.setColor(Qt::green);
    QPainter painter(mdiArea_test->viewport());
    // do paint operations
    painter.setBrush(brush);
    painter.drawEllipse(20,20,10,10);
    painter.setPen(Qt::black);
    QRect rect = QRect(0, 0, 20, 20);
    painter.drawText(rect, Qt::AlignCenter, "Data");
    painter.drawRect(rect);
    painter.end();

    I would be grateful if somebody could show me my mistake(s).

    Thanks
    Gib

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Painting on QMdiArea

    If this code is anywhere except inside the paintEvent() for the class you have derived from QMdiArea, it will have no effect. The only time you can paint on a QWidget of any sort is when you are inside the QWidget::paintEvent().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting on QMdiArea

    I was wondering about that. I am not using paintEvent(), mdiArea_test was added to my form from the menu in Qt Creator. This is a convenient way to define an area to paint within. I'm looking for the simplest way to add some simple 2D graphics - is there a way to paint to an intermediary that is somehow attached to mdiArea_test?
    Thanks for your help.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Painting on QMdiArea

    is there a way to paint to an intermediary that is somehow attached to mdiArea_test?
    Only if "mdiArea_test" or the intermediary is your own class, derived from QWidget, and you do the painting inside the paintEvent() for that class.

    The only exception to this rule is if you are "painting" to a QBitmap, QPixmap, or QImage. This can be done anywhere, but in order to get it to appear on the screen is to paint the bitmap in a paintEvent() using QPainter::drawPixmap().

    You can also construct a QPainterPath anywhere, but again, in order to show it on screen, it hase to be done in a paintEvent() using QPainter::drawPath().

    For example, you could create a QPixmap, use QPainter to draw on it, and then set that pixmap on a QLabel. QLabel::setPixmap() Setting the pixmap causes the label to repaint itself, which it does in its own paintEvent(). So if you can use a QLabel as your "intermediary", then that is one way to accomplish painting outside of a paintEvent().

    Basically, in all modern graphics systems there is no more "immediate mode" painting where you can draw onscreen any time you want. Everything that occurs is under the control of an "event loop", and it is a paint event inserted into the list of things to be processed by the loop that determines when the display changes.
    Last edited by d_stranz; 4th October 2018 at 16:09.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Image into QMdiArea (again)
    By vcp in forum Qt Programming
    Replies: 6
    Last Post: 2nd October 2010, 14:31
  2. QwtPlot3d and QMdiArea
    By YaK in forum Qwt
    Replies: 0
    Last Post: 17th March 2010, 04:53
  3. QMdiArea with Gridlayout
    By ericV in forum Qt Programming
    Replies: 0
    Last Post: 14th September 2009, 11:26
  4. QMdiArea
    By Programm3r in forum Qt Programming
    Replies: 0
    Last Post: 4th May 2009, 14:25
  5. Not updating while painting in QMdiArea
    By rippa in forum Qt Programming
    Replies: 18
    Last Post: 14th April 2008, 16:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.