Subclassing QMessageBox - qt

I need to customize QMessageBox. I need to remove the frame and title bar and add my own title bar and close button. Also need to replace the standard buttons and probably redo the background color of the box.
Is it possible to subclass it and achieve the above? is there any example anywhere for this? Or, should I just subclass Dialog and create my own message box?

This tutorial on custom windows might help you. It's in French but the code examples are in English, it shows how to compose your own title bar, create a window and attach the new title bar on it. I've been through it before, it's pretty straightforward once you've done it.

There is no need to subclass QMessageBox or QDialog. You can pass a QMessageBox the parameter Qt::FramelessWindowHint to remove the frame and buttons. You can also use Qt Style Sheets to style the background of the QMessageBox as well as the buttons. Something like this should work:
msgBox->setStyleSheet("QDialog {background-color: red;}"
"QPushButton {background-color: blue;}")
I haven't tested this but it should work or be pretty close.

Related

QToolButton Space Between Text and Icon

I have a QToolButton. I wanted text and icon to be on it. I set button style by setToolButtonStyle(Qt::ToolButtonTextBesideIcon).
But icon and text are so close to each other. Is there a method to give some space between icon and text by css?
You can't. There is no such a property neither for QToolButton (nor for QPushButton). All the properties for these buttons are in the documentation.
One thing you can do is to create your own class inheriting from QToolButton and overriding the paintEvent(). In this function, you will manually place your icon.
This is the shortest solution, but if you're brave enough, there are longer paths, like creating your own button subclassing directly QWidget (but in this case, you will need to implement ALL its behavior).
You can achieve it although this is not the documented solution.
Just provide spaces in QPushButton text start. By Doing this, icon and text will automatically go far from each other.

Modify button form in Qt

I'm new on Qt and I'm looking for some way to modify the form of a QPushButton, but I didn't find something on the web.
Is it possible and if so, how can I do this?
Thx
If you want to change how the QPushButton looks, you can use the Qt style sheets. If you want to put an image on the button, you can take advantage of the "background-image" property of the QPushButton
QPushButton
{
background-image: url("alpha.jpg");
}
You can change other properties like "border", "background-color", etc. Your question needs to be more specific as to what visual elements you need. Go through the documentation, it will help you.
One way to go about getting a different for the QPushButton is to make the necessary image available. Get the necessary image and make sure it has a transparent background. Use could use other attributes of QPushButton like, "background-color: transparent" and without any border - "border: none".
Hope it helps!

How can I make a QMenuBar item appear over its QMenu

First of all, I'm fairly new with Qt and Qt Creator so go easy if this is a stupid question.
I was practicing using Qt Creator, playing around with css styles. In particular, I'm trying to get the menubar and its menus to look something like this (on Windows): http://i.stack.imgur.com/9lMnQ.png.
However, the closest I've been able to get so far is this: http://i.stack.imgur.com/5Nlen.png.
I've searched online to see if anyone has tried something like this but I wasn't able to find anything.
The only possible solution I can think of is if the menubar item (with no bottom border) could be rendered in above the menu, so that they overlap, covering its top border over the width that they overlap.
If that won't work or is impossible or whatever please do suggest any other solutions/workarounds/hacks.
Thanks in advance!
I think that the only good solution is to avoid any tricks and create a new widget:
Create a new class inherited from QWidget with Qt::Popup attribute.
Place a QMenu into a layout of the widget.
Get a position of QMenuBar item which is clicked using QMenuBar::getActionGeometry.
Calculate position of the widget and of the tab in the widget to be placed over the menubar item.
Customize form of the widget using QWidget::setMask to make it look like a rectangle with a tab.
Show your widget instead of QMenu.

Tooltips for QDockWidget close button & float button?

Is there a way to set a tool tip text for the close button & float button in a QDockWidget ?
As ixSci mentioned, using setTitleBarWidget() is a potential way of solving this problem. Having said that I was looking for a much simpler solution, ideally using QSS (Qt Style Sheets). So after digging into the source code of QDockWidget I found an alternative way which suits my requirement better.
I wanted to find the place these float and close buttons are created. That is inside QDockWidgetPrivate::init() method in QDockWidget.cpp.
As for an example, the float button is created like this:
QAbstractButton *button = new QDockWidgetTitleButton(q);
button->setObjectName(QLatin1String("qt_dockwidget_floatbutton"));
QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_toggleTopLevel()));
layout->setWidgetForRole(QDockWidgetLayout::FloatButton, button);
Now all I need is to use the flexibility of Qt Style Sheets, for that I need only the Object Name, in this case it's "qt_dockwidget_floatbutton"
So all you need to do, to set tooltips for Close and Float buttons of a QDockWidget, is to add following two lines of styles in your application style sheet
QAbstractButton#qt_dockwidget_closebutton{qproperty-toolTip: "Close";}
QAbstractButton#qt_dockwidget_floatbutton{qproperty-toolTip: "Restore";}
You can implement whatever title widget you want and set it with setTitleBarWidget(). In that widget you can add whatever buttons with tooltips you need.

How to make all tabs in QTabBar not been selected?

How to make all tabs in QTabBar not been selected?
Thank you.
#ilyabvt at forum.crossplatform.ru suggested to create first fake tab and use Qt Style Sheets to make it's width = 0:
QTabBar::tab:first {
max-width: 0px;
}
This approach works fine with QTabBar integrated in QTabWidget. But unfortunately doesn't work with single QTabBar (look the screenshot below). Maybe it is my fault, but i cant find out where the mistake.
I think it's supposed to be like radio buttons, i.e. there is always one selected. If none of the tabs are active perhaps the whole bar should be disabled.

Resources