Creating a QMessageBox without Button - qt

Despite setting setStandardButtons(0); it doesn't close the msgBox.
QMessageBox msgBox;
msgBox.setText("My List");
msgBox.setStyleSheet("QDialog { border: 1px solid black;}");
msgBox.setStandardButtons(0);
QTimer::singleShot(5000, &msgBox, SLOT(close()));
msgBox.exec();

Use accept instead of close.
msgBox.setStandardButtons(QMessageBox::NoButton);
QTimer::singleShot(5000, &msgBox, &QMessageBox::accept);

Related

Problem with having different background color for QWidget for hover and selected state

Trying to style qt widget, I need different colors for hover, while the mouse is pressed, disabled and normal state. I m not able to change color for the pressed state. Please, help?
QWidget
{
background-color: rgb(170, 170, 255);
}
QWidget:hover
{
background-color: rgb(0, 170, 0);
}
QWidget:pressed
{
background-color: rgb(255, 0, 0);
}
QWidget does not have ":hover" and ":pressed" pseudo-states supported. However, you can simulate these pseudo-states through dynamic properties. Still, in order to use such a mechanism, you would have to do the following things first (you can choose whichever you like):
Install event filter on your QWidget instance. This event filter would have to react to the following events: QEvent::Enter and QEvent::Leave for hover state. And then, QEvent::MouseButtonPress would need to be used for the mouse press state. Regarding event types, you can read more here. Also, there might be circumstances where you would need to react to the current mouse position. However, by not knowing your exact use case it is hard to tell if you need that or not.
Implement custom class that would be a child of QWidget. By doing so, you would need to override the following methods:
virtual void enterEvent(QEvent *event)
virtual void leaveEvent(QEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
Now, in each implementation you would have to set your own dynamic property. The following example illustrates just how to set a dynamic property and ensure it gets styled on run-time:
ui->someWidget->setProperty("property_applied", true);
// This is the limitationn. You can read more about it here: https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
ui->someWidget->style()->unpolish(ui->someWidget);
ui->someWidget->style()->polish(ui->someWidget);
When styling by dynamic properties, you can set the following stylesheet:
#someWidget[property_applied=true] {
background-color: #ff0000;
}
#someWidget[property_applied=false] {
background-color: #00ff00;
}
In other words, it would be not too hard to implement the following styling mechanism through usage of event filters or custom widget implementation and dynamic properties:
QWidget {
background-color: rgb(170, 170, 255);
}
QWidget[hover=true] {
background-color: rgb(0, 170, 0);
}
QWidget[pressed=true] {
background-color: rgb(255, 0, 0);
}

Qt use palette color in stylesheet

In qt you normally set the color of a QWidget with the QPalette.
Example:
QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());
QLineEdit *line = new QLineEdit();
line->setPalette(palette);
Now I have a little problem. It is not possible to change the bordercolor of a QLineEdit with the QPalette. That means, that I have to use a QStyleSheet.
Example:
QLineEdit *line = new QLineEdit();
line.setStyleSheet("border: 1px solid green");
But now I can't set the basecolor of the QLineEdit with QPalette, because the background-color of QLineEdit is not longer connected to QPalette::base.
That means, that the following code wouldn't change the background-color of the QLineEdit:
QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());
QLineEdit *line = new QLineEdit();
line->setPalette(palette);
line->setStyleSheet("border: 1px solid green");
But it is not possible, to define the background-color of the QLineEdit in the StyleSheet, because the background-color of the QLineEdit have to be dynamically.
My question: How to connect the background-color of the QLineEdit with QPalette::base to define the background-color of QLineEdit dynamically with QPalette?
Alternatively:
line->setStyleSheet(QStringLiteral(
"border: 1px solid green;"
"background-color: palette(base);"
));
Reference: http://doc.qt.io/qt-5/stylesheet-reference.html#paletterole
Using PaletteRole also lets the CSS be in a separate file/source.
Just construct the required QString at runtime...
auto style_sheet = QString("border: 1px solid green;"
"background-color: #%1;")
.arg(QPalette().color(QPalette::Base).rgba(), 0, 16);
The above should result in a QString such as...
border: 1px solid green;
background-color: #ffffffff;
Then...
line->setStyleSheet(style_sheet);
I found a solution for my situation. Because I only want to mask the border, and don't want to color it, I can use the method QLineEdit::setFrame(bool). But what is, if I want to color the frame like in my example above? I didn't find a solution for that so far. I am happy about every answer.

