Removing the title from a QGroupBox - c++

Is it possible to completely remove the title from a QGroupBox? If you just give it an empty title, the label where the title would be still takes up space. It looks like this:
But I want it to look like this instead:
I tried the following things without success:
Setting the title font size to zero
Giving the title a size of zero in the stylesheet via setStyleSheet("QGroupBox:title{ max-width: 0; max-height: 0; }");
Moving the title inside the box via setStyleSheet("QGroupBox:title{ subcontrol-position: center center;}")
Note: You might say that a group box without title is a use case for QFrame. The problem with this is that I want to mix groups/frames with and without title, but the frame has a different visual style than the group box. So if you could tell me how to make QFrame look like QGroupBox that would solve my problems too.

You could use:
setStyleSheet("QGroupBox{padding-top:15px; margin-top:-15px}")

What you see is a bug of the particular style you're using. This pretty much "works" without you having to do anything special on other common styles (e.g. Mac, Windows). If you insist on using the particular style in question, you'll have to patch it yourself and either copy the style into your project, or build entire Qt to have a fixed version.

Related

Hide label text for Qt tabs without setting text to empty string

I need a QTabWidget with icons only:
How can I hide the label text of a tab in Qt? I cannot set the text to an empty string (""), as I am using docked widgets ( QDockWidget ) and the label text is set automatically (and I need it if the widget is floating).
But in tabbed mode I just want to display the icons (of the tabs).
Possible approaches:
Font size to 0?
I need to create my own bar class and override the paint event as here
Anything easier / cleaner?
--- Edit ---
Ok, the "set window title to empty string, and reset it the original text" approach works. I am using the topLevelChanged signal for this. However, it has some drawbacks, as the empty text still occupies some space. Another issue, with the text the tooltip is gone, and I cannot set it back.
What I am currently trying is something in-between the "text empty" and Prasad Silva's approach. I try to identify the text label inside the tab and set its size to 0, then reset it. It's slightly different, but would keep the text intact.
Btw, I see a line on top of my tabs, any idea what this is (where it comes from)?
Edit: There seems to be no "easy way" (style sheet, attribute) for this, see Hiding bottom line in QTabBar
Maybe I will create the whole tab bar on my own, as the automatically generated stuff is just too hard to handle (agree with PS on this).
This can not be done easily. Use empty text.
The way I solved something like was to create a QDockWidget subclass that installed a QWidget subclass as the titlebar (via setTitleBarWidget). This gave me control over showing/hiding the text in the titlebar when the dock widget fires topLevelChanged, dockLocationChanged and visiblityChanged.
This is really a big hack to get around the fact that Qt has refused to expose a public API for the docking system. We have since moved on to a custom docking implementation due to these limitations.
If you do not want to see the text, you can set it to an empty text after saving the current text, and when you want to see it again, restore it from the stored variable.
I do not think there is anything in the API for this not so common case, which means you will need to do it yourself.
Now, you could claim that it is tedious to do for many widgets, but on the other hand, you could write a simple hash define or inline function to do this repetitive work for you, which would only result a one-liner call, basically, which you would need to use anyway when changing the state.

Using different fonts/attributes in QTextEdit

I have a problem with displaying a text to area with different attributes.
My project has a multi-threading build. I reach to GUI text area by using signal-slot mechanism. I put my texts to the text area like this;
addrMW->ui->printerArea->appendPlainText(command.Data);
I want to append my text to this area with different font, size, etc..
I'm using Qt Creator 2.7.2 / Qt 5.1. Could someone explain this to me with an example?
What you want is a rich text edit. Luckily QTextEdit is able to handle that. Check the acceptRichText property (which should be true by default).
Then the methods you're looking for are:
setCurrentCharFormat
setCurrentFont
setFontFamily
setFontPointSize
etc...
Then, instead of appendPlainText() you should use append() to add text to the QTextEdit. Also see this Q/A. As proposed in the accepted answer, you can also use html formatted text instead.

Qt::How to lower the text in a QSpinBox

I'm using a spinbox with a custom font which looks too high in the spinbox. How do I move the text lower?
I have already reimplemented QStyle and made the font lower in another widget but I can't find where to do it with the spinbox. There must be a QRect somewhere where you can just move the top of it but I don't know and can't seem to find where it is.
Qt specifies a QStyle::SC_SpinBoxEditField, which appears to be what you want to modify. If I recall correctly from a few years ago when I was doing stuff with styles, you should be able to hook into getting options for that subcontrol, which would include the rect within which it is supposed to be drawn. Modifying that might get the result you want. If not, it is a place to begin searching for your answer.
This is more of a guess than a positive answer, but you might be able to do this with stylesheets:
spinbox->setStyleSheet("QSpinBox { bottom: -2px;}");
Ideally there would be a subcontrol or something for just the text, but the stylesheet documentation doesn't list one, which might imply the above will have undesirable consequences.
You can do:
spinBox->setAlignment(Qt::AlignCenter);//Or the Align Flag that you want
I hope this help.

