PDA

View Full Version : Qt::Popup with titlebar



keyur259
25th January 2010, 05:41
Hi,

I have a subclass of QWidget. And I have set windowFlags to Qt::Popup in the constructor. But when popup widget is displayed, it comes without titlebar. I have also tried Qt::Popup | Qt::WindowTitleHint. But it's still displayed without titlebar.

How can I get popup with title bar?

Please reply.

Thanks & Regards,
Keyur Shah

aamer4yu
25th January 2010, 06:21
With Qt::Popup you wont get the title bar.
Dont set any flags, and you will get the widget with a title bar. You can also use QDialog , or QWidget with Qt::Tool flag set,, whatever suits you.

keyur259
25th January 2010, 06:30
Thanks for your reply.

Oh... But none of other flags gives me what i wants.
QDialog are providing only application specific functionaliy.

I want my widget to close if user clicks outside the widget or better outside the application too.

So how can I get such things without using Qt::Popup flag?

aamer4yu
25th January 2010, 07:27
Well, if thats the case, I can think only of the following for now -
Draw the title bar yourself in the paintevent of your widget. And you can have the Qt::Popup flag set on your widget too :)

keyur259
25th January 2010, 07:36
Is there any method using which I can draw titlebar of widget by myself?

aamer4yu
25th January 2010, 11:00
QStylePainter::drawComplexControl(QStyle::CC_Title Bar,option);

keyur259
25th January 2010, 11:36
Still it is diaplyed without titlebar.

I have written following code in paintEvent

QStylePainter painter( this );
QStyleOptionTitleBar option;
painter.drawComplexControl( QStyle::CC_TitleBar, option );

Please let me know if I am missing something.

aamer4yu
25th January 2010, 11:52
try setting option.initFrom(this);
But I doubt if QStyle will permit you draw a title bar on a Qt::Popup widget.

You can try setting the QStyleOptionTitleBar::titleBarFlags and see if draws.

keyur259
25th January 2010, 12:00
How to set QStyleOptionTitleBar::titleBarFlags???

Do you mean set the widget->setWindowFlags(Qt::WindowTitleHint) ?

faldzip
25th January 2010, 12:22
What is the exact functionality you want and get with Qt::Popup and you can't get with other flags?

keyur259
25th January 2010, 12:25
With Qt::Popup, If I click anywhere including outside application, only the popup will be dismissed and no other window has any effect.

aamer4yu
25th January 2010, 12:48
I got it working by -

class PopupWithTitle : public QWidget
{
public:
PopupWithTitle(QWidget* parent =0);
~PopupWithTitle();
protected:
void paintEvent ( QPaintEvent * event ) ;

};

PopupWithTitle::PopupWithTitle(QWidget* parent) : QWidget(parent)
{
setWindowFlags( Qt::Popup);
setFixedSize(300,200);
}

PopupWithTitle::~PopupWithTitle()
{

}

void PopupWithTitle::paintEvent ( QPaintEvent * event )
{
QWidget::paintEvent(event);

QStylePainter stylePainter(this);
QStyleOptionTitleBar option;
option.initFrom(this);
option.text = "Popup title";
option.rect = rect();
option.rect.setHeight(20);
stylePainter.drawComplexControl(QStyle::CC_TitleBa r,option);
}



But yes, the window doesnt move, if you need that, you need to implement yourself :)

keyur259
25th January 2010, 12:55
Thanks.. I have got popup with titlebar.

I will try to implement dragging & coloring the titlebar with close button.

And will let you know if will get any problems.

If someone already know please reply.

aamer4yu
25th January 2010, 13:00
Hint :

QStylePainter::drawItemPixmap

QStyle::standardIcon and QStyle::SP_TitleBarCloseButton

mousePressEvent

keyur259
27th January 2010, 07:43
Hi, I have got titlebar & close button. And also able to drag it now using coding.

But I am unable to get text in titlebar. Shall we have to add control for it in paintEvent?

And color of the titlebar is not the same as other windows have. How can I get the general window titlebar color & set it for cuurrent titlebar?

aamer4yu
27th January 2010, 07:59
Did you try the code I posted ? I could get text in the title bar.

