Making QMessageBox InformativeText Bold and increase font size - c++

In my qt C++ gui application, I have a QMessageBox with 3 buttons and some standard functionality. I want to decorate all these with stylesheets and fonts and what not.
I am successfully able to do the following -
i. Set MessageBox background.
ii. Set Button background.
void MainWindow::on_pushButton_clicked()
{
QMessageBox msgbox;
msgbox.setInformativeText("YOLO");
msgbox.setStandardButtons(QMessageBox::Ok|QMessageBox::No|QMessageBox::Cancel);
msgbox.setWindowFlags(Qt::FramelessWindowHint);
msgbox.setStyleSheet("QMessageBox { background-image: url(:/res/bk.jpg) }");
msgbox.button(QMessageBox::Ok)->setStyleSheet("QPushButton { background-image: url(:/res/green_bk.jpg) }");
msgbox.button(QMessageBox::No)->setStyleSheet("QPushButton { background-image: url(:/res/red_bk.jpeg) }");
msgbox.exec();
}
But I am stuck on how to set a font (type and size) to the InformativeText field, and also how to make it Bold. I have referred to the InformativeText Properties but I am unable to sort things out. Any ideas on how to achieve this ? Thanks.
As an added query, can I change the fonts (type and size) of buttons as well and set Bold Italics etc ?
EDIT
I did the following changes to my code, and the results are baffling,
QFont font;
font.setBold(true);
msgbox.setFont(font);
After doing this, my informationText has become bold, my cancel button has become bold. But the Ok and No buttons are normal (non-bold). If I remove the setStylesheets for the two buttons, only then are they becoming bold (which is useless, since I want background images).
Any ideas to have a synergy in all these ???

I found solution for your problems.
QMessageBox msg(QMessageBox::Information,"Hey", "Dude", QMessageBox::Ok);
QFont font;
font.setBold(true);
msg.setFont(font);
msg.button(QMessageBox::Ok)->setFont(font);
msg.exec();

Related

Customize QMenu added to a QMenuBar

