Results 1 to 11 of 11

Thread: Displaying text on a MDI area background

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Displaying text on a MDI area background

    Hi

    I've got a quick question in regard with the Mdi Area in Qt. Is it possible to add normal text on the background? I've checked the MdiArea and QBrush documentation and it does not seem possible.

    Have anyone done this before?

    Thanks
    Jaco

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Displaying text on a MDI area background

    Open a painter on the viewport (either by providing your own or through an event filter) and paint the text you want using it.

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Displaying text on a MDI area background

    Thanks again for the reply Wysota.

    I've tried your suggestions, and I think I'm missing something small somewhere. I've re-implemented the paint event of the mdiArea using an event filter. However, the mdiArea still shows up blank (white background). I tried to use the example in the Qt documentation as shown below:

    Qt Code:
    1. if (event->type() == QEvent::Paint) {
    2. loggerSingleton::Instance()->addDebugString("Found paint event for mdiArea...",0);
    3. QPaintEvent *paintEvent = static_cast<QPaintEvent*>(event);
    4. QPainter painter(this);
    5. painter.setPen(Qt::blue);
    6. painter.setFont(QFont("Arial", 30));
    7. painter.drawText(paintEvent->rect(), Qt::AlignCenter, "Test 123...");
    8. return true;
    To copy to clipboard, switch view to plain text mode 

    Should I link my painter with the paint event somehow?

    Also, do you know of a good place to start learning the details of viewports? I've searched the Qt docs, but none of them explain the viewport specifically, unless I missed it...

    Thanks
    Jaco

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying text on a MDI area background

    Where are you writing the above code ?

    you could create a painter on viewport as -
    QPainter painter(myMdiArea->viewport());

    Hope am right

  5. #5
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Displaying text on a MDI area background

    The above code is in an event filter for the mdiArea. I'm sure the event filter is working, so the problem somewhere else.

    I've constructed the QPaintDevice using the QPainter constructor as you said, but still its not working...

    Qt Code:
    1. if (event->type() == QEvent::Paint) {
    2. loggerSingleton::Instance()->addDebugString("Found paint event for mdiArea...",0);
    3. this->updateMessagesAndLogs();
    4. //QPaintEvent *paintEvent = static_cast<QPaintEvent*>(event);
    5. QPainter painter(mdiArea->viewport());
    6. painter.setPen(Qt::blue);
    7. painter.setFont(QFont("Arial", 30));
    8. painter.drawText(10,10, "Test 123...");
    9. return true;
    To copy to clipboard, switch view to plain text mode 

    as far as I know this should work since the constructor calls begin() for you automatically. However when I launch the application and the paint event is called twice, the first time it steps through the code above without any problems, but the second time it give the following warnings:

    Qt Code:
    1. warning: QPainter::begin: Widget painting can only begin as a result of a paintEvent
    2.  
    3. warning: QPainter::setFont: Painter not active
    4.  
    5. warning: QPainter::end: Painter not active, aborted
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Displaying text on a MDI area background

    I take it that the mdi area installs an event filter on the viewport itself and it causes some interference with your event filter. I suggest taking a look into QMdiArea's source code to see what it does and do the same.

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying text on a MDI area background

    Is your code inside QWidget::paintEvent ???

  8. #8
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Talking Re: Displaying text on a MDI area background

    The code is in an event filter installed on the mdiArea. In the mainWindow's constructor:

    Qt Code:
    1. mdiArea->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool mainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (obj == this->mdiArea) {
    4. if (event->type() == QEvent::Paint) {
    5. loggerSingleton::Instance()->addDebugString("Found paint event for mdiArea...",0);
    6. this->updateMessagesAndLogs();
    7. //QPaintEvent *paintEvent = static_cast<QPaintEvent*>(event);
    8. QPainter painter(mdiArea);
    9. painter.setPen(Qt::blue);
    10. painter.setFont(QFont("Arial", 30));
    11. painter.drawText(10,10,"Test 123...");
    12.  
    13. QRectF rectangle(10.0, 20.0, 80.0, 60.0);
    14. int startAngle = 30 * 16;
    15. int spanAngle = 120 * 16;
    16. painter.drawPie(rectangle, startAngle, spanAngle);
    17. painter.end();
    18. return true;
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying text on a MDI area background

    what happens if u dont write painter.end() ??

  10. #10
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Displaying text on a MDI area background

    Same thing, I've just added it to see if it makes a difference.

    I've looked at the paintEvent in the mdiArea source, and there isn't anything really different there:

    Qt Code:
    1. void QMdiArea::paintEvent(QPaintEvent *paintEvent)
    2. {
    3. Q_D(QMdiArea);
    4. QPainter painter(viewport());
    5. const QVector<QRect> exposedRects = paintEvent->region().rects();
    6. for (int i = 0; i < exposedRects.size(); ++i)
    7. painter.fillRect(exposedRects.at(i), d->background);
    8. }
    To copy to clipboard, switch view to plain text mode 

    I can't see why it does not work. If I return true in the event filter it should filter the event shouldn't it? Thus, it should only draw whatever I draw in my event filter...

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Displaying text on a MDI area background

    What about:
    Qt Code:
    1. mdiArea->viewport()->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    ?

    Notice that you'll need to change the eventFilter() implementation accordingly:
    Qt Code:
    1. if (obj == this->mdiArea->viewport()) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 

    Much nicer approach is to subclass QMdiArea and reimplement paintEvent().
    J-P Nurmi

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. displaying text
    By me_here_me in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2007, 11:48
  3. displaying Text in QTAble dynamically
    By raghvendramisra in forum Qt Programming
    Replies: 8
    Last Post: 31st August 2007, 11:47
  4. displaying png image for a given area..
    By sar_van81 in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2007, 14:56
  5. Rich Text Tale Vol1 : Background colors
    By fullmetalcoder in forum Qt Programming
    Replies: 37
    Last Post: 9th February 2006, 16:05

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.