Anyways the functionalities you are adding, I guess it would be better to simply use a modal QDialog instead of so many turnarounds.

keyur259
27th January 2010, 08:02
I have used the code that you have posted using which I have got the titlebar without text. Then also I wil re-check.

The problem with modal dialog is It is only application specific modal.

keyur259
27th January 2010, 12:59
How can I get the general window titlebar color & set it for cuurrent titlebar?

wirasto
29th January 2010, 16:35
Hi, I have got titlebar & close button. And also able to drag it now using coding.


Wanna share your code how to add close button ? And maybe drag function if you finish it.

keyur259
30th January 2010, 05:23
For Close Button, Title Bar With Text & PopUp with Frame


void CustomPopUp::paintEvent( QPaintEvent* event )
{
QWidget::paintEvent( event );

QStylePainter painter( this );
QPalette tPalette = palette();
tPalette.setColor( QPalette::WindowText, QColor( Qt::white ) );

QStyleOptionTitleBar titleOption;
titleOption.initFrom( this );
titleOption.text = "Custom PopUp";
titleOption.rect = rect();
titleOption.rect.setHeight( 23 );
titleOption.titleBarState = (int)Qt::WindowActive | ~Qt::WindowMinimized;
painter.drawComplexControl( QStyle::CC_TitleBar, titleOption );

QStyleOptionFrame frameOption;
frameOption.initFrom( this );
frameOption.rect = QRect( rect().x(), rect().y() + 23, rect().width(), rect().height() - 23 );
painter.drawPrimitive( QStyle::PE_Frame, frameOption );

QIcon icon = style()->standardIcon( QStyle::SP_DialogCloseButton );
QRect r = QRect( 0, 0, rect().width() - 5, 23 );
painter.drawItemPixmap( r, (int)Qt::AlignRight | Qt::AlignVCenter, icon.pixmap( 19, 19 ) );

r = QRect( 0, 0, rect().width(), 23 );
QFont f = painter.font();
f.setBold( true );
painter.setFont( f );
painter.drawItemText( r, (int)Qt::AlignHCenter | Qt::AlignVCenter, tPalette, true, "Custom PopUp", QPalette::WindowText );
}

keyur259
30th January 2010, 05:29
For Dragging Functionality



QRect closeButtonRect( QRect( 553, 5, 15, 15 ) );
QRect captionRec( QRect( 0, 0, 575, 23 ) );
bool dragged( false );
QPoint dragPosition;

bool CustomPopUp::isPointInCloseButton( const QPoint& point )
{
return closeButtonRect.contains( point );
}

bool CustomPopUp::isPointInCaption( const QPoint& point )
{
return ( captionRect.contains( point ) && !( isPointInCloseButton( point ) ) );
}

void CustomPopUp::mousePressEvent( QMouseEvent* event )
{
if( event->button() == Qt::LeftButton )
{
dragged = false;
QPoint gPos = event->globalPos();
if ( isPointInCaption( event->pos() ) )
{
dragged = true;
dragPosition = gPos - frameGeometry().topLeft();
}
}
QWidget::mousePressEvent( event );
}

void CustomPopUp::mouseReleaseEvent( QMouseEvent* event )
{
QWidget::mouseReleaseEvent( event );

if( !dragged && event->button() == Qt::LeftButton )
{
if( isPointInCloseButton( event->pos() ) )
{
close();
}
else if( isPointInCaption(event->pos()) )
{
dragPosition = event->pos();
}
}
}

void CustomPopUp::mouseMoveEvent( QMouseEvent* event )
{
QWidget::mouseMoveEvent( event );
if ( dragged && event->buttons() & Qt::LeftButton )
{
move( event->globalPos() - dragPosition );
event->accept();
}
}

wirasto
30th January 2010, 14:32
The close button not automatic handle close event ?

aamer4yu
1st February 2010, 05:36
The close button not automatic handle close event ?

No, since you are drawing it yourself. Think of it as a normal button, do buttons handle clicked() signal on their own ?
If you thinking of QDialog, there also there must be handling to close dialog on close button. isnt it ?