PDA

View Full Version : QDialog with qss-stylable subcontrols (title and close button)



Elder Orb
20th April 2008, 20:41
I need to create QDialog-based control with custom title and close button. Simple way is to add some methods for controlling title and close button, but I want to have a possibility to style subcontrols through qss ::title and ::close-button selectors, which are already defined for QDockWidget. I want to do something like the following:



MyDialog::title {
subcontrol-origin:margin;
subcontrol-position:left;
color: white;
.... etc....
}

MyDialog::close-button {
subcontrol-origin:margin;
subcontrol-position:right;
image: url(./img.png);
.... etc....
}


Is it possible at all ?

wysota
20th April 2008, 21:28
No, it's not. If you want to control the title, use QWidget::setWindowTitle(). As for the close button - it's part of the window manager and not your application so you can't modify it.

Elder Orb
20th April 2008, 22:07
Thank you for reply, but I'm speaking about custom title and close button. They are implemented by me and all I want is possibility to style them due qss as it is done with QDockWidget. As a temporary workaround I found possibility to "address" this control with their object names, for example


MyDialog#title {
.........
}

or
MyDialog#closeButton {
...
}


but this approach is less clear than MyDialog::title and it seems subcontrol-position and subcontrol-origin qss properties are not working, so question is still opened.
And I've got one more question: How to add :hover qss state to QWidget/QDialog subclass ?

wysota
20th April 2008, 22:39
You can't add your own style selectors to widgets. You can try to address QDialog:hover or MyDialog:hover but make sure first the widget receives hover events at all. But if the :hover state is not handled by QDialog, there is nothing you can do.

Elder Orb
21st April 2008, 07:28
Probably I can't. But I've already added ability to be stylable with
"border-image" and "border-width" qss properties, which are not allowed for QDialogs according to the docummentation. All I need to do was:



void MyDialog::paintEvent( QPaintEvent* event )
{
QStyleOptionFrame opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this);
}


So, I'll try to continue experiments just in case if solution is not harder than code above. Probably deep investigation of QDockWidget internals will help.

wysota
21st April 2008, 07:54
It's your time, not mine. If you want to waste it - go ahead. The above works because you ask the style to draw the primitive for you. If you'd like :title to work, you'd have to again ask the style to draw the respective primitive for you and so your title would look like QDockWidget's which is probably not what you want, especially that the title is not drawn by some widget styles at all.