I created a QFrame and added a layout which contain QSizeGrip to resize that QFrame.
Here is my code.pp:
DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
//Set flag to resize
setWindowFlags(Qt::SubWindow);
QGridLayout *layout = new QGridLayout(this);
layout->setContentsMargins(QMargins());
layout->setSpacing(0);
//Add size grip (to resize) to layout
QSizeGrip * sizeGrip = new QSizeGrip(this);
layout->addWidget(sizeGrip, 0, 0, 1, 1, Qt::AlignBottom | Qt::AlignRight);
}
I want to design that QFrame has a background like this:
Moreover is there any way to customize the icon resize of QSizeGrip (make it more clearly...).
You can customize the QSizeGrip using style sheet.
Here an example: Customizing QSizeGrip
Here the Qt style sheet documentation: Qt Style Sheets
In your case you can add this line of code:
sizeGrip->setStyleSheet("QSizeGrip { image: url(yourFolder/yourImage.png); }");
In a similar way you can customize the QFrame background, setting an image that will be repeated. See Customizing QFrame and List of properties
EDIT
Another approach is subclassing the QSizeGrip and reimplement the paintEvent() method (and maybe sizeHint() depending on the icon size). Here an example Analog Clock Example
Related
In JavaFx I can easily add a CSS style class as follows:
Scene scene = new Scene(new Group(), 500, 400);
scene.getStylesheets().add("path/stylesheet.css");
......
Label label = new Label("Cool Looking Styled Label");
label.getStyleClass().add("my-label-style");
css
.my-label-style {
-fx-font: 16px "Serif";
-fx-padding: 10;
-fx-background-color: #CCFF99;
}
How can I go about adding a style class to a QWidget, QLabel, for example?
label->setProperty("class", "my-label-style");
Then in a CSS you can call it normally by:
.my-label-style {
[..]
}
The method is in the QWidget base class; it's QWidget::setStyleSheet.
you have two ways to do that
coding
widget.setStyleSheet("css code");
Form ui
right click on the widget
select change stylesheet
a dialog box appears where you need to write css codes
but all css code are not supported ,you need to see documentation of stylesheet
I'm trying to change the background color of the QLineEdit and I can't figure it out at all.
I tried using stylesheets originally like this
QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");
but that didn't do anything. I tried using QPalette like this
QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);
but this didn't do anything either. I've been looking all day and can't find anything. am I doing something wrong or is there another way to do it?
You can set the background and text colors of line edit by setting the palette like :
QLineEdit *le = new QLineEdit();
QPalette palette;
palette.setColor(QPalette::Base,Qt::black);
palette.setColor(QPalette::Text,Qt::white);
le->setPalette(palette);
Works fine for me:
QLineEdit *le = new QLineEdit();
le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
Your code is almost correct. Only QLine edit uses the Base color. So if you do not want to replace existing stylesheet which can contain borders padding and margin and you want to change background only, use QPalette:
QPalette palette = _ui->lnSearch->palette();
palette.setColor(QPalette::Base, Qt::green);
_ui->lnSearch->setPalette(palette);
Thanks to: https://forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect
I had to use background-color from standard css like this:
QLineEdit* edit = new QLineEdit();
edit->setStyleSheet("QLineEdit {background-color: black;}");
I am using Qt 5.4
I'm using Qt Framework to build an App supporting multiple languages.
The default font is loaded from StyleSheet.
I override paintEvent() method, and setFont() method works OK for all widgets except for QLabel and QComboBox.
For QComboBox, the selected item has the correct font, the but the dropdown list items are using the default font. The Qt manual says setFont will set the font for both the comboBox button and the comboBox popup list to font.
Anyone happens to see this problem and have an idea to fix that? Thanks.
Answer is so long, because I wrote different approaches, choose the best for you.
Try to do next:
Create QListView, customize it (with stylesheet for example)
Set model with your data and set view to QComboBox with special methods:
setModel() and setView()
http://qt-project.org/doc/qt-4.8/qcombobox.html#setView
setStyleSheet("font-family: Arial;font-style: normal;font-size: 12pt");
For label you can use stylesheet too, setFont or just set HTML code with suitable font:
QFont f( "Arial", 14, QFont::Bold);
label->setFont(f);
With ComboBox you can use this for example:
QStringList stringList;
stringList << "#hello" << "#quit" << "#bye";
QStringListModel *mdl = new QStringListModel(stringList);
QFont comboFont("Arial",16,-1,true);
QListView *vw = new QListView;
vw->setFont(comboFont);
ui->comboBox->setModel(mdl);
ui->comboBox->setView(vw);
But it will install font to your data in popup menu, not in the header, so you can use also next:
QFont comboFont("Arial",16,-1,true);
for(int i = 0; i< ui->comboBox->count(); i++)
{
ui->comboBox->setItemData(i,QVariant(comboFont),Qt::FontRole);
}
ui->comboBox->setFont(comboFont);
Wityh this code snippet you'll get popup menu and header with this font and you don't need create models and views.
My dear, it is enough to do hereunder:
ui->CboxOpisBaza->lineEdit()->setFont(QFont("MS Shell Dlg 2", 12));
I set the style sheet of a widget, and then I add a child widget after that. But it seems that contentsMargins() does not return the real margins until after the child widget is shown.
Sample code:
this->setStyleSheet("QLabel {padding: 0px 5px 10px 15px;}");
QLabel *label = new QLabel(this);
qDebug() << label->contentsMargins();
label->show();
qDebug() << label->contentsMargins();
This is the output:
QMargins(0, 0, 0, 0)
QMargins(15, 0, 5, 10)
Note that if I were to swap the order, i.e. adding the child widget before setting the parent's style sheet, the problem would disappear.
But for various reasons, I can't add the child widget before setting the style sheet, and I also need to be sure that I'm not being fed false data from contentsMargins(). Is there a workaround to ensure that?
You can call QWidget::ensurePolished to make sure the widget has the correct styling. This is what QWidget uses internally right before it is shown.
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.