I would like to customize the look of QTabWidget tabs themselves (the tabs that we actually click to switch to another tab widget) by changing their height and width (which on OS X look more like pushButtons).
How to achieve this?
from PyQt4 import QtGui
app=QtGui.QApplication([])
dialog = QtGui.QDialog()
dialog.setLayout(QtGui.QVBoxLayout())
tab_widget = QtGui.QTabWidget(dialog)
dialog.layout().addWidget(tab_widget)
tab_widget.addTab(QtGui.QWidget(), "First")
tab_widget.addTab(QtGui.QWidget(), "Second")
tab_widget.addTab(QtGui.QWidget(),"Third")
dialog.show()
app.exec_()
On OS X, the size of these elements is fixed. They will lose platform styling when you attempt to resize them. Thus you will have to come up with full tab styling on your own: overriding even one attribute drops the platform style, which can't be adjusted, and reverts full control to you.
The styling is done via QSS (Qt Style Sheets), a CSS-lookalike. Here's an example, and you'll also want to consult the documentation.
Related
I can change the image or any other style of a QPushButton in the designer by changing the stylesheet, but any change in the style sheet changes all the other properties.
For instance, when I change the border image everything else is changed and it does not inherit the rest of the properties from the parent (or windows style).
How can I keep the windows style while changing only the image?
From How can I set Styles to a QTabwidget without overwriting existing styles in QT ?:
A style sheet is all or nothing. so if u all apply style sheet, it
might sort of reset the look for some widgets and to fix that, you
must supply a full stylesheet.
The key parts are:
A style sheet is all or nothing.
you must supply a full stylesheet.
You can set an image for the QPushButton from the code or by using QtDesigner like this:
Select the button
In the properties window set the image and the
size (properties icon and iconSize).
Did you try set style to the single push button:
your_push_button_pointer->setStyleSheet(
"QPushButton{background-image: url(:/images/call_up.bmp);}"
"QPushButton:hover{background-image: url(:/images/call_hov.bmp);}"
"QPushButton:pressed{background-image: url(:/images/call_down.bmp);}");
I want to tweak the existing style at runtime, specifically QStyle::PM_ToolBarIconSize. Can this be done? As far as I can tell, you can only inherit QStyle and override pixelMetric().
Edit 1: In the specific case of QStyle::PM_ToolBarIconSize, I could use QToolBar::setIconSize, but that would set the icon size for just a single toolbar instance. I want to change the underlying style hint to affect all toolbars everywhere with one fell swoop. And QStyle::PM_ToolBarIconSize may not be the only style I want to tweak, it's just the first one I'm looking at that just so happens to have a "change this instance's icon size" function.
Edit 2: I can't just make a new style subclass because the current style is already a custom style based on style sheets. There are several styles that a user can choose from. I don't want to create a bunch of new styles just so I can tweak a couple of toolbar icon or menu height size settings.
This is the exact purpose of QProxyStyle.
Why not overriding QStyle then? Your subclass would return an icon size (via pixelMetric) which depends on a settable parameter of your QStyle.
As Qt does not have a dynamic QStyle mechanism, it is better to create a new style instance with the changed icon size, then set it to the QApplication, rather than altering the current style.
My current GTK popups look like this - note it takes the dark ambiance colour theme.
In GTK3.8 and later there are GTKMenuButtons - the popup looks like this - note it looks like it uses the button styling cues.
I like this style and I want my application popups to look the same so there is a better look - integration and feel.
I know I can override the background colour of the popup using this snippet of python code:
style = button.get_style_context()
color = style.get_background_color(Gtk.StateFlags.NORMAL)
popup_menu.override_background_color(Gtk.StateFlags.NORMAL, color)
It looks like this if I apply the button background colour.
I've no idea how to apply the button font colour to the popup.
More importantly there is that annoying black border - 1px wide?
Thus to my question - am I attempting this the correct way (overriding theme properties) or can I somehow apply the CSS styling of one widget (the button or the button popup) to the popup so I can mimic the menubutton popup styling?
More information - the GTKMenuButton source gtkmenubutton.c doesnt have any theming controls for the popup, thus I'm at a loss how the menubutton popup gets its theme.
After further investigation I discovered that the style class of the widget (or container) affects the overall style of embedded objects.
Let me explain further with an example:
Construct a grid and attach the MenuButton containing the popup menu.
Adding the Toolbar StyleClass to the Grid influences all objects in that grid including the popup.
style = grid.get_style_context()
style.add_class(Gtk.STYLE_CLASS_TOOLBAR)
The result is this:
from gi.repository import Gtk
class MenuExampleWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Menu Example")
self.set_default_size(200, 200)
grid = Gtk.Grid()
grid.insert_column(0)
menu = Gtk.Menu()
mitem1 = Gtk.MenuItem(label = "Item 1")
mitem2 = Gtk.MenuItem(label = "Item 2")
menub = Gtk.MenuButton(label='menu')
menub.set_popup(menu)
menub.set_align_widget(None)
menub.show_all()
menu.append(mitem1)
menu.append(mitem2)
menu.attach_to_widget(menub, None)
menu.show_all()
style = grid.get_style_context()
style.add_class(Gtk.STYLE_CLASS_TOOLBAR)
grid.attach(menub, 0,0,1,1)
self.add(grid)
window = MenuExampleWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
Assuming you use gtk3+
gtk_widget_get_style/gtk_widget_get_modifier_style and gtk_widget_set_style/gtk_widget_modify_style should do what you want. Be careful, there is a builtin precedence for which style gets applied, which you can not modify (see DocBook entries for the above functions)
I have a QDialog I'm working with. It is made somewhat like a QMessageBox. I noticed that the size of the QMessageBox (and the size of its label) depends on the size of the message displayed.
How would I make the size of my QDialog adjust automatically like a QMessageBox? Presently my QDialog contains a button box and a label, and the QDialog is layout Vertical.
(I know I could just use the message box directly but eventually I will have more complex dialogs.)
Automatic solution:
Use layouts and set size policies to QSizePolicy::Expanding. In QtDesigner, once all your children are placed on your QDialog, then click on the Adjust Size button next layout ones. Your QDialog will be automatically resized at runtime.
Manual solution:
The QWidget class has a method adjustSize that resize the QWidget to fit its content. Just call it when all children are set.
Set your dialog to be expanding, and very small. Then, be sure to set your message before showing the dialog. When shown, it will try to find its proper size, based on the size of the objects it contains. (This happens recursively, so if the dialog isn't the direct parent of the label in which you show your message, make sure everything between the label and the dialog is set to use layouts.)
A TIP : if you try to use "adjustSize()" function when dialog is hidden, it may not be works fine. It would be better to use it after the "show()" function.
I've got the following class:
class SelectDateDialog(QDialog):
startDate = date.today()
endDate = date.today()
def __init__(self, text, isInterval = False):
QDialog.__init__(self)
uic.loadUi("resources/SelectDate.ui", self)
Now, the dialog is resizable on Mac OS X 10.5, but it shouldn't be. It has the resize-handle in the lower right corner.
I've already tried the setSizeGripEnabled function, it didn't change anything.
How can I make it not resizable?
if you want a non-resizable QDialog dlg,
then set
dlg.setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
I use the following code to fix the size of a QDialog:
layout()->setSizeConstraint( QLayout::SetFixedSize ) ;
setSizeGripEnabled( false ) ;
The first line enforces the layout size based on the preferred size of the widgets contained within the layout. The second line removes the actual grip.
To reverse this, you would set the default constraint on the layout and re-enable the size grip.
The cleanest way to make a window or dialog non-resizable is to set the size constraint of its layout to QLayout.SetFixedSize (or QLayout::SetFixedSize in C++). You only need to do this for the main layout in the window - the one which contains all the other widgets and layouts.
I see that you're using Qt Designer to create the user interface for your dialog. Open the .ui file and select the window, then scroll down in the property editor until you see the Layout section. Set the layoutSizeConstraint property to SetFixedSize.
When you preview the form, the widgets inside the dialog should be arranged correctly, but you won't be able to resize the dialog.
For whatever reason, I have always had to do the following (in Qt Designer) to be absolutely sure the window could not be resized:
sizePolicy -> Horizontal Policy = Fixed
sizePolicy -> Vertical Policy = Fixed
minimumSize -> Width = X
minimumSize -> Height = Y
maximumSize -> Width = X
maximumSize -> Height = Y
Note that I chose X and Y to illustrate that minimumSize == maximumSize. When you do this, the resize handle should disappear on its own, though I have seen at least one distribution of Linux leave the handle on. In that particular case, we just hid the handle, since it could not resize the window anyway.