PDA

View Full Version : Image into QMdiArea (again)



vcp
12th March 2009, 18:49
Hi people,

I did like to put an image (logo) in my MdiArea. Already I tried many methods but nothing worked.
The application main window was created with QDesigner and I create the mdiarea like this:

qBuilder::qBuilder(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl ), Ui::formQBuilder()
{
setupUi(this);
mdiArea = new QMdiArea(this);
setCentralWidget(mdiArea);

}

I try use:

QImage img("logo.jpg");
mdiArea->setBackground(QPixmap::fromImage(img));

but the logo is showd in tiled mode. Already I tried to use also QPaintEvent but also didn't works

Can someone help me to resolve this problem?

Thanks

Lykurg
12th March 2009, 20:46
qBuilder::qBuilder(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl ), Ui::formQBuilder()
{
setupUi(this);
mdiArea = new QMdiArea(this);
setCentralWidget(mdiArea);

}


Ok, that looks stange to me. Normally you set the QMdiArea already in the designer so only "setupUi(this);" is necessary (or how's your ui file) and then just set the background:


qBuilder::qBuilder(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl ), Ui::formQBuilder()
{
setupUi(this);
QImage img("logo.jpg");
mdiArea->setBackground(img);
}

Not proofed, but should work if "logo.jpg" exists.

Lykurg

vcp
13th March 2009, 12:52
Hi thanks for your answer, but the problem is that the image is shown side by side (tiled)

Lykurg
13th March 2009, 13:22
Hi thanks for your answer, but the problem is that the image is shown side by side (tiled)
Ah, sorry, have read to fast. So you have to subclass:

#ifndef MYQMIDIAREA_H
#define MYQMIDIAREA_H

#include <QtGui>

class myQMdiArea : public QMdiArea
{
Q_OBJECT
public:
myQMdiArea(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *);
};

#endif // MYQMIDIAREA_H
#include "myqmdiarea.h"

myQMdiArea::myQMdiArea(QWidget *parent)
: QMdiArea(parent)
{}

void myQMdiArea::paintEvent(QPaintEvent *paintEvent)
{
QPainter painter(viewport());
painter.fillRect(paintEvent->rect(), QColor(23,200,123));
QImage img("/home/lykurg/Desktop/icons/oxygen/16x16/apps/gimp.png");
painter.drawImage(QPointF(10,10),img);
}
Note: the paintEvent is not very nice, only an example!

Lykurg
13th March 2009, 13:24
ok, better in paintEvent:

void myQMdiArea::paintEvent(QPaintEvent *paintEvent)
{
QMdiArea::paintEvent(paintEvent);
// and now only paint your image here
}

vcp
13th March 2009, 17:15
Hi Lykurg,

Yor explanation was very good and simple.

Thanks a lot! You help

cia.michele
2nd October 2010, 14:31
Hello to everybody,
I'm a newbie of c++ and QT and i need to place an image in the center of a QMdiArea. I take your code but... how can I use it into QtCreator form? I tryed to promote a widget but QMdiArea can't be used as base widget :(, so I tryed to create plugin but it can't be attached (or I didn't be able to do it) to QtCreator widget list :((, and more, how can I change the image from there? How can I change your constant in a property or pass it from the upper class using it?

Thanks a lot for your time.

Michele

PS. I'm using 2010.04 QT sdk on Windows