QT: How to trigger "bold" upon mousehover of QMenuBar's items - c++

I am using Qt6 to create a cross-platform GUI application. I am playing around with the automatically generated QMenuBar object in Designer Mode. I want to change the QMenuBar items (File, Edit, Help.. ) to bold and red.
I am using the following code in the StyleSheet editor of the MainWindow object:
QMenuBar::item:selected {
font:bold;
color:red;
}
The code works fine for changing the color of the text but it does not set the font to "bold" as I would like. Also, trying to change the font size does not work.
What am I missing?
Thank you in advance!!

QMenubar does not have a font, you need to give the font separately to QAction. I think this will solve the problem
QMenu *fileMenu ;
QAction *newAct = new QAction(tr("&New"), this);
QFont f = newAct->font();
f.setBold(true);
newAct->setFont(f);
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
but if you want an active QAction then you need to
setStyleSheet("QMenu::item:selected {font: bold;}");

Related

Display ToolInfo on QT Window Title Bar

I am creating a window using QT with C++. The title of the window is a file name. I want to show(like tool tip) file path whenever I hover the mouse over the window title bar.
How do I do this using QT?
For adding ToolTip to your widgets or buttons you have 2 ways :
if you create your UI from the Designer section :
RightClick in your button or widget and choose Change ToolTip
Then write what you want as ToolTip and also you can change its style by adding cone as HTML
like this :
Create your widget and then use setToolTip function
pushButton = new QPushButton(this);
pushButton->setToolTip(QCoreApplication::translate("MainWindow", "<span style=" font-size:12pt;">This is ToolTip", nullptr));

qt5 multiline button with multiple style for each line

Can I make button, that have multiple lines, and each line have different style? When I use gtkmm, I can put widgets inside buttons, so I can make vbox with two different style labels and put it into button. But it looks, like I can't insert widgets into buttons in qt. Maybe there another way, to make multiline button with different style for each line?
Sorry for my English!
After all, I found a solution. I cat put layout of widgest just like in gtkmm. I just use method setLayout() of QPushButton. With this method I can put layout of multiple labels inside button.
QLabel* label = new QLabel( this );
QGridLayout* layout = new QGridLayout( this );
QPushButton* button = new QPushButton( this );
layout()->addWidget( label );
button->setLayout( layout );

Qt How to change stylesheet of the widgetContents-widget of every scrollarea in mainWindow?

In Qt I can not just style the QScrollArea (I only want to set the background color), I have to style the widgetContents-widget of every scrollarea like:
// qss code
QScrollArea #scrollAreaWidgetContents_1, #scrollAreaWidgetContents_2, ...{
background-color: MYCOLOR;
}
Question: How do I have to set up the stylesheet of mainWindow, that the widgetContents-widget of every scrollarea changes its background color without calling them all manually like in the example? Thanks for answers!
If you don't use the widget's object names somewhere else, you could set a common object name for all of them:
auto* content = new QWidget();
content->setObjectName("scrollAreaWidget");
auto* scrollArea = new QScrollArea();
scrollArea->setWidget(content);
And then address them from the qss like this:
QWidget#scrollAreaWidget
{
background-color: white;
}
If that is no option, you can try subclassing QWidget and apply the style for your new class. I haven't tried this approach, but it seems, you might face some difficulties there.

How to make background of cell widget inside QTableWidget not selectable?

The answer https://stackoverflow.com/a/24909605/3125006 shows how to add a QPushButton into a QTableWidget cell. This works perfectly.
But then the background area around the PushButton is selectable. (Background turns into selection color when clicked). How can I prevent that?
The solution in Pogrammer_ARM's comment is a suitable workaround. With corrected case:
QTableWidget* tblSensors = new QTableWidget();
tblSensors->setStyleSheet("QTableWidget::item{ selection-background-color: rgb(255,255,255)}");

How can you modify a Qt stylesheet?

How can I modify an existing stylesheet?
For example: if I want to create buttons, which when pressed each modify a single aspect of the stylesheet. One button can insert a margin-left attribute of 10. Another button can make the background colors blue. Lastly, another button can round the corners. The trick here though, is that I dont want to store all the variables and rebuild the style sheet on each button press. I would like to have a simple this->setStyleSheet(this->getStylesheet()+"margin-left: 10px:") for example.
Is there any way to do this?
Here is the code in main.cpp
QWidget wdg;
QHBoxLayout hlay;
wdg.setStyleSheet("border:2px solid rgb(74, 74, 74);");
QPushButton btn;
btn.setStyleSheet("border-radius:5px;");
btn.setText("Hello");
QPushButton btn2;
btn2.setStyleSheet("background-color: rgb(190, 190, 190);");
btn2.setText("Hello");
hlay.addWidget(&btn);
hlay.addWidget(&btn2);
qDebug()<<btn.styleSheet();
wdg.setLayout(&hlay);
wdg.show();
setting and getting style sheet works with QString and so you can use + operator.