Display required fields - qt

Is there any class for highlighting required fields in Qt? For C# there's ErrorProvider. Does Qt has anything similar?

For C# there's ErrorProvider. Does Qt has anything similar?
Not that I'm aware of.
Probably the easiest way is to use QWidget::setStylesheet() to set a background (or whatever) to highlight the required fields.
Adding an icon next to the required field -- a red asterisk, say -- would be fairly straightforward: create a Field class using a horizontal layout with a required-field icon widget, a label and a 'field' widget, and give it a setRequired(bool) function.
I guess you could also use QStyle to create a custom widget.

Related

What's the NSView version of NSCell's highlighted property?

I'm migrating a cell-based NSTableView to be view-based. With NSCell, to determine if a cell was highlighted (e.g., to draw the text in white instead of black), I looked at the NSCell highlighted property.
What's the NSView version of this? I can't find anything like this in the docs.
The easiest way to do this is to simply subclass NSTableCellView. All the documentation says that you can subclass either NSTableCellView or NSView, e.g., Table View Programming Guide for Mac:
Drag an NSTableCellView object (or a custom view) from the object library to the appropriate column in the table view. ... Typically, the view class is a subclass of NSTableCellView.
It doesn't say what this is, or why you'd want to use it. It looks like an NSView which has an NSTextField and an NSImageView, and that's it -- so if you're not making a view that has these, it's tempting to ignore this class and just subclass NSView.
Interestingly, though, if you have any NSTextFields in an NSTableCellView (even if you don't use the textField property for this!), they automatically use the correct light/dark coloring.
In particular, it seems that the backgroundStyle property of NSTableCellView is what causes the text value to actually change. The documentation says:
The default implementation automatically forwards calls to all subviews that implement setBackgroundStyle: or are an NSControl, which have NSCell classes that respond to setBackgroundStyle:.
NSTextField is an NSControl with an NSCell, of course, so it gets this called on it.
While it's not exactly clear in Apple's documentation (what does "this" refer to?), it seems that NSTableView calls -setBackgroundStyle: on any view that defines it. So if you don't want to subclass NSTableCellView, you can alternatively just add a property to your own NSView:
var backgroundStyle: NSBackgroundStyle
and have your drawing code use that.

How to make a QSlider read-only?

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.

Editable QTabWidget

I want that user could rename tabs: clicking on current tab label should turn on editable mode.
The only way I can see is to use stacked layout + scrollarea + custom widgets + stackedwidget.
Can I do it with standard QTabWidget?
And one more question. Can I create custom pseudo states (like predefined :hover, :selected etc)?
You can reimplement QTabWidget to get access to QTabBar and use QTabBar::setTabButton method. But sometimes it is difficult to adjust position of inserted widgets ;)
You can't create custom states, but you can use custom properties. See Q_PROPERTY macro and related QSS syntax. Note, that styles will not updated on property change, so you should manually call polish/unpolish to force widget to use QSS.

is it possible to put a customize gtkwidget in a gtktreeview?

I'm learning GTK+ in this moment (after Qt) just to know what is possible with it thus Qt is for digia. Put a custom widget in a QListWidget is possible so I want to know if it is possible to do something like that using GTK+. An example of this widget could be one pixmap, one label and one button all in the same Cell and layout with a container like Gtktable.
Thanks
Yes, of course. You just need a "custom cell renderer":
http://scentric.net/tutorial/ch-treeview.html
http://gtk.php.net/manual/en/html/tutorials/tutorials.treeview.view.html

How do I tell Qt to always show an editor in a QTableView?

I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.

Resources