QTextDocument default style sheet - css

I try to draw a text on a QImage using the QTextDocument.
How to set a default ("body") style?
Currently I use a <p> tag, like this
QPainter painter(_image);
QTextDocument doc;
doc.setDefaultStyleSheet("p { color : green; background-color : black; }");
doc.setHtml("<p>test 123</p>");
doc.drawContents(&painter,_image->rect());
I would like to get rid of <p> tag
doc.setHtml("test 123");
I have tried
doc.setDefaultStyleSheet("body { color : green; background-color : black; }");
doc.setDefaultStyleSheet("{ color : green; background-color : black; }");
doc.setDefaultStyleSheet("color : green; background-color : black; ");
doc.setDefaultStyleSheet("QImage { color : green; background-color : black; }");
EDIT:
I have also tried
doc.setDefaultStyleSheet("* { color : green; background-color : black; }");

You should wrap content with <body> tag:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextDocument doc;
doc.setDefaultStyleSheet("body { color : green; background-color : black; }");
doc.setHtml("<body>test 123</body>");
QTextEdit textEdit;
textEdit.setDocument(&doc);
textEdit.show();
return app.exec();
}

Related

Trying to get rid of the white border in the ComboBox

enter image description here
I am new to CSS and I am trying to get rid of the white space (as seen in the image) in the drop menu of the combo box... setting the background black didn't work... this is my CSS for the combo box
.combo-box {
-fx-border-width : 1 ;
-fx-border-color : #29a8a6;
-fx-text-fill: #29a8a6;
-fx-border-radius: 50;
-fx-padding : 0;
-fx-background-color: #29a8a6;
-fx-background-radius: 50;
}
.combo-box .list-cell{
-fx-prompt-text-fill : #29a8a6;
-fx-text-fill: #29a8a6;
-fx-background-color: black;
-fx-padding : 2;
-fx-cell-border : 0;
-fx-border-width: 0;
-fx-border-radius: 50;
-fx-background-radius: 50;
}
To get rid of the white space in the drop down menu you can add this to your css file:
.combo-box .list-view {
-fx-background-color: black;
}
PS:
Whenever you are not sure which elements styling you need to change, you can make use of Scenic View to find out the element.
apparently the problem was the listview in the combo-box , so setting the insets to 0 solved my problem
.combo-box {
-fx-background-color:#29a8a6;
-fx-background-radius : 40;
}
.combo-box .list-view {
-fx-prompt-text-fill : #29a8a6;
-fx-background-color : black;
-fx-background-radius : 40;
-fx-insets : 0;
}
.combo-box .list-cell{
-fx-prompt-text-fill : #29a8a6;
-fx-text-alignment : CENTER;
-fx-background-radius : 40;
-fx-background-color : black;
}

How do I delete the drop-down arrow of the drop-down menu while keeping the border-image of QSS?

I changed a QPushButton into a drop-down menu. My codes are as follows:
m_menu = new QMenu(this);
m_addAction = new QAction(m_menu);
m_delAction = new QAction(m_menu);
m_addAction->setText(QObject::tr("add"));
m_delAction->setText(QObject::tr("del"));
m_menu->addAction(m_addAction);
m_menu->addAction(m_delAction);
m_menu->setStyleSheet("\
QMenu {\
background-image: url(:/img/tanchu-1.png);; /*background-image*/\
border: 3px solid rgb(235,110,36);/*menu border*/\
}\
QMenu::item {\
font-size: 10pt; \
color: rgb(225,225,225);\
border: 3px solid rgb(60,60,60);\
background-image: url(:/img/tanchu-1.png);\
padding:160px 160px;\
margin:2px 2px;\
}\
QMenu::item:selected { \
background-color:rgb(235,110,36);\
}\
QMenu::item:pressed {\
border: 1px solid rgb(60,60,61); \
background-color: rgb(220,80,6); \
}\
");
ui->pushButton->setMenu(m_menu);
and the button I get looks like this. There is a black drop-down arrow in the lower right corner.
But now the problem is that when I remove the drop-down arrow from the drop-down menu by using
ui->pushButton->setStyleSheet("QPushButton::menu-indicator{image:none;}");
the border-image that I set to the QPushButton with QSS border-image: url(:/img/btn_mid_0.png);disappears like image2 . The drop-down arrow and the border-image disapper together. How do I remove the drop-down arrow while maintaining the border-image I set before?
Besides, the The width of the submenu does not change with the qpushbutton like image3. How can I set the width of the drop-down menu to be as wide as the QPushButton?
Thanks!
All my codes are as follows:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QMenu *m_menu;
QAction *m_addAction;
QAction *m_delAction;
QPushButton *m_pushButton;
void addFunc();
void delFunc();
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_menu = new QMenu(this);
m_addAction = new QAction(m_menu);
m_delAction = new QAction(m_menu);
m_addAction->setText(QObject::tr("add"));
m_delAction->setText(QObject::tr("del"));
m_menu->addAction(m_addAction);
m_menu->addAction(m_delAction);
connect(m_addAction, &QAction::triggered, this, &MainWindow::addFunc);
connect(m_delAction, &QAction::triggered, this, &MainWindow::delFunc);
m_menu->setStyleSheet("\
QMenu {\
background-image: url(:/img/tanchu-1.png);; /*background-image*/\
border: 3px solid rgb(235,110,36);/*menu border*/\
}\
QMenu::item {\
font-size: 10pt; \
color: rgb(225,225,225);\
border: 3px solid rgb(60,60,60);\
background-image: url(:/img/tanchu-1.png);\
padding:160px 160px;\
margin:2px 2px;\
}\
QMenu::item:selected { \
background-color:rgb(235,110,36);\
}\
QMenu::item:pressed {\
border: 1px solid rgb(60,60,61); \
background-color: rgb(220,80,6); \
}\
");
ui->pushButton->setMenu(m_menu);
ui->pushButton->setStyleSheet("QPushButton::menu-indicator{image:none;}");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addFunc()
{
qDebug() << "addFunc";
}
void MainWindow::delFunc()
{
qDebug() << "delFunc";
}
Just set all the button CSS in one place, either in the QtCreator/Designer properties, or in the C++ code, not both. Basically your C++ CSS is overriding what you set in designer view.
In Designer view just use something like this for the styleSheet property:
QPushButton { border-image: url(:/img/btn_mid_0.png); }
QPushButton::menu-indicator { image: none; }
Or the same thing in C++ but with quotes :)
As for the menu styling... I'm not sure what's going on in there now (looks strange!) but the simplest way to have their sizes match is to set the size for both explicitly, either in CSS (width: XXXpx;) or in C++ (QWidget::setFixedWidth(XXX)). The C++ version may work better if the button is managed by a layout, since what happens with CSS size properties then gets vague. For CSS sometimes min-width and/or max-width work better than just width.

