QComboBox ListView is occuping combobox frame - c++

the comboBox contains two entries and , when the combobox is clicked, the listview occupies the frame of combobox. I want the list view just below the combobox. stylesheet is as shown below:
QComboBox {
border: 1px solid #8F9EAB;
border-radius: 3px;
font: 16pt "Arial";
min-width: 6em;
color: #1B3067;
background-color: rgb(255, 255, 255);
padding-left:10px;
}
QComboBox:pressed
{
border: 1px solid #1562AD;
color: rgb(255, 255, 255);
background-color: #87A6D5;
}
QComboBox:disabled
{
border: 1px solid #BFBFBF;
color: #BFBFBF;
background-color: rgb(255, 255, 255);
}
QComboBox::down-arrow
{
background-image: url(:/Images/data/img/Images/DOWN_enabled.png);
height: 7px;
width : 13px;
}
QComboBox::drop-down
{
subcontrol-position: top right;
width: 40px;
color: white;
border-left-width: 0px;
border-left-color: #8F9EAB;
border-left-style: solid;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
QComboBox QListView
{
font: 16pt "Arial";
color: rgb(80, 80, 80);
}
QComboBox QAbstractItemView::item
{
border-bottom: 5px solid white; margin:3px;
}

The position of the view() can not be handled with Qt Style Sheet, you must create a class that inherits from QComboBox and overwrite the showPopup() method as shown below.
class ComboBox: public QComboBox{
public:
using QComboBox::QComboBox;
public:
void showPopup(){
QComboBox::showPopup();
QPoint pos = mapToGlobal(QPoint(0, height()));
view()->parentWidget()->move(pos);
}
};

Related

How to change scroll bar margin in QListWidget

I need that scroll bar in QListWidget have
margin = 6;
you should use a stylesheet, for example look at this :
QScrollBar:horizontal {
background: transparent;
height: 10px;
margin: 0;
}
QScrollBar:vertical {
background: transparent;
width: 10px;
margin: 0;
}
QScrollBar::handle:horizontal {
background: #374146;
min-width: 16px;
border-radius: 5px;
}
QScrollBar::handle:vertical {
background: #374146;
min-height: 16px;
border-radius: 5px;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal,
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
background: none;
}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal,
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
border: none;
background: none;
}
QListWidget QScrollBar::handle:horizontal {
background-color:rgb(0, 170, 171);
}
QListWidget QScrollBar::handle:vertical {
background-color:rgb(0, 170, 171);
margin:2;
}
output
I set margin 2, margin 6 looks like this

Button style on the form

Please tell me how to correctly specify the arguments for the fact that all the buttons on the form had a style when you hover the mouse over these buttons? In addition to the style for the button, there is also a style for the form itself.
I specify it in the constructor:
{
ui->setupUi(this);
this->setStyleSheet("background: rgb(49, 54, 59); color: rgb(220, 221, 218); selection-color: lightyellow; selection-background-color: darkcyan;"
"QPushButton::hover {color: darkcyan; border: 2px solid grey; border-radius: 1px};");
}
It works if I only specify a separate style for the button, but I need to combine all the styles into one entry.
this->setStyleSheet("QPushButton::hover {color: darkcyan; border: 2px solid grey; border-radius: 1px};");
I think you are looking for this
this->setStyleSheet("QWidget { background: rgb(49, 54, 59); color: rgb(220, 221, 218); selection-color: lightyellow; selection-background-color: darkcyan; }"
" QPushButton::hover {color: darkcyan; border: 2px solid grey; border-radius: 1px};");
If your main form is QMainWindow you can also set style only for QMainWindow but not for all QWidgets
this->setStyleSheet("QMainWindow { background: rgb(49, 54, 59); color: rgb(220, 221, 218); selection-color: lightyellow; selection-background-color: darkcyan; }"
" QPushButton::hover {color: darkcyan; border: 2px solid grey; border-radius: 1px};");

QSlider handle ignores border radius when height is reduced

I am trying to create a gui application to control the volume level of my machine using Qt5 and C++.
This is what I kind of want to achieve.
So, I created the basic layouts and added the QSlider, and then used the following stylesheet to style it:
QSlider::groove:horizontal
{
height: 16px;
background: yellow;
margin: 2px;
}
QSlider::handle:horizontal
{
background: blue;
width: 16px;
height: 16px;
margin: -5px 6px -5px 6px;
border-radius:11px;
border: 3px solid #ffffff;
}
I got the following result:
First the handle is an eclipse, and not a circle. But I wanted to reduce the height of the groove, so I modified the above stylesheet:
QSlider::groove:horizontal
{
height: 10px; // modified here
background: yellow;
margin: 2px;
}
QSlider::handle:horizontal
{
background: blue;
width: 16px;
height: 16px;
margin: -5px 6px -5px 6px;
border-radius:11px;
border: 3px solid #ffffff;
}
And now, the handle of the slider became a rectangle.
Can anyone please answer what's causing it to behave like this, or point me to some docs.
your border-radius should be proportional to the length and width to become a circle.
Try this style :
QSlider::groove:horizontal {
border-radius: 1px;
height: 3px;
margin: 0px;
background-color: rgb(52, 59, 72);
}
QSlider::groove:horizontal:hover {
background-color: rgb(55, 62, 76);
}
QSlider::handle:horizontal {
background-color: rgb(85, 170, 255);
border: none;
height: 40px;
width: 40px;
margin: -20px 0;
border-radius: 20px;
padding: -20px 0px;
}
QSlider::handle:horizontal:hover {
background-color: rgb(155, 180, 255);
}
QSlider::handle:horizontal:pressed {
background-color: rgb(65, 255, 195);
}
And This is for your style (I change margin and padding)
QSlider::groove:horizontal
{
height: 16px;
background: yellow;
margin: 2px;
}
QSlider::handle:horizontal
{
background: blue;
width: 16px;
height: 16px;
margin: -4px 0;
padding: -4px 0px;
border-radius:11px;
border: 3px solid #ffffff;
}

