How to change QPushButton icon using stylesheets in Qt app - c++

Is it possible to set and change the icon on a QPushButton using stylesheets?
I need this for a Windows-based white-label Qt4.5 application that customers stylize using stylesheets.
Thanks.

A little late for a reply, but in case anyone else pops on this via google;
You can add the icon by changing the QAbstractButton-property. Simply use:
QAbstractButton {
qproperty-icon: theme_url("/button_action/add");
}
The type-selector (QAbstractButton), enables you to set the style for the selected object and all its subclasses. Since QProcessButton extends the QAbstractButton, this will do the trick. (Of course you could also use the QPushButton type-selector here).
For more information on how to use selectors, I found this quite helpful.
If you want to learn more about the button-properties, refer to the QT-Documentation here.
PS. In case you want to change the icon, once the button is clicked, use
qproperty-icon: url(":/Icons/before_click.png") off,
url(":/Icons/after_click.png") on ;

Yup.
border-image: url(/url/to/image);

You can use:
image: url(/url/to/image);
See http://doc.qt.io/qt-5/stylesheet-reference.html#image-prop and http://doc.qt.io/qt-5/stylesheet-customizing.html#box-model.

Related

QDialog with ok and cancel buttons

I need Qt Dialog with ok and cancel buttons with standard functionality, placed on the right side of its layout. I need to inherit from it and add other widgets to its layout. I can implement it myself, but maybe there is something standard, in that case I prefer to use it, since it will be more portable.
QMessageBox shows a message, I need something more general, only QDialog and standard buttons, or maybe QDialog has an option which activates them.
Subclass QDialog and use a QDialogButtonBox for the standard buttons (docs).
In case you use QT Designer, learnpyqt.com has this nice tutorial in which they describe how to build a QDialog with multiple input fields.
QT Designer can be downloaded as described here on stackoverflow in a comment.

QT4 C++ GUI Design - Tabbed interface alternative

Designing an interface with QT4 I have been advised that using multiple "Tabs" at the top of an interface to navigate different pages is not the most elegant design. Instead I would like to do something similar to the Options dialog in QT Creator 4.8.
Does anyone know the name of a widget that resembles that in the Options Dialog of QT Creator.
ex. Click Tools->Options... In QT Creator.
Notice the layout on the left hand side of the screen, which to me seems nicer than only tabs across the top of the screen.
Thanks for your help!
It's QLiveView/QListWidget with delegate that draw icon and string.
Here is link to documentation for QListView, If you are not happy with default list view delegate then you can create your own QItemDelegate to handle custom drawing. Here is sample code to create custom QItemDelegate
I just stumbled upon this implementation of a ribbon interface this morning. Maybe this is helpful to you?
http://qt-project.org/forums/viewthread/4214

Creating a tree-view with buttons? in QT

I am trying to make a dialog box like below in QT, the only problem is I have no idea what the widget is called. The bar on the left is like a tree-view widget, but when you click on it, it updates the text on the right. Does anybody happen to know what the widget is called or what widget(s) are required to perform this? I am using QT C++ on Windows.
There is an example with Qt showing you how to do this.
https://doc-snapshots.qt.io/4.8/dialogs-configdialog.html
If you're using Qt Creator as IDE, you can find it under the "Demos and Examples" tab in the Welcome Screen too.
It uses a QListWidget for the selector, and QStackedWidget to control the different pages. Connect the currentItemChanged signal of the list widget to change what page should be shown. Everything you'll need is in configdialog.cpp.
If you realy need to add QPushButton into QListWidget, use setItemWidget, or into ListView use QAbstractItemView::setIndexWidget

Changing the color of a Push Button in QT Creator

How do I change a buttons color?
I have found ways to do it by writing
button->setStyleSheet("* { background-color: rgb(255,125,100) }");
in Ui_Window.h
But everytime I qmake, the Ui_Window.h gets remade and I lose my colors.
Does anyone know how to permanently keep the colors of the button? I am working with QT Creator. If someone could direct me =D
Thanks so much!
Easiest way would be to use a style sheet on the button:
backgroundColourButton->setStyleSheet("background-color: red");
Aero, take into account that you MUST not modify Ui_Window.h file since it is generated by Qt Designer. So, every time you recompile the .ui file, this header file will be overwriten. As I see, you are using Qt Designer to add buttons to the layout. In this case, you can right click on a widget (or in the main dialog) in Qt Designer and click then in 'Change layout...'. On there, you can do something like:
QPushButton {
background-color: rgb(255,125,100);
}
for all buttons, or for an specific button:
#nameOfTheButton {
background-color: rgb(255,125,100);
}
Tell me if this works for you. Cheers,
If you use your form only in one class, you may add your expression there, after calling to ui->setupUi() in it.
Or even add stylesheet directly to form, look for property 'StyleSheet' in properties view of Qt designer.

Is there a way to prevent the hide operation of a toolbar?

In Qt, if I right-click on a toolbar the menu will be shown that allows me to hide the toolbar. I need to disable this functionality because I don't want the toolbar to possible to hide. Is there a way to do this?
I was able to set the ContextMenuPolicy directly on the toolbar (not the main window), as long as I used either Qt::PreventContextMenu or Qt::ActionsContextMenu. Prevent eliminated the context menu and made right-click have no effect on the toolbar, while Actions made a nice context menu composed of the actions already in my toolbar. Qt::NoContextMenu didn't seem to have any effect.
toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
Use setContextMenuPolicy (Qt::NoContextMenu) for the main window of the toolbar.
There are several ways to achieve this without having to alter the contextMenu functionality. See the following 3 PySide examples:
1. Disable the toggleViewAction of the QToolBar:
UnhidableToolBar = QToolBar()
UnhidableToolBar.toggleViewAction().setEnabled(False)
2. Connect to the visibilityChanged signal:
toolbar.visibilityChanged.connect(lambda: toolbar.setVisible(True))
3. Subclass QToolBar and use the hideEvent:
class UnhideableQToolBar(QToolBar):
def hideEvent(self, event):
self.setVisibile(True)
Recommendation:
While 2 & 3 are pretty dirty, solution 1 shows the toolbar in the context menu like a QDockWidget that has the feature DockWidgetClosable set. So either use solution 1 or if you want to remove the action have a look at Steven's answer.
Override QMainWindow::createPopupMenu() e.g.
QMenu* MyApp::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
filteredMenu->removeAction( mUnhidableToolBar->toggleViewAction() );
return filteredMenu;
}
Note that the other answers that suggest disabling the context menu will only work if you want to disable hiding/showing of all toolbars and all dock widgets.
Inherit QToolbar and reimplement contextMenuEvent().
The simplest thing to do is:
self.toolbar.toggleViewAction().setVisible(False)
Unlike self.toolbar.toggleViewAction().setEnabled(False), which still shows the disabled popup if you're right-clicking the toolbar for any reason.