QT style sheet for HLine / VLine color

I'm new to this Qt thing and its whole style system. My experience with HTML / CSS helps a little to understand the system, but a lot of things just happen for no apparent reason ... or not.

In any case, the mystery of HLINE and VLINE and how to change the color of the lines is a mystery to me. I found out from other questions and forums that it has to do with QFrame elements. And I can change the line color if I just use something like

QFrame
{
color: red;
}

      

But this of course changes the color of a lot of other things that use QFrame as well. Of course, I could go into the HLINE element and put it there color: red;

and that works great, but my application requires me to put everything in one style sheet, which is loaded into the application. So styling individual elements is not an option.

The solution will look something like this:

QFrame HLine, QFrame VLine
{
color: red;
}

      

+5


source to share


4 answers


QFrame[frameShape="4"] /* QFrame::HLine == 0x0004 */
{
    color: red;
}

QFrame[frameShape="5"] /* QFrame::VLine == 0x0005 */
{
    color: green;
}

      



+12


source


HLine

and VLine

complex in style. It's worth taking a look at the " Detailed Description " section of the documentation. For a quick fix, I found that this set of rules allows you to customize the appearance of such strings through a stylesheet in a reliable and relatively clean way:

QFrame[frameShape="4"],
QFrame[frameShape="5"]
{
    border: none;
    background: red;
}

      

This works regardless of the property frameShadow

, which would otherwise affect their appearance and the effect of the style rules. Keep in mind that the width of the lines are not 1px by default - it can be changed using the properties of min-width

, max-width

, min-height

or max-height

, if it is necessary.

Read more about my findings below.


Most QFrame

have QFrame :: Plain default frameShape, but HLine

and VLine

default frameShape - QFrame::Sunken

, which means that they do not actually line and thin boxes that contain a middle line, which is used for 3D-effect. From the doc:

The middle line width defines the width of the extra line in the middle of the frame, which uses the third color to produce a special 3D effect. Note that the middle row is only displayed for Box, HLine and VLine frames that are raised or sunk.



If you set the frameShape to Plain, that middle line will still be visible and can be styled with an attribute color

(note: this is not background-color

or border-color

!)

But that doesn't work for HLine / VLine, which left Sunken as default. One way to fix this might be to set separate styles for the Plain and Sunken QFrames using decimal attribute selectors of property enumerations (which are documented in the hehadecimal documentation), for example:

/* Reference (from doc.qt.io/qt-5/qframe.html#types):
 * - frameShape[4] --> QFrame::HLine = 0x0004
 * - frameShape[5] --> QFrame::VLine = 0x0005
 * - frameShadow[16] --> QFrame::Plain = 0x0010 (default for most widgets)
 * - frameShadow[48] --> QFrame::Sunken = 0x0030 (default for HLine/VLine)
 */
QFrame[frameShape="4"][frameShadow="16"],
QFrame[frameShape="5"][frameShadow="16"]
{
    ...
}

QFrame[frameShape="4"][frameShadow="48"],
QFrame[frameShape="5"][frameShadow="48"]
{
    ...
}

      

but since the styles that work for HLine / VLine with QFrame :: Sunken also work for those who have it QFrame::Plain

, it's usually a waste. I am showing them above for educational value only on how to use the attribute selector.

The best approach is to treat the QFrame as the box it is, and either (1) set border-top

it or border-right

in conjunction with max-height: 0px

(or max-width

for the VLine) to ensure that the inside of the box doesn't take up space in the layout; or (2) use the background color associated with border: none

(in this case the max-height / width must be 1 or greater, otherwise the QFrame is invisible). The last solution I would recommend, as shown in the first block of code above.

Hope this helps!

+7


source


but my application requires me to put everything in one stylesheet, which is loaded into the application.

You can use Conflict Resolution . Suppose you have a QMainWindow object with a lot of widgets on it. Set these stylesheets for the maindionw stylesheet :

QLabel#label{
    background-color: rgb(255, 170, 255);
}
QPushButton#pushButton{

    color: rgb(0, 0, 255);
}
QFrame#line{
    background-color: rgb(0, 170, 255);
}

      

The first css just changes the name label QLabel

on your main window and sets its color back to rgb (255, 170, 255) . The next color will change the color of the text QPushButton

named pushButton to (0,0,255) ;. The third is to change the string property. Lines are just QFrame.
So the solution I can suggest is to put your css in a file and then load that file with QFile

and QTextStream

and then set the file content for the main viewfinder or main widget css using a function setStyleSheet ( const QString & styleSheet )

, or If you are using the creator, just click right click in the main window and select change stylesheet and then paste your css. But keep in mind that you must use conflict resolution...

+1


source


According to the QDarkStyleSheet problem , you can use:

QFrame[width="3"], QFrame[height="3]

      

These selectors seem to work cross-platform and are unlikely to change. Probably better than using the values enum

as quality ints

, as they are likely to change in Qt versions, but the line style will not, since they meet certain requirements.

0


source







All Articles