QToolButton with margin is clickable
-
So I Adjusted the margin-left with:
_ui.myToolBar->setStyleSheet("QToolButton { width: 37px; height: 37px; padding: 0px; border: 0px; margin-left: 16px;}");
But this happens: When I put the mouse in the black area:
The button enters a hover event and I can Click and perform the actions. I Thought that margin-areas were not clickable, How can I avoid this behavior?
-
Toolbar does some funky stuff and basically adds this margin to the toolbutton, but the mouse events are handled on the layout item area, which contains that margin.
To space items on a toolbar set spacing value for it instead of margins on individual items i.e._ui.myToolBar->setStyleSheet("QToolBar { spacing: 16px; }");
-
@Chris-Kawa For some reason spacing is not working for me..............
Is there something wrong about the code? Or maybe some other stylesheet is influencing this behavior?_ui.otherBar->setStyleSheet("QToolButton { width: 37px; height: 37px; border: 0px; padding: 0px; } QToolBar::separator {background-color: black; margin-top: 15px; margin-bottom: 15px;} QToolBar {background-color: green; spacing: 16px;}");
-
Works for me. What platform are you on? Have you tried stepping into the layout code to see what happens when the item geometry is calculated?
-
@Chris-Kawa I Have a class mainWindow, thats where I Have:
_ui.otherBar->setStyleSheet("QToolButton { width: 37px; height: 37px; border: 0px; padding: 0px; } QToolBar::separator {background-color: black; margin-top: 15px; margin-bottom: 15px;} QToolBar {background-color: green; spacing: 16px;}");
And In My QApplication I set a Stylesheet in the following order:
QFile css(QStringLiteral(":/stylesheet/my.css")); css.open(QFile::ReadOnly | QFile::Text); app.setStyleSheet(css.readAll()); css.close(); MainWindow *mainWindow = new MainWindow;
If I bring the
mainWindow
declaration to the top of the code, it works fine, but if it is left after theQAplicattion::setStylesheet
, the property spacing doesn't work. -
Since it changes geometry of the items after maybe it's another quirk. Try calling QStyle::unpolish and then QStyle::polish() with the toolbar.