So I try, with Qt, to bold the fourth Menu Title "Test" in a QMenuBar (see picture below).
QMenuBar* pQMenubar = new QMenuBar();
pQMenubar->addMenu(new QMenu("Fichier")); //Some QMenu, not important
pQMenubar->addMenu(new QMenu("Windows")); //Some QMenu, not important
pQMenubar->addMenu(new QMenu("Tools")); //Some QMenu, not important
QMenu* pQMenuTest = new QMenu("Test"); // The Menu I wanna bold
pQMenubar->addMenu(pQMenuTest);
//There is some action added to each QMenu, but I do not want them bold
To achieve that, I've tried by setting StyleSheet or font to the QMenuBar or QMenu("test")
Set the font (setBold(true)) or StyleSheet(font-weighted: bold) to the QMenu do not work, it only set actions contains in the QMenu in bold.
But when I change styleSheet or Font of the QMenuBar, all QMenu and QAction are bold, so I've tried this way.
I've tried to "filter" the object affected by the style sheet :
//Try 1
pQMenuBar.setStyleSheet(QMenu {font-weight: bold}); //Nothing is bold
//Try 2
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(QMenu#Test {font-weight: bold}); //Nothing is bold
//Try 3
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(#Test {font-weight: bold}); //Nothing is bold
//Try 4, just to check if it's works
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(QWidget {font-weight: bold}); //Evrything is bold
I've started to suspect the QMenuBar.addMenu(QMenu) do not really use the QMenu for the "display" and so filter by QMenu or the objectname do not work. (I've tried to filter with QAction, but nothing is bold too).

qt set max height to a dropdown combobox (style cleanlooks)

I'm working on a software which contains combobox with a lot of items inside, the problem is when I click on it the list is too large and I can't see all of the items by scrolling on it.
The style cleanlooks is used (that's why the combobox have a too large size) but I can't change it so I'm looking for a solution to set a maximum size.
I found nothing about this on the web, neither in the documentation.
I tried to use the size policy but doesn't work. I also tried to get the QLineEdit used by the combobox and to set a QSize on it and finally to by setting stylesheet on the combobox and on the QLineEdit but nothing worked properly.
What I tried with the QSize and the stylesheet :
sz = QSize(20, 20)
combo.view().setGridSize(sz)
combo.view().setStyleSheet("""QListView { max-height: 50px; background-color: yellow; } """)
combo.setStyleSheet("""QComboBox { max-height: 30px; background-color: pink; } """)
EDIT: After the comment of #Vladimir Bershov I tried to set correctly the size with : setMaxVisibleItems() but as said in the doc ("Note: This property is ignored for non-editable comboboxes in styles that returns true for QStyle::SH_ComboBox_Popup") the property is ignored.
So I looked for modify the QStyle Hint to unset the SH_ComboBox_Popup but as explained on this post that's impossible.
If you have any suggestions I'm listening.
Thanks.
Like explained in the comments there's no available solutions in PyQt4
This line worked for me when I was trying to minimize the height of the QComboBox dropdown in Qt/C++.
ComboBox->setStyleSheet("combobox-popup: 0;");

QToolButton and color

I'm trying to make a simple design to select a color and I'm using a QToolButton to open a QColorDialog.
My problem is that I would like the QToolButton to be of the selected color but I've only suceeded to set the background color, which is not what I want.
Here is a picture:
My code:
QColor color = QColorDialog::getColor(m_couleur);
if (color.isValid()) m_couleur=color;
m_labelCouleur->setPalette(QPalette(m_couleur));
m_labelCouleur->setAutoFillBackground(true);
m_buttonCouleur->setPalette(QPalette(m_couleur));
m_buttonCouleur->setAutoFillBackground(true);
Basically I would like the QToolButton to look something like this:
edit: Can anyone explain to me why the website won't let me add "Hi everybody" at the beginning?
QColor color = QColorDialog::getColor(m_couleur);
QPixmap px(20, 20);
px.fill(color);
m_buttonCouleur->setIcon(px);
No CSS involved in this case is (for me ofcourse) big pro
Use the setStylesheet function in order to change the background color of your button
m_buttonCouleur->setStyleSheet(QString("QToolButton{ background: %1; }").arg(m_couleur.name()));
I've done exactly that by using a QPushButton and setting its style sheet to the result from the color picker. I guess a tool button should probably be the same.
button->setStyleSheet(QString("background-color: %1; "
"border: 1px; "
"border-color: black; "
"border-style: outset;").arg(color.name()));

Removing borders from QWidgets in a QGraphicsGridLayout

I currently have a QGraphicsScene that I am using with a QGraphicsGridLayout. I am trying to align QWidgets (QLabels and a custom graph QWidget) on this grid layout, and then export it to a QPrinter for pdf export.
The problem I'm having is that I have these grey divider lines between the QLabels that I can't seem to get rid of. I have tried settings spacing in the layout to 0, margins to 0, all the different properties of the QLabel palette, etc. all to no avail. Here's the relevant code:
main class:
QLabel lbl("some text");
lbl.setAutoFillBackground(true);
QPalette pal = lbl.palette();
pal.setColor(QPalette::Window, Qt::white);
lbl.setPalette(pal);
lbl.setFrameStyle(QFrame::NoFrame);
reportlayout->addWidget(&lbl);
reportlayout->generatePDF(reportfilename);
reportlayout class:
gridlayout->setContentsMargins(0,0,0,0);
gridlayout->setSpacing(0);
QGraphicsWidget* page = new QGraphicsWidget();
page->setLayout(gridlayout);
scene->addItem(page);
printer->setOutputFileName(filename);
painter->begin(printer);
scene->render(painter);
painter->end();
I have a feeling that it is the layout doing this, as the lines are between cells in the grid - but the layout doesn't have any color properties and I couldn't find anything to do with divider lines.
Thanks a bunch!
Have you tried stylesheets?
For example,
setStylesheet("QLabel { border:0px solid black; }");
You must investigate all possible selectors till find which one introduces the border.

QTableView with an editor for all fields

I am trying to modify the QTableView to always show all editors. I am ok with the workaround to call openPersistentEditor() on all cells.
However I'd like the content of the cells to not be selected and no text cursor for empty fields.
This is what I get:
And this is what I'd like to have:
I tried using clearSelection() and clearFocus() but that does not do the trick. If I click on each cell I get the desired result and I could do the same thing programmatically, but I'd to know if there's a more direct way.
I had this exact same issue. I ended up just adjusting the selection color and selection background color on the QLineEdits. You can do it on all QLineEdits or just on a custom QLineEdit by giving each editor an object name and referencing that in the stylesheet.
/* applies to all QLineEdits in the application */
QLineEdit {
selection-background-color: white;
selection-color: black
}
/* applies to all QLineEdits with the object name "custom" in the application */
QLineEdit#custom {
selection-background-color: white;
selection-color: black
}