[SOLVED]Defining QMdiArea background



  • Hello folks,

    I want to add background image for QMdiArea but without tiling the image, how can I do that?

    PS



  • The following "FAQ":http://developer.qt.nokia.com/faq/answer/when_setting_a_background_pixmap_for_a_widget_it_is_tiled_if_the_pixmap_is_ suggests a way of achieving this by reimplementing the resizeEvent() and scaling the image.



  • Thanks a lot (I didn't notice that answer although I used search option).

    I modified the answer a little bit.
    @#ifndef MDIAREA_H
    #define MDIAREA_H

    #include <QMdiArea>
    #include <QResizeEvent>
    #include <QPainter>

    class MdiArea : public QMdiArea
    {
    Q_OBJECT
    public:
    MdiArea(/*QMdiArea clone, */QImage backgroundImage, QBrush backgroundBrush)
    {
    i = backgroundImage;
    b = backgroundBrush;
    }
    protected:
    void resizeEvent(QResizeEvent *resizeEvent)
    {
    QImage newBackground(resizeEvent->size(), QImage::Format_ARGB32_Premultiplied);
    QPainter p(&newBackground);
    p.fillRect(newBackground.rect(), b);

        QImage scaled = i.scaled(resizeEvent->size(),Qt::KeepAspectRatio);
        QRect scaledRect = scaled.rect();
        scaledRect.moveCenter(newBackground.rect().center());
        p.drawImage(scaledRect, scaled);
        setBackground(newBackground);
        QMdiArea::resizeEvent(resizeEvent);
    }
    

    private:
    QImage i;
    QBrush b;
    };

    #endif // MDIAREA_H@

    I tried to use that implementation for passing QMdiArea as a copy but it didn't' work because " QMdiArea::QMdiArea(const QMdiArea&) is private"
    @ui->mdiArea = new MdiArea(MdiArea(QImage(":/img/gnome-graphics.png"), Qt::red)); // mdiArea is QMdiArea object@

    How can I fix this issue?



  • Can you give some more details on what exactly you want to do? Have you created a QMdiArea in Designer and want to use it together with the QMdiArea subclass that was created in code? If so, you can simply "promote ":http://doc.qt.nokia.com/latest/designer-using-custom-widgets.html#promoting-widgets the QMdiArea in Designer to your subclass.

    Creating a copy of the QMdiArea is not possible. It is not possible to create copies of QObjects and their subclasses for the reasons outlined in this "FAQ":http://developer.qt.nokia.com/faq/answer/why_is_it_not_allowed_to_copy_qobjects. Instead, you can follow the advice listed "here":https://developer.qt.nokia.com/faq/answer/how_can_i_clone_a_widget if you need to clone a widget



  • [quote]Have you created a QMdiArea in Designer and want to use it together with the QMdiArea subclass that was created in code? If so, you can simply promote [doc.qt.nokia.com] the QMdiArea in Designer to your subclass.[/quote]
    Yes exactly but I couldn't find QMdiArea in base class drop list of Promoted widgets window!

    [quote]Can you give some more details on what exactly you want to do?[/quote]
    you can test my trial at [url=http://www.mediafire.com/file/9rebbbdumq982df/MDITest.zip]this link[/url] (it's tiny test don't be afraid).

    PS
    I'm wondering why these forums don't allow to post attachments this is really sucks.



  • [quote author="mbnoimi" date="1311689985"]PS
    I'm wondering why these forums don't allow to post attachments this is really sucks.[/quote]

    There were many discussions about that, but the result was, it is complicated due to legal issues.



  • Guys could you please help me... I couldn't fix this issue.



  • The QMdiArea is indeed not listed among the promoted widgets. I checked the reason for this with our developers here and QMdiArea is put in an exclude list since it is a container that it is problematic to promote. I see that you are investigating a way around this in this "thread":http://developer.qt.nokia.com/forums/viewthread/8334, and that sounds like a viable approach since QFrame is a container as well.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.