Editable QTabWidget - qt

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.

Related

Sub-Controls for custom widgets in Qt

in Qt it is possible to use sub-controls to style widgets, that are part of another widget: http://doc.qt.io/qt-5/stylesheet-syntax.html#sub-controls
For example: With QDockWidget::close-button, I can set the style for a button in a QDockWidget.
Now I want to create my own button for a Widget. It is possible to call it in the qss-stylesheet with the sub-control syntax MyWidget::my-button, too how to set these sub-control command to the button?
best regards

Qt Squared Radio Button

I want to create a squared radio button, with the text inside the button. Is that possible? I think the button shape could be changed trough css, but what about the text?
Any hint?
Rather than trying to deform a QRadioButton into something which visually resembles a QPushButton, I would simply use a QPushButton with some custom logic.
You won't have to worry about the visual aspect then, while the logic itself is not all that hard to write.
As stated by #besworland, QPushButton inherits from QAbstractButton, which already has the option to be checkable or not. You can set this via setCheckable(bool).
To mimic the "exclusive" behaviour of a set of QRadioButtons, you can add your buttons to a QButtonGroup and make it an exclusive one. As stated in the documentation "An exclusive button group switches off all checkable (toggle) buttons except the one that was clicked." You can use a QButtonGroup's setExclusive(bool) method for that.
In any case, I would consider those easier options than transforming a QRadioButton to fit your needs.

How can I customize the appearance of the actions in my QToolBar?

I have just changed some toolbars from Q3ToolBars (with QToolButtons explicitly added to them) into Q4 toolbars (with actions added to them straight away instead.)
The old tool buttons had a nice outline around them, but this is not displayed in the new version; the QActions in the Q4 toolbar just look like a line of icons. Is there a way to change the 'button' style in the new version (assuming these actions can be considered as such) and give them the outline? I've looked through the QToolBar reference, but the toolButtonStyle() function only appears to work with whether you want to display icon, text, etc.
...Or will I have to just make actual tool buttons and/or QPushButtons and use addWidget()?
The widget associated with a given action is accessible through QToolBar::widgetForAction (since Qt 4.2). So, you can pass your actions to this method, get the QWidgets returned by it, convert them to QToolBar, and handle them like you normally would (code not tested):
// ...
auto toolButton =
static_cast<QToolButton *>(
m_ui.toolbar->widgetForAction(m_ui.my_Action));
// Will make the toolButton always appear raised:
toolButton->setAutoRaise(false);
// ...
As far as I've been testing, some methods might not work (i.e., QWidget::hide), so do your own testing.
Yes, of course you can edit look of QToolButtons in two different ways:
You can set it style sheet using void QWidget::setStyleSheet(const QString &)
You can reimplement QToolButtons class with new paintEvent function where you will be able to exactly set how your button should looks like.

Is it possible to style individual tab bars?

I would like to style individual tabs of the tab bar of a QTabWidget (not hover or current but an individual one) WHY? because I need to get the user's attention so that he knows urgent information appeared in that tab. There can be multiple tabs that need attention.
You would have to assemble your own tab widget but i think if you subclass QTabBar and use setTabButton(int index, ...) you can set a custom widget for the item in the tab, this would let you control the behaviour and you could change it through code or style it via the property selector e.g.
QTabBar MyButtonWidget[showalert=true]
{
background-color: red;
}
This seems a little strange. Why don't you start with that tab selected?
I don't think you can do that with stylesheets. You can easily customize first and last but not any tab (AFAIK)
http://doc.qt.io/archives/4.6/stylesheet-reference.html
On the other hand you can set custom icon to draw attention or use void QTabBar::setTabTextColor ( int index, const QColor & color )
You might want to try simply using stylesheets to directly style a tab instead of subclassing just for that purpose. In the stylesheet, you can use the :tab subcontrol to access a single tab, and you can then modify a dynamic propoerty to set its state as either needing attention or not, and applying the style based on the dynamic property.
See : :tab sub control , Customizing using dynamic properties

Display required fields

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.

Resources