Background color of QComboBox does not change properly on windows 7

I am using QT (latest-5.6.0 msvc 64-bit) and want the comboBox to be displayed as white but it does not seem display correctly on win 7.
I have tried one or more combinations of the following:
QPalette p = ui.comboBox->palette();
p.setColor(QPalette::Active, QPalette::Button, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
p.setColor(QPalette::Active, QPalette::Background, Qt::white);
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Background, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::white);
p.setColor(ui.comboBox->backgroundRole(), Qt::white);
ui.comboBox->setPalette(p)
But none of them do change the display at least on win 7. I have also tried this:
ui.comboBox->setStyleSheet("QComboBox { background-color: white; }");
But the display became this:
Although it is white the down pointer has a grey box around it. I want it more like this:
Edit: I have tried this:
ui.comboBox->setStyleSheet("QComboBox { background-color: white; } QComboBox::drop-down { background-color: white; }");
but down arrow is not shown. How to change color while still showing the arrow?
Any solutions, suggestions, or ideas? Thanks.
Well currently, I found a solution. while it may not be the best, works which is to add a custom down arrow image (black arrow on a white background) and use it with stylesheet as follows:
ui.comboBox->setStyleSheet("
QComboBox { background-color: white; }
QComboBox::drop-down { image:url(:/Stock_Purchase_Simulator/Resources/arrow.png); }");

What's the right way to apply stylesheet on a class derived from QPushButton?

i wrote a simple class that derives QPushButton, and tried to apply stylesheet on it.
but it didn't work.
I read the qt doc, but i could not find the point.
Can any one help me out?
class Button : public QPushButton
{
Q_OBJECT
public:
Button(QWidget * parent = NULL);
~Button();
protected:
void keyPressEvent(QKeyEvent * event);
};
Button * btn = new Button(rootframe);
// I tried the following ways, all NG.
btn->setStyleSheet("background: white; color: blue;");
btn->setStyleSheet("QPushButton{background: white; color: blue;}");
btn->setStyleSheet("Button {background: white; color: blue;}");
Thanks..
From the Qt Style Sheets Reference:
Warning: If you only set a background-color on a QPushButton, the
background may not appear unless you set the border property to some
value. This is because, by default, the QPushButton draws a native
border which completely overlaps the background-color.
This should work
btn->setStyleSheet("background-color: white; color: blue; border: none");
It is advisable to check the QPushButton stylesheets example.

How do you remove the border from a QPushButton?

I have some QPushButtons in the rows of a QTreeView, and they're showing up with these black borders around them that I can't seem to modify. Currently I can grey out the buttons with this code:
for (int i = 0; i < QPalette::NColorRoles; i++){
QPalette::ColorRole thisRole = static_cast<QPalette::ColorRole>(i);
QColor newColor = commitPalette.color(QPalette::Disabled,thisRole);
int grayColor = qGray(newColor.rgb());
newColor.setRgb(grayColor,grayColor,grayColor,50);
commitPalette.setColor(QPalette::Disabled, thisRole, newColor);
}
But it doesn't do anything to the border. I'd prefer to avoid using stylesheets, as I like the automatic color generation provided by QPalette's constructor
If you are using Qt creator, right click the QPushButton and setStyleSheet as border: none; Thats it.
If you set the QButton property isFlat = true it should disable the border unless it's being clicked.
I suggest using a stylesheet. From the code you can make it to a function:
void setFlatStyle(QPushButton *btn)
{
btn->setStyleSheet(QString("QPushButton {border: 0px;}"));
}
Just pass the button in there and get your result.
button.setStyleSheet("QPushButton { border: none; }")
As said #RajaRaviVarma

Resources