How to make a QSlider read-only? - qt

Using Qt 5.2.1
Is it possible to set a QSlider (doesn't matter if it's horizontal or vertical) to read-only that is user cannot change the value of the slider but only use it as an indicator of some sort? I was unable to find anything in the Qt documentation or the Qt Designer.
Example for application: displaying a binary state of some sort in the GUI (in my case is the emergency stop on or off).

AFAIK such feature is not available in the QSlider implementation.
However, you can create your own class deriving from QSlider and implement the desired behavior by overwriting mousePressEvent, mouseReleaseEvent, mouseMoveEvent, keyPressEvent and keyReleaseEvent and only call the respective parent implementation if the readOnly property is set to false.
Luckily, such an implementation is already available in kalarm, so have a look at it: http://api.kde.org/4.6-api/kdepim-apidocs/kalarm/lib/html/slider_8cpp_source.html

Maybe a QProgressBar would be more suitable since users know it as "read only" and "shows how much has been done".

Following kuba ubar's second approach -
Suppose the object name of your slider is horizontalSlider. Then the code should be
// getting the palette of the slider
QPalette _sliderPalette = ui->horizontalSlider->palette();
// changing the colorGroup of that palette
_sliderPalette.setCurrentColorGroup(QPalette::Active);
// setting the changed palette to the slider
ui->horizontalSlider->setPalette(_sliderPalette);

One simple solution would be to install an event filter on the slider that consumes all mouse, focus and keyboard events. You'd also need to make the slider have a Qt::NoFocus policy. Such an event filter would be universal and could be used with any control widget.
An alternative would be to disable the widget, and style it so that the disabled and enabled palette are the same. This might not work with some of the platform styles, though, and would need experimental verification before you commit to it.

Related

How to intercept / modify excape key functionality in Qt

I am trying to fix a bug in a Qt app which I did not write. The window changes the background color of the entire window to red and puts up some buttons, dialog boxes, etc. When the escape key is pushed, the boxes and buttons go away, leaving an empty red screen. The Cancel button does the right thing in returning to the previous window. I think I need to somehow be notified of when the escape key is pushed, and then call the same function as the cancel pushbutton does. Hopefully, I can limit the scope of this special action to when the problem window is up. I am an experienced programmer but a complete Qt newbie. This app is purely C++. To my knowledge, it does not appear to use any QML. I am still searching through the Qt online documentation, but any suggestions / examples are appreciated.
This depends a lot on your specific Qt version and setup of your application. That said, I'll take a shot at helping. In the class where you're trying to intercept the escape key press, assuming it's inheriting QObject, simply override the virtual function eventFilter. Inside your overridden instance of eventFilter, check that the QEvent type is a QEvent::KeyPress, and then check whether the key of that KeyPress is the escape key, and handle as needed.
Be sure that you pass the event out of your function, else you'll see your overridden function eat all events. If you explicitly want the event to be "eaten", simply do not return it from your function. For specifics and examples, check out documentation of QObject::eventFilter().

How to gray a group of widgets when they are disabled?

There is a QGroupBox full of widgets, and all of them need to be disabled and grayed out.
setDisabled(true) does disable them functionally, but they don't turn gray.
What is the easiest and most proper way to turn them gray?
This should be a standard operation: text turns gray so that users can easily see that they are disabled.
QWidget::setDisabled() is yet (another) slot for the QWidget enabled property.
From Qt doc.:
This property holds whether the widget is enabled
In general an enabled widget handles keyboard and mouse events; a disabled widget does not. An exception is made with QAbstractButton.
Some widgets display themselves differently when they are disabled. For example a button might draw its label grayed out.
(Emphasizing by me.)
How widgets are displayed depends on their respective rendering as well as the (default) QStyle which is used in the application.
Concerning custom widgets (classes derived from any "built-in" Qt widget) where paintEvent() is overloaded, the custom widget is itself responsible to render accordingly to different states.