Simplest way to create simple monochromatic button

I need to create simple monochromatic button, just black frame with black text (I know, with 1bit it will be ugly) on white background. Is there any method which doesn't need paintEvent to be re-implemented?
The best way is using your own style sheet.
A basic example could be the following one. In this example, we set a style with white background, black border and black text. The opposite when the user presses the button.
Depending on your requirements, you could set the style for states like disabled, checked or hover.
main.cpp
#include <QtWidgets>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
};
#endif
mainwindow.cpp
#include <QtWidgets>
#include "mainwindow.h"
MainWindow::MainWindow()
{
QWidget *centralWidget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout;
QPushButton *pushButton = new QPushButton("PushButton");
pushButton->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 1px solid black;"
" color: black;"
" outline: none;"
"}"
"QPushButton:pressed {"
" background-color: black;"
" border: 1px solid white;"
" color: white;"
"}"
);
layout->addWidget(pushButton);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
Good examples here and here.

How do I keep the buttons from changing style on adding them to a layout box?

I'm messing around in the QT UI designer and trying to add a horizontal layout box around some buttons and a spacer. But I'm getting some weird results:
Here's the before image:
And here's the after image:
Not only did it not retain the order of the items, it also changed the style of the minus button to this weird gray box.
How do I keep it from doing that? Or how do I change the button's style back to what it was before it was added to the constraint?
I tried in Qt designer and it works. I think something overwrites your button styles.
Also have you tried to code buttons style? To use this code you need to create empty QWidget window and place two buttons.
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <QHBoxLayout>
#include <QMessageBox>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
public slots:
void plusClicked();
void minusClicked();
private:
Ui::Form *ui;
QHBoxLayout *horizontalLayout;
};
#endif // FORM_H
form.h
#include "form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
ui->pushButton->setText("+");
ui->pushButton->setStyleSheet("background-color: white; border-style: solid; border-width: 1px; border-radius: 8px; border-color: gray; width: 60px; height: 30px; font-weight: bold; font-size: 14pt;");
ui->pushButton_2->setText("-");
ui->pushButton_2->setStyleSheet("background-color: white; border-style: solid; border-width: 1px; border-radius: 8px; border-color: gray; width: 60px; height: 30px; font-weight: bold; font-size: 14pt;");
horizontalLayout = new QHBoxLayout(this);
horizontalLayout->addStretch();
horizontalLayout->addWidget(ui->pushButton);
horizontalLayout->addWidget(ui->pushButton_2);
this->setLayout(horizontalLayout);
connect(ui->pushButton, &QPushButton::clicked, this, &Form::plusClicked);
connect(ui->pushButton_2, &QPushButton::clicked, this, &Form::minusClicked);
}
void Form::plusClicked()
{
QMessageBox::information(this, "Form", "Plus button clicked!", QMessageBox::Ok);
}
void Form::minusClicked()
{
QMessageBox::information(this, "Form", "Minus button clicked!", QMessageBox::Ok);
}
Form::~Form()
{
delete ui;
}
main.cpp
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form mainWindow;
mainWindow.show();
return a.exec();
}

QTableView - Selection background color

I am using the following code for set the style for the table in simulator(S60)(Nokia Qt SDK).
searchTable->setStyleSheet("background: rgb(255,255,255);color:rgb(0,0,0); font-family:Arial Narrow;font-size:20px; border: 4px outset rgb(255,255,255);gridline-color: #669933;"
"selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #486909, stop: 1 white);"
);
But When I am selecting the element in the data I got the following output. Please find the attachment.
Please help me.... What I did wrong .. Thanks in advance.
I guess your mistake is that you set up the stylesheet only for QTableView and not for all it's child widgets: cells. You could try to write your style code into a ".qss" file, add it to your app's resource file and then load it into your main.cpp with this code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QFile file(":/qss/stylesheet.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
file.close();
qApp->setStyleSheet(styleSheet);
w.show();
}
In your style file you have to write something like this:
QLineEdit{
border: 2px solid grey;
border-radius: 10px;
padding: 0 8px;
background: white;
selection-background-color:darkgrey;
}
In this way all QLineEdit widgets will be shown with your style rules.

Resources