QMenu: How to customize the menu items of QMenu

I want to build a dropdown list control with QPushButton and QMenu like below:
QPushButton* menuBt = new QPushButton("Please select");
menuBt->setFlat(true);
QMenu* menu = new QMenu();
menuBt->setMenu(menu);
QWidgetAction* wa1 = new QWidgetAction(menu);
QLabel* l1 = new QLabel("Option1");
wa1->setDefaultWidget(l1);
menu->addAction(wa1);
QWidgetAction* wa2 = new QWidgetAction(menu);
QLabel* l2 = new QLabel("Option2");
wa2->setDefaultWidget(l2);
menu->addAction(wa2);
menu->setStyleSheet("QMenu::item {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:hover {background-color: rgb(0, 0, 255);}");
menuBt->setStyleSheet("QPushButton {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
I set the font and hover background color to the menu items by setStyleSheet, but seems it doesn't work.
How make the font and hover background color work on menu items?
Answer:
class QTDropDownButton : public QPushButton
{
Q_OBJECT
public:
QTDropDownButton(QString text, QWidget *parent = nullptr);
void addItem(QString text);
protected slots:
void menuAboutToShow();
private:
QMenu* menu_;
};
QTDropDownButton::QTDropDownButton(QString text, QWidget *parent) :
QPushButton(text, parent)
{
setFlat(true);
menu_ = new QMenu();
setMenu(menu_);
connect(menu_, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);");
menu_->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:selected {background-color: rgb(0, 255, 255);}"
"QLabel {font-family: Arial; font-size: 13pt;}"
"QLabel:hover {background-color: rgb(0, 0, 255);}");
}
void QTDropDownButton::addItem(QString text)
{
if(!menu_)
return;
QWidgetAction* wa1 = new QWidgetAction(menu_);
QLabel* l1 = new QLabel(text);
wa1->setDefaultWidget(l1);
menu_->addAction(wa1);
}
void QTDropDownButton::menuAboutToShow()
{
if(menu_)
menu_->setFixedWidth(this->width());
}
To set the font-family you don't need to put quotes around the Arial. I believe this prevents your style sheet from parsing correctly.
A side note: at the moment only your menuBt is styled, other buttons will look like default buttons. To change button style for all buttons in the menu, move the style into the setStylesheet() call of the menu like this:
menu->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}" +
"QMenu::item:hover {background-color: rgb(0, 0, 255);}" +
"QPushButton {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
But if you want only this one button to look different, it is correct to call
setStylesheet() on it, but you can omit the selector, like this:
menuBt->setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);");

QWebView plain text document style

In my app I have several places that use a QWebView and load the content from the server. In addition the app allows the user to select a default or a dark theme. When the content-type of the document to display is text/plain and the application theme is set to dark, the text is displayed as black text on a black background. I can only see the text if I highlight it. text/html documents work well with either theme.
Ideally when the dark theme is selected I want to display white text on a black (or dark gray) background. I have not been able to figured out how to do this.
Here is the code I currently use to set up the theme:
if(Theme == "dark")
{
app.setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(88,88,88));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(25,25,25));
darkPalette.setColor(QPalette::AlternateBase, QColor(88,88,88));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53,53,53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(150, 200, 255));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
app.setPalette(darkPalette);
} else {
Theme = "default";
app.setStyle(QStyleFactory::create("Fusion"));
app.setPalette(DefaultPalette);
}
// load the style sheet
QFile file(":/themes/" + Theme + "theme.css");
file.open(QIODevice::ReadOnly);
const QByteArray theme = file.readAll();
app.setStyleSheet(theme);
The default theme style sheet is blank and the dark style sheet is:
QToolTip {
color: #ffffff;
background-color: #2a82da;
border: 1px solid white;
}
QScrollBar:horizontal {
border: 1px solid grey;
background: #606060;
height: 20px;
margin: 0px 20px 0px 20px;
}
QScrollBar::handle:horizontal {
border: 1px solid #909090;
background: #303030;
min-width: 20px;
}
QScrollBar::add-line:horizontal {
border: 1px solid #909090;
background: #303030;
image: url(:/themes/themes/dark/sbright.png);
width: 20px;
subcontrol-position: right;
subcontrol-origin: margin;
}
QScrollBar::sub-line:horizontal {
border: 1px solid grey;
background: #303030;
image: url(:/themes/themes/dark/sbleft.png);
width: 20px;
subcontrol-position: left;
subcontrol-origin: margin;
}
QScrollBar:vertical {
border: 1px solid grey;
background: #606060;
width: 20px;
margin: 20px 0 20px 0;
}
QScrollBar::handle:vertical {
border: 1px solid #909090;
background: #303030;
min-height: 20px;
}
QScrollBar::add-line:vertical {
border: 1px solid #909090;
background: #303030;
image: url(:/themes/themes/dark/sbdown.png);
height: 20px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}
QScrollBar::sub-line:vertical {
border: 1px solid grey;
background: #303030;
image: url(:/themes/themes/dark/sbup.png);
height: 20px;
subcontrol-position: top;
subcontrol-origin: margin;
}