QLineEdit Rounded Corners? - c++

Is there a way to round the corners of a QLineEdit widget? If not is there a similar widget I could do this to?
Visual Meaning:
Solved: (See below for additional information)
QLineEdit *lineEdit = new QLineEdit;
lineEdit -> setStyleSheet("QLineEdit { border: 2px solid gray;"
"border-radius: 5px;}");

You can use StyleSheets to set styles of Qt components just like you would use them in making a website. You can set a stylesheet in two ways: in your application's code, or in QtDesiner.
To do it in QtDesiner (which is most convenient), right-click on the component which you have placed on the form, and press "Edit StyleSheet" (or maybe "Change Stylesheet", sorry, my Qt is not it English, so I'm not sure about the exact name of the option). A window will open that will let you edit the element's style sheet.
It is very convenient because it has some useful options like adding resources or colors or fonts right there, and you just need to press a couple of buttons to set the option you need through the GUI without the need to type or even to know CSS syntax.
From the code, you can do it like this (example):
SomeComponent->setStyleSheet("QLineEdit { border-radius: 5px; }");
Here is the documentation about the stylesheets.

Use stylesheets. From http://doc.qt.io/archives/qt-4.7/stylesheet-examples.html:
QLineEdit {
border: 2px solid gray;
border-radius: 10px;
}
Also, you can always override paintEvent if you want to get your hands dirty.

Related

Qt QTabWidget background color

I have been trying to set the background color of a QTabWidget to black (or any other color for), but have been unsuccessful in doing so.
It seems that you need the option autoFillBackground set and then also set "background-color: black;" in the stylesheet. This then displays it properly in the Designer, but fails in the application.
This answer suggests to enclose it in another QWidget and then use the transparency, but that is a hack around the issue.
How do I set the background color of a QTabWidget via stylesheets?
EDIT
Setting QTabBar { background-color: black; } results in the following image.
As an alternative to QTreeWidget, use QTabBar + QStackedWidget and the following stylesheet
QTabBar { background-color: black; }
or use
Qt: Styling QTabWidget

How to set only QTabWidget background color stylesheet

I have a Qt application that, among many other widget types, uses a QTabWidget. I'm having difficulty styling the background color for this object.
I've tried some of the following lines, which I found from other forum posts, in my stylesheet with no effect on the program
QTabWidget { background-color: black; }
QTabWidget::pane { background-color: black; }
QTabWidget#tabWidget { background-color: black; }
QTabWidget#tabWidget::pane { background-color: black; }
If I use QWidget { background-color: black; }, then yes my color is properly changed, but then all of the other widgets in my program are changed as well... so this isn't what I'm looking for...
I've also tried it in code with ui->tabWidget->setStyleSheet("background-color: black"); But this too is undesirable because it changes the background color of all of its children widgets.
Does anyone have any other ideas on how to style a QTabWidgets contents background area?
About a year late but I recently ran into the same problem and got it working.
First of all QTabWidget has a child QWidget for every tab you make. That is the area that you put your other widgets into, and that is what you want to set the background color of.
Set the style-sheet by doing this.
1)Determine the name of your tab widgets from the design object window top right, they should match the currentTabName that you set when creating your tab.
2)Realize this is a QWidget not a QTabWidget this is why QTabWidget { background-color: black; } does not work.
3)Realize that by specifying the object in the style-sheet with the '#' the child object will not inherit the style-sheet.
For me I specified my style-sheet as such, repeating for each tab object name that I had:
#objectName {background-color: rgb(240,240,240);}
This provided me with the exact behavior I needed. In my case I wanted to get the natural gray background onto my Tab pages but not override the child components on the tab pages.
I hope this helps someone in the future...

What would this element be called in c++ Qt?

Im making a GUI for my program and currently trying to figure out what this component would be called.
Does anyone know what this would be called or how I could achieve this effect?
Add a QListWidget to your ui form from the Qt Designer toolbox window. And click right on it then click changeStylesheet... then paste this:
QListWidget {
color:white;
border:1px solid transparent;
border-radius:10px;
padding:0px;
background:#F6911B;
show-decoration-selected: 1;
}
QListWidget::item{
border-top:1px solid #E98919;
padding: 15px;
}
QListWidget::item:hover {
border-top:1px solid #E98919;
background-color: #F6911B;
color:white;
}
QListWidget::item:selected:!active {
border-top:1px solid #E98919;
background-color:white;
color:#F6911B;
}
It looks like this:
To learn how style sheets work in relation to QListView, see http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qlistview
QListWidget is a listbox similar to the image you posted.
It is a QMenu with QActions attached to it. First you create a QAction, connect with the related slot, and attach to QMenu. Then attach the menu to a QWidget (or its subclass)

Qt stylesheet of dock area splitter

How to change the style sheet of splitter/handle beside QDockWidget when it is added to DockWidgetArea. There is one main movable handle and multiple handles between each dockwidgets on that side. I would like to change at least bg colour and border of the handle and hover colour.
It would help if I know what kind of object it is or to get some pointer to the handle to setStyleSheet on it.
Ok I found it after some digging inside source code. Finally the answer was so simple and was screaming from the documentation of style sheet:
Note: Use QMainWindow::separator to style the resize handle.
Ok so the code is simple:
QMainWindow::separator
{
background-color: green;
width: 4px;
border: none;
}

Background color of styled QLineEdit flickers

When setting the background-color of a QLineEdit using stylesheets, there is a very noticeable flicker upon mouseover of the control. Example code:
QLineEdit* flicker = new QLineEdit(this);
flicker->setStyleSheet("QLineEdit {background: red;}");
flicker->show();
This only happens when running on Windows Vista and later, and not in XP. I think it has something to do with the default styling for Windows (Aero?) applications, because setting the style to QStyle::Fusion fixes the problem:
QLineEdit* flicker = new QLineEdit(this);
QStyle* fusion = QStyleFactory::create(QString("Fusion"));
flicker->setStyle(fusion);
flicker->setStyleSheet("QLineEdit {background: red;}");
flicker->show();
Edit:
I also have an eventfilter set up such that the control gets repainted on mouseover, and the debugger is confirming that that gets called immediately.
Ran into the same problem and wanted to share a possible workaround:
The reason for the flickering of the QLineEdit on mouseover is probably that another style sheet is used for "QLineEdit:hover{...}" that still contains the defaults. Unfortunately, it does not seem to be enough to add "QLineEdit:hover{background-color: red}". The only way I found it to work correctly up until now is to use
flicker->setStyleSheet("QLineEdit{background-color: red;} QLineEdit:hover{border: 1px solid gray; background-color red;}");
Not quite sure why the border property needs to be set explicitly, but it worked for me.
I've had a similar problem and resolved it by adding a border to QLineEdit, like this:
#dont_flick_lineedit{
background-color: red;
border: 1px solid #CCC;
}
#flick_lineedit{
background-color: blue;
}