How can I disable a Qt widget without changing its appearence

I have a Qt widget which has a layout and there are more widgets inside it. When I disable the widget the whole widget becomes little faded and no modifications are possible anymore. I want the features that comes with disabling a widget but I do not want it's appearance to change. Please let me know how this can be done.
Few ideas that comes to my mind:
Rather disabling widget, capture all the events on the widget and do nothing
Update style sheet for disabled state (not sure if possible)
1. Capture events
Use QObject::installEventFilter() and QObject::eventfilter().
Keep in mind the way Qt dispatch GUI events, in particular children get events first. So you need to install the event filter recursively on all widgets and watch for QEvent::ChildAdded.
2. Using stylesheets
This is a not a good solution. Stylesheets tend to break QStyle mechanisms which may lead to side effects.
3. Use a QPixmap
Hide all the child widgets, render them to a QPixmap and draw the pixmap in the paintEvent.

How to make app looks nice in Qt?

I want to know is it possible to make application fully skinned/styled in Qt I mean by that not only controls inside the application window but the mainwindow itself! like close button/maximize button/minimize button and the bar, and the mainwindow border!, is it possible to do that in Qt? and how to?
Yes it is possible. The best method in Qt is to use Qt style sheets. The Qt documentation has plenty of examples and you can style all the widgets, either by specifying a whole class such as QPushButton, or a single, named widget.
As for the items on the title bar, I'm not sure if that's possible directly, but you could certainly turn off the main tool bar that contains the minimise / maximise buttons and then implement and style your own widgets while replicating their functionality.
The second argument to the QWidget constructor is Qt::WindowFlags. You can use the flags to control the properties of a window. For example, pass the flag Qt::FramelessWindowHint to create a borderless window.
There are a few ways to do this in code. You can use the setWindowsFlag method from within your widgets constructor:
setWindowFlags(Qt::FramelessWindowHint);
If you are creating a custom widget, you can pass the flag to QWidget's constructor:
YourWidget::YourWidget(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint)
{
// ....
}
Or you can pass the flag when you create a widget:
QWidget *your_widget = new QWidget(parent, Qt::FramelessWindowHint);
There are also flags for the minimize button (Qt::WindowMinimizeButtonHint), maximize button (Qt::WindowMaximizeButtonHint), close button (Qt::WindowCloseButtonHint), and title bar (Qt::WindowTitleHint). With these flags, you must also set Qt::CustomizeWindowHint to disable the defaults as described in the documentation.
See the flags documentation for a full list and additional details.
As #Merlin069 stated, style sheets allow you to control the look and feel of the widgets within your application.

How to draw something in a tooltip?

I am trying to develop a functionality using Qt which I don't know whether it is possible to implement. Here is the requirement:
When the user hovers over a node (an object derived from QGraphicsItem), a window will be shown near the node, in the window there might be some histograms or buttons that can be clicked to show further information. When the mouse leaves the window, it will automatically close.
I tried to use a tooltip, because it can pop-up near the node and close when the mouse leaves it, but it can only show text. So, it still cannot work that way. I am wondering if there is another way to do this? I did lots of google search, but there is still no answer.
Thanks so much for helping me with this.
If you're ok with using a 3rd party library, Qxt provides a class that provides a tooltip that is QWidget based, which will let you use an arbitrary widget as the tooltip rather than just text.
See: Qxt::ToolTip
you don't have to use tooltip for your app
you can use or call widget or dialog, on hover mouse event
Please refer Qt Example EmbeddedDialog Example, It is advanced, But you can understand how hover Enter/Leaving events are working. I personally prefer don not create instance of Popupdialog for each item, create it if only nesessary. Otherwise create one dialog and pass its reference to all the items through the constructor initialization.
1. These are the API you are intrested on, reimplemet this.
QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) and void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2. When You create Dialog, You can pass Qt::WindowFlags as Qt::ToolTip.

Resources