PDA

View Full Version : Custom dialog



simol
1st April 2009, 14:23
how can I make custom shape dialog - know i can use image to set fixed size (don't know how it will act during resizing), but i like to create this shape inside subclass of QDialog.
Please help.

talk2amulya
1st April 2009, 14:28
create your dialog with QDialog as base class, reimplement paintEvent() to create your shape. You'll have to use QPainter, read about it. It provides you with a lot of drawing capabilities with a lot of different shape. Also look at QPainterPath. Use resizeEvent() for your resizing operations. Read more about other events in QEvent .What image are you talking about? Revert back for more specific queries.

simol
1st April 2009, 14:49
Doing this i only create shape inside QDialog, right (if i'm worng i'm sorry but i'm pretty certain)? I want the dialog to be bounded by the shape and i also dont't want an active title bar to appear.

spirit
1st April 2009, 14:53
take a look at QWidget::setMask and also take a look at this example QTDIR/examples/widgets/shapedclock.

simol
1st April 2009, 15:10
Thanks for help. I acctualy used setMask but i didn't notice i can add QRegions :o (it was the key), anyway thanks again.

simol
1st April 2009, 15:34
I have couple more questions. How to create a QRegion to be roundedRect? And another thing: I would like this dialog to dissapear slowly (i decreas opacity of shapes i painted) on close button clicked. Painted shapes change their opacity but the region that bounds them doesn't so it doesn't look like i woudl like to. How can i do that effect (decrease region's opacity)?

spirit
1st April 2009, 16:06
this code creates round rect widget


QPixmap pixmap(300, 300);
QPainter painter(&pixmap);
QRectF rectangle(0.0, 0.0, 299.0, 299.0);
painter.setBrush(Qt::red);
painter.setPen(Qt::red);
painter.drawRoundedRect(rectangle, 20.0, 15.0);

QLabel *label = new QLabel();
label->setMask(QBitmap(pixmap));
label->setPixmap(pixmap);
label->setAttribute(Qt::WA_DeleteOnClose);
label->show();

spirit
1st April 2009, 16:19
here is an example with opacity animation (if I understand you correctly)


void Test::setLabelOpacity(int opacity)
{
m_label->setWindowOpacity(opacity / 100.0);
}

void Test::showLabel()
{
QPixmap pixmap(300, 300);
QPainter painter(&pixmap);
QRectF rectangle(0.0, 0.0, 299.0, 299.0);
painter.setBrush(Qt::red);
painter.setPen(Qt::red);
painter.drawRoundedRect(rectangle, 20.0, 15.0);

m_label = new QLabel();
m_label->setMask(QBitmap(pixmap));
m_label->setPixmap(pixmap);
m_label->setAttribute(Qt::WA_DeleteOnClose);


QTimeLine *timeLine = new QTimeLine(1000, this);
timeLine->setCurveShape(QTimeLine::LinearCurve);
timeLine->setFrameRange(0, 100);
connect(timeLine, SIGNAL(frameChanged(int)), SLOT(setLabelOpacity(int)));
timeLine->start();

m_label->setWindowOpacity(0);
m_label->show();
}

talk2amulya
1st April 2009, 17:48
u can have closeEvent() of your custom dialog like this to achieve that:


void CustomDialog::closeEvent(QCloseEvent *event)
{
QEventLoop el;
QTimeLine *timeLine = new QTimeLine(1000, this);
timeLine->setCurveShape(QTimeLine::LinearCurve);//u can change this curve shape to achieve different effects, read QTimeLine
timeLine->setFrameRange(0, 100);
connect(timeLine, SIGNAL(frameChanged(int)),this, SLOT(setLabelOpacity(int)));
connect(timeLine, SIGNAL(finished()),el, SLOT(quit()));
timeLine->start();
el.exec();
QDialog::closeEvent(event);
}

void CustomDialog::setLabelOpacity(int opacity)
{
setWindowOpacity(opacity / 100.0);
}

i havent tested it but it should work

spirit
1st April 2009, 17:52
yup, that will pretty cool. ;)

simol
1st April 2009, 18:25
Thanks guys, appreciate your help.