Qt: How to show icon when item selected

I have a QListWidget containing items which have icons and when the items are selected the icon is just highlighted out. Is there a way to prevent this? I can't use stylesheets because it's for an embedded application and including them takes up too much space.
thanks
I suppose when you say "Highlithed out", you mean that the icon colors don't render well when the line is selected, and therefore, you can't see properly the icon...
Maybe you could consider using a different icon when the item is selected. It's possible to do so by specifing a mode to your icon.
Example :
QIcon MyIcon(":/images/foo");
MyIcon.addFile(":/images/bar", QSize(...), QIcon::Selected);
You can easily make a try in QtDesigner and see the results...
Hope it helps a bit !
Certainly, drawing on a black-and-white screen presents its challenges.
It sounds like you just want to change the appearance of the interface, not any functionality. If this is the case, a QItemDelegate-derived class (or QStyledItemDelegate) is almost certainly what you want. In particular, the drawDecoration function looks like it is used to draw an icon, and the style options should include whether it is selected. The simplest fix would be to override that function, set the selected flag in the options to false, then pass it up to the parent's function.

Default HTML style for controls in the Qt library

This is a question about Qt library, not about Web design.
For QLabel and other controls I can set HTML text, for example "<h3>Some Text</h3>". The question is: where is the default HTML style is defined? How can I find out what a font would be used for <h3> tag?
The next question: can I change the default HTML style?
Edit: I want to specify in one place in my code how my controls would be displayed. To specify css style in all the labels doesn't seem to be an elegant solution to me.
Edit2: It seems people don't get the question. I'll try again. Suppose I do the following:
QLabel* label = ...
label->setText("This <b>is</b> a <h3>Header</h3>");
The question: what fonts will be used for label text rendering? How can I control them? Is there a way to specify, say, default font size for <h3> headers?
Edit3: Thomi have suggested to use QTextDocument::setDefaultStyleSheet. But this is just a workaround. I have to manually apply css style to all the QTextEdits (and not QLabels) in the interface. And the question was: how to find out the default style sheet? QTextDocument::setDefaultStyleSheet just overrides it for a single QTextDocument object. Maybe QTextDocument::defaultStyleSheet returns it? I don't have a computer with Qt insatlled right now so I can't check it.
What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.
However, You can achieve this using a QTextEdit & QTextDocument.
Try something like this (I'm writing this from memory, so it may not compile or be 100% correct):
QTextDocument *doc = new QTextDocument(this);
doc->setDefaultStyleSheet("h3 { font-color: red; }");
QTextEdit *edit = new QTextEdit(this);
edit->setDocument(doc);
edit->setHTML("this is a red <h3>heading</h3>");
The important thing is to use a QTextDocument, which allows you to change the HTML stylesheet. From the QT documentation:
The default style sheet is applied to all newly HTML formatted text that is inserted into the document, for example using setHtml() or QTextCursor::insertHtml().
The style sheet needs to be compliant to CSS 2.1 syntax.
Note: Changing the default style sheet does not have any effect to the existing content of the document.
see here for more info
Edit:
To get the default stylesheet, you can call QTextDocument::DefaultStyleSheet() - however, this only applies to QTextDocuments, and may or may not apply to all Qt controls (including QLabel).
As mentioned in other answers you can do it for widget styles but not for HTML tags. The only way to do it is to set CSS styles inside your widgets text property individually.
Qt uses its rich text engine to render HTML tags which are defined according to rules in HTML 4 specification. See Supported HTML Subset
If you need just one style for all your labels why not to set it using setStyleSheet() like this:
MainWindow w;
w.setStyleSheet("QLabel{font-size:20px; color:purple;};");
Unless you want to use more than one style inside your labels (for example: "More than one style") that's the right way to do it.
Have a look at Qt Documentation about Style Sheets
You can probably use QApplication::setStyleSheet() or QWidget::setStyleSheet() to get it done.
In latest QT ie on QT4..The above answers are working.
Tell which QT version u r working on...
Try the following...
QLabel{
border: 2px solid green;
border-radius: 4px;
padding: 2px;
background-image: url(images/welcome.